| | |
For loop in guessing game, utter confusion!
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Jul 2005
Posts: 12
Reputation:
Solved Threads: 0
I've figured out im using the wrong loop for the task at hand but im still wondering why it repeats this exactly 3 times:
for(ch = (char) System.in.read(); ch != ('k' | 'K') ;
ch = (char) System.in.read())
{
System.out.println("sorry, you're incorrect!\n Guess again: ");
}
It prints this in the console if you don't hit k or K:
Sorry, you're incorrect!
Guess again:
Sorry, you're incorrect!
Guess again:
Sorry, you're incorrect!
Guess again:
[] <--represents where you would input next character
I'm just wondering why it's repeating three times, instead of once or four times, or inifinite times.
for(ch = (char) System.in.read(); ch != ('k' | 'K') ;
ch = (char) System.in.read())
{
System.out.println("sorry, you're incorrect!\n Guess again: ");
}
It prints this in the console if you don't hit k or K:
Sorry, you're incorrect!
Guess again:
Sorry, you're incorrect!
Guess again:
Sorry, you're incorrect!
Guess again:
[] <--represents where you would input next character
I'm just wondering why it's repeating three times, instead of once or four times, or inifinite times.
hi
you are reading the line end characters as well, they are \r and \n in windows.
That is the reason why you are getting 3 lines of :
"Sorry, you're incorrect!"
than the expected one line.
As server crash said you can try with buffered reader which will remove line end characters, but readLine() will give you a String rather than char, you may not be able to type cast like:
c = (char)br.readLine();
I would suggest you to use the String and iterate to get the result.
I have modified your code a bit and avoided the line end characters.
[HTML]
import java.io.*;
class TestDani
{
public static void main(String args[]) throws Exception
{
char ch = (char) System.in.read();
while(ch != ('k' | 'K'))
{
if(ch=='\r'||ch=='\n')
{
ch = (char) System.in.read();
continue;
}
System.out.println("sorry, you're incorrect!\n Guess again: ."+ch+".");
ch = (char) System.in.read();
}
}
}
[/HTML]
you are reading the line end characters as well, they are \r and \n in windows.
That is the reason why you are getting 3 lines of :
"Sorry, you're incorrect!"
than the expected one line.
As server crash said you can try with buffered reader which will remove line end characters, but readLine() will give you a String rather than char, you may not be able to type cast like:
c = (char)br.readLine();
I would suggest you to use the String and iterate to get the result.
I have modified your code a bit and avoided the line end characters.
[HTML]
import java.io.*;
class TestDani
{
public static void main(String args[]) throws Exception
{
char ch = (char) System.in.read();
while(ch != ('k' | 'K'))
{
if(ch=='\r'||ch=='\n')
{
ch = (char) System.in.read();
continue;
}
System.out.println("sorry, you're incorrect!\n Guess again: ."+ch+".");
ch = (char) System.in.read();
}
}
}
[/HTML]
We come to love not by finding a perfect person, but by learning to see an imperfect person perfectly.
-Sam Keen, from To Love and Be Loved
-Sam Keen, from To Love and Be Loved
![]() |
Similar Threads
- How do i create a simple game using c++?? (C++)
- Tic-Tac-Toe Game (Visual Basic 4 / 5 / 6)
- 2D guessing game (Python)
- C++ Loop Question Here (C++)
- HELP creating a multiserver game in VB winsock (Visual Basic 4 / 5 / 6)
- Random guessing game (C++)
Other Threads in the Java Forum
- Previous Thread: about CVS protocol and CVS client
- Next Thread: Help with an Array GRading assignment
Views: 2637 | Replies: 2
| Thread Tools | Search this Thread |
Tag cloud for Java
911 addball addressbook android api append apple applet application arguments array arrays automation binary bluetooth button chat class classes client code component css csv database draw eclipse ee error event exception file fractal game givemetehcodez graphics gui helpwithhomework html ide image input integer j2me java javaarraylist javaprojects jmf jni jpanel julia jvm key linux list loan loop map method methods mobile netbeans newbie number object oracle output packets phone print problem program programming project recursion reporting robot scanner screen se server service set size sms socket software sort sql stream string swing test threads time transfer tree ubuntu windows wrong






