Page 1 of 2 12 LastLast
Results 1 to 10 of 15

Thread: java looping problem?

  1. #1
    Senior Member
    Join Date
    Dec 2003
    Posts
    317

    java looping problem?

    whenever i try to do a loop in java, it does this wierd thing where it repeats the printed lines multiple times. this is a problem for me because everytime my instructor asks us to make a program that uses a loop(most every program we make now) he sees this and says it's a probem. i asked him how to fix it but he either doesnt know or expects us to figure it out on our own(everybody else is having the same problem). here's an example:
    Code:
    import java.io.*;
    class password {
    	public static void main (String args[])
    	throws java.io.IOException {
    		
    		char input, passwd='y';
    		
    		do {
    		System.out.print("Enter your password: ");
    		input=(char)System.in.read();
    	    if(input!=passwd) System.out.print("wrong password");
    		}
    		while(input!=passwd);
    		
    	System.out.println("you got it");
    	}
    	}
    will output this if you enter the wrong password
    wrong passwordEnter your password: wrong passwordEnter your password: wrong passwordEnter your password:
    and then the user can input another letter

    what it should do is just say "wrong password" and then wait for input again... anybody know what the problem is?

  2. #2
    Antionline Herpetologist
    Join Date
    Aug 2001
    Posts
    1,165
    Try this code instead. System.in.read() is a strange beast that should not be used. If you need to accept console input, use a BufferedReader instead.

    Code:
    import java.io.*;
    class password 
    {
    	public static void main (String args[])	throws java.io.IOException 
    	{
    		
    		char input, passwd='y';
    
    		do 
    		{
    			System.out.print("Enter your password: ");
    			input=(char)new BufferedReader(new InputStreamReader(System.in)).read(); 
    	    		if(input!=passwd) System.out.println("wrong password");	
    			
    		}
    		while(input!=passwd);
    		
    	System.out.println("you got it");
    	}
    }
    EDIT: In case you didn't understand the code, it can be broken up into this:
    Code:
    import java.io.*;
    class password 
    {
    	public static void main (String args[])	throws java.io.IOException 
    	{
    		
    		char input, passwd='y';
    		InputStreamReader isr = new InputStreamReader(System.in);
    		BufferedReader buf = new BufferedReader(isr);
    		do 
    		{
    			System.out.print("Enter your password: ");
    			input=(char)buf.read(); 
    	    		if(input!=passwd) System.out.println("wrong password");	
    			
    		}
    		while(input!=passwd);
    		
    	System.out.println("you got it");
    	}
    }
    Cheers,
    cgkanchi
    Buy the Snakes of India book, support research and education (sorry the website has been discontinued)
    My blog: http://biology000.blogspot.com

  3. #3
    Senior Member
    Join Date
    Dec 2003
    Posts
    317
    ill try it out, thnx

  4. #4
    Senior Member
    Join Date
    Dec 2003
    Posts
    317
    i just compiled and ran urs and it does the same thing:
    bash-2.05b# java password
    Enter your password: a
    wrong password
    Enter your password: wrong password
    Enter your password:
    it seems like the more characters i input the more screwy responses i get before it asks for input again.
    "aaaa" gets me:
    bash-2.05b# java password
    Enter your password: aaaa
    wrong password
    Enter your password: wrong password
    Enter your password: wrong password
    Enter your password: wrong password
    Enter your password: wrong password
    Enter your password:

  5. #5
    Banned
    Join Date
    Sep 2004
    Posts
    305
    I compiled his and it works fine:

    Code:
    Enter your password: aaaa
    wrong password
    Enter your password: aaaa
    wrong password
    Enter your password: aaaa
    wrong password
    Enter your password: d
    wrong password
    Enter your password: d
    wrong password
    Enter your password: ssads
    awrong password
    Enter your password: das
    wrong password
    Enter your password: y
    you got it

  6. #6
    Senior Member
    Join Date
    Dec 2003
    Posts
    317
    oops, i must have swiped the wrong code when i copy and pasted that sry bout that

    edit
    wierd, the first code you wrote worked, but the second one still messes up. they're supposed to be the same right?

  7. #7
    Senior Member
    Join Date
    Dec 2003
    Posts
    317
    hey, what if i wanted to have a password thats more than just one char? how would i go about that?

  8. #8
    Banned
    Join Date
    Sep 2004
    Posts
    305
    Haven't tested this, just edited it quickly... should work:

    Code:
    import java.io.*;
    class password 
    {
    	public static void main (String args[])	throws java.io.IOException 
    	{
    		
    		String input, passwd="whateveryouwant";
    		InputStreamReader isr = new InputStreamReader(System.in);
    		BufferedReader buf = new BufferedReader(isr);
    		do 
    		{
    			System.out.print("Enter your password: ");
    			input=(String)buf.read(); 
    	    		if(!input.equals(passwd)) System.out.println("wrong password");	
    			
    		}
    		while(!input.equals(passwd));
    		
    	System.out.println("you got it");
    	}
    }

  9. #9
    Antionline Herpetologist
    Join Date
    Aug 2001
    Posts
    1,165
    ;TT :
    It should be buf.readLine() for String input. BufferedReader.read() only works for int's and char's.

    Phonedog911:
    Strange. I tried it out and get the same thing. Both are supposed to be equivalent. Looks interesting... I'll play a bit and get back to you.

    Cheers,
    cgkanchi
    Buy the Snakes of India book, support research and education (sorry the website has been discontinued)
    My blog: http://biology000.blogspot.com

  10. #10
    Senior Member
    Join Date
    Jul 2003
    Posts
    813
    I'm gonna have a try at it, see how this works

    Code:
    import java.util.Scanner;
    
    public class Password 
    {
    	public static void main (String args[])
    	{
    		
    		Scanner kbd = new Scanner(System.in);
    		
    		String passwd= new String("whateveryouwant");
    		String input;
    		
    		do 
    		{
    			System.out.print("Enter your password: ");
    			input=kbd.nextLine();
    	    		if(!input.equals(passwd)) System.out.println("wrong password");	
    			
    		}
    		while(!input.equals(passwd));
    		
    	System.out.println("you got it");
    	}
    }
    [/B][/QUOTE]

    Compiled and works perfectly, but you need Java 1.5.0 [there's a stable release now] to use this.
    /\\

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •