Results 1 to 13 of 13
- 05-05-2008, 09:43 PM #1
Member
- Join Date
- May 2008
- Posts
- 7
- Rep Power
- 0
Trouble with For loop and variables in a program
Hi,
Im relatively new to java, and as part of a course that i am on, need to write an application to validate a password entered by a user.
A while ago, i had my program at least semi working, but now i have done something, and it doesnt, lol.
I was wondering if anyone could identify any problems with my for loop, and subsequent code?
String getUsername;
String getPassword;
int getLength;
char c;
char a;
System.out.println("Please enter your username...");
getUsername = EasyIn.getString();
System.out.println("Welcome " + getUsername + "!");
System.out.println("Please enter your password for validation...");
getPassword = EasyIn.getString();
getLength = getPassword.length();
System.out.println("Password length = " + getLength);
System.out.println("Password is fine");
// loops over string and reports back numbers of letters
for (int i = 0; i < getLength; i++)
{
c=getPassword.charAt(i);
System.out.println("Letter " + i);
System.out.println(" = " + c);
boolean start = true;
do {
// Checks for space
boolean b_space = Character.isWhitespace(c);
if (b_space == true)
do {
System.out.println("You have a space in your password, please remove it and try again");
//start = false;
}
while(b_space == true);
if (b_space == false)
System.out.println("Password has no spaces");
The easyIn class is something that i was provided with to get input from users. The main problem seems to be that the loop prints the "password has no spaces" bit for as many times at the length of the password. The loop is mainly supposed to count the letters in the password, so, if a password was fish, letter 0 would be f, letter 1 would be i and so on.
if someone could tell me where im going wrong, i would very much appreciate it, im going mad!
thanks alot
Sam
- 05-05-2008, 10:51 PM #2
Member
- Join Date
- Apr 2008
- Posts
- 23
- Rep Power
- 0
I''m not sure without looking at the easyIn class, but is Character defined somewhere.
if not that's where your problem is.Java Code:boolean b_space = Character.isWhitespace(c);
- 05-05-2008, 11:46 PM #3
Member
- Join Date
- May 2008
- Posts
- 7
- Rep Power
- 0
Thanks for your reply!
I did look at that, and you're right, it isnt defined, anywhere! However, it doesnt complain about the fact that it isnt defined. i always thought character was built in to the java language, or Eclipse.
Do you know how i would define it, and what as?
TIA!
- 05-06-2008, 12:25 AM #4
Member
- Join Date
- Apr 2008
- Posts
- 23
- Rep Power
- 0
hmm, After closely looking at your code you can use Character like you did. Give me a few minute and I should get back to you.
Ok, your problem is you are using to many loops for your purpose just use the main for..loop. Below is my fix for you.
You can play with it and make the output they way you like. For example my code above will continue to print even if there is a whitespace.Java Code:for (int i = 0; i < getLength; i++) { c=getPassword.charAt(i); System.out.println("Letter " + i); System.out.println(" = " + c); boolean start = true; // Checks for space boolean b_space = Character.isWhitespace(c); if (b_space == true) System.out.println("You have a space in your password, please remove it and try again"); } System.out.println("Password has no spaces");Last edited by theonly; 05-06-2008 at 12:59 AM.
- 05-06-2008, 01:03 AM #5
Member
- Join Date
- May 2008
- Posts
- 7
- Rep Power
- 0
Thanks loads for that mate, it got rid of the loop problems
However, i now have new problems.
if i run the program, i get this...
Please enter your username...
sam
Welcome sam!
Please enter your password for validation...
s am
Password length = 4
Password is fine
You have a space in your password, please remove it and try again
Password has no spaces
As you can see, it adds the last message to the end, even though there is a space. I thought i might solve this by just removing the second message and having one big message at the end.
I was wondering, how would i put the rest of my checking statements in? E.g, ive tried to insert a statement after your lines so it will check if there are capitals in the password, and it doesnt work at all.
Do you know how i would add additional statements?
Thanks for your help
Sam
- 05-06-2008, 01:12 AM #6
Member
- Join Date
- May 2008
- Posts
- 7
- Rep Power
- 0
// Checks for space
boolean b_space = Character.isWhitespace(c);
if (b_space == true)
System.out.println("You have a space in your password, please remove it and try again");
boolean b_ucase = Character.isUpperCase(c);
if (b_ucase == false)
System.out.println("You have no uppercase letters");
boolean b_lcase = Character.isLowerCase(c);
if (b_lcase == false)
System.out.println("You have no lower letters");
boolean b_digit = Character.isDigit(c);
if (b_digit == false)
System.out.println("You have no digits");
}
I have tried using this, but it doesnt really work for the digit part. I think my program is essentialy doing what it is supposed to: checking each char for the type - but my problem is that it is reporting on every one.
e.g
Please enter your username...
sam
Welcome sam!
Please enter your password for validation...
TTYG676
Password length = 7
= T
You have no lower letters
You have no digits
= T
You have no lower letters
You have no digits
= Y
You have no lower letters
You have no digits
= G
You have no lower letters
You have no digits
= 6
You have no uppercase letters
You have no lower letters
= 7
You have no uppercase letters
You have no lower letters
= 6
You have no uppercase letters
You have no lower letters
(the = signs are purely there for making sure that the letters are at least picked up)
- 05-06-2008, 01:18 AM #7
Member
- Join Date
- Apr 2008
- Posts
- 23
- Rep Power
- 0
Ok, so are you require to print out every letter in the password or just to check everyone without printing them.
- 05-06-2008, 01:25 AM #8
Just a hint put your code in code braces there is a button in the advanced reply.
My IP address is 127.0.0.1
- 05-06-2008, 01:43 AM #9
Member
- Join Date
- May 2008
- Posts
- 7
- Rep Power
- 0
@ theonly
i dont need to print the letters, just to check if the entered password conforms to the requirements.
That does mean checking every letter, but as you can see from my last post, its not quite doing it correctly. Do you have any ideas?
@ Zosden
Noted, thanks mate! :)
- 05-06-2008, 02:26 AM #10
Member
- Join Date
- Apr 2008
- Posts
- 23
- Rep Power
- 0
Why yes I do have an idea :)
You can use a few nested if-statements to check each character, and if java encounter an error output an error message and then have java shut down the program.
- 05-06-2008, 02:36 AM #11
Member
- Join Date
- May 2008
- Posts
- 7
- Rep Power
- 0
I have tried doing nested IF statements before, but i dont think that gets rid of the fact that for every letter it is still printing the statements every time - an example of this is in post 6, in bold.
Could you possibly elaborate? I do apologize for taking up so much time, java isnt my strong point! :/
- 05-06-2008, 04:18 AM #12
Member
- Join Date
- Apr 2008
- Posts
- 23
- Rep Power
- 0
Ok, what are your conditions that correspond with correct input. I.E. everything should be lowerCase, uppercase, etc..
- 05-06-2008, 04:25 AM #13
Member
- Join Date
- May 2008
- Posts
- 7
- Rep Power
- 0
WEll, first i need to take the password that was entered and check that password for
* No spaces -isWhiteSpace
* At least one number -isDigit
* At least one uppercase letter -isUpperCase
* At least one lowercase number -isLowerCase
The problem im having at the moment is that the loop is reporting the error statement (i.e there is a space in the password) over every letter in the password
Thanks
Similar Threads
-
compiling trouble
By capacitator in forum CLDC and MIDPReplies: 4Last Post: 06-10-2008, 10:12 PM -
Need another program with and if-else, array, and for loop
By Zebra in forum New To JavaReplies: 2Last Post: 05-05-2008, 01:56 PM -
Compile Trouble
By adelgado0723 in forum New To JavaReplies: 5Last Post: 04-21-2008, 02:02 AM -
trouble with program
By jimJohnson in forum New To JavaReplies: 1Last Post: 04-03-2008, 09:29 AM -
JTree trouble
By Alantie Vala in forum AWT / SwingReplies: 3Last Post: 07-31-2007, 11:12 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks