For loop in guessing game, utter confusion!

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Jul 2005
Posts: 12
Reputation: asatrujesus is an unknown quantity at this point 
Solved Threads: 0
asatrujesus asatrujesus is offline Offline
Newbie Poster

For loop in guessing game, utter confusion!

 
0
  #1
Jul 25th, 2005
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.
Reply With Quote Quick reply to this message  
Join Date: Jun 2004
Posts: 2,108
Reputation: server_crash is on a distinguished road 
Solved Threads: 18
server_crash server_crash is offline Offline
Postaholic

Re: For loop in guessing game, utter confusion!

 
0
  #2
Jul 25th, 2005
It's your structure. You need a while loop:

BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
char c;
do
{
c = (char)br.readLine();
}
while (c != 'e');



Something like that.
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 45
Reputation: cheenu78 is an unknown quantity at this point 
Solved Threads: 0
cheenu78's Avatar
cheenu78 cheenu78 is offline Offline
Light Poster

Re: For loop in guessing game, utter confusion!

 
0
  #3
Jul 26th, 2005
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]
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
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 2637 | Replies: 2
Thread Tools Search this Thread



Tag cloud for Java
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC