Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 05-05-2008, 10:43 PM
Member
 
Join Date: May 2008
Posts: 7
dablyz is on a distinguished road
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
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 05-05-2008, 11:51 PM
Member
 
Join Date: Apr 2008
Posts: 23
theonly is on a distinguished road
I''m not sure without looking at the easyIn class, but is Character defined somewhere.

Code:
boolean b_space = Character.isWhitespace(c);
if not that's where your problem is.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 05-06-2008, 12:46 AM
Member
 
Join Date: May 2008
Posts: 7
dablyz is on a distinguished road
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!
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 05-06-2008, 01:25 AM
Member
 
Join Date: Apr 2008
Posts: 23
theonly is on a distinguished road
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.

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");
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.

Last edited by theonly : 05-06-2008 at 01:59 AM.
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 05-06-2008, 02:03 AM
Member
 
Join Date: May 2008
Posts: 7
dablyz is on a distinguished road
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
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 05-06-2008, 02:12 AM
Member
 
Join Date: May 2008
Posts: 7
dablyz is on a distinguished road
// 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)
Bookmark Post in Technorati
Reply With Quote
  #7 (permalink)  
Old 05-06-2008, 02:18 AM
Member
 
Join Date: Apr 2008
Posts: 23
theonly is on a distinguished road
Ok, so are you require to print out every letter in the password or just to check everyone without printing them.
Bookmark Post in Technorati
Reply With Quote
  #8 (permalink)  
Old 05-06-2008, 02:25 AM
Zosden's Avatar
Senior Member
 
Join Date: Apr 2008
Posts: 353
Zosden is on a distinguished road
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
Bookmark Post in Technorati
Reply With Quote
  #9 (permalink)  
Old 05-06-2008, 02:43 AM
Member
 
Join Date: May 2008
Posts: 7
dablyz is on a distinguished road
@ 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!
Bookmark Post in Technorati
Reply With Quote
  #10 (permalink)  
Old 05-06-2008, 03:26 AM
Member
 
Join Date: Apr 2008
Posts: 23
theonly is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
  #11 (permalink)  
Old 05-06-2008, 03:36 AM
Member
 
Join Date: May 2008
Posts: 7
dablyz is on a distinguished road
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! :/
Bookmark Post in Technorati
Reply With Quote
  #12 (permalink)  
Old 05-06-2008, 05:18 AM
Member
 
Join Date: Apr 2008
Posts: 23
theonly is on a distinguished road
Ok, what are your conditions that correspond with correct input. I.E. everything should be lowerCase, uppercase, etc..
Bookmark Post in Technorati
Reply With Quote
  #13 (permalink)  
Old 05-06-2008, 05:25 AM
Member
 
Join Date: May 2008
Posts: 7
dablyz is on a distinguished road
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
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
compiling trouble capacitator CLDC and MIDP 4 06-10-2008 11:12 PM
Need another program with and if-else, array, and for loop Zebra New To Java 2 05-05-2008 02:56 PM
Compile Trouble adelgado0723 New To Java 5 04-21-2008 03:02 AM
trouble with program jimJohnson New To Java 1 04-03-2008 10:29 AM
JTree trouble Alantie Vala AWT / Swing 3 08-01-2007 12:12 AM


All times are GMT +3. The time now is 12:29 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org