-
October 15th, 2004, 06:50 PM
#1
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?
-
October 15th, 2004, 08:00 PM
#2
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
-
October 15th, 2004, 09:49 PM
#3
-
October 15th, 2004, 10:26 PM
#4
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:
-
October 15th, 2004, 11:40 PM
#5
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
-
October 16th, 2004, 12:24 AM
#6
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?
-
October 16th, 2004, 12:27 AM
#7
hey, what if i wanted to have a password thats more than just one char? how would i go about that?
-
October 16th, 2004, 01:01 AM
#8
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");
}
}
-
October 16th, 2004, 05:20 AM
#9
;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
-
October 16th, 2004, 11:30 AM
#10
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
-
Forum Rules
|
|