[java] Please inform me of this problem!

Started by
1 comment, last by Brackus 22 years, 6 months ago
Hello all, I have to hand in this assingment at 6:00 a.m. and I was just wondering why after you enter a 0-5 the program does what its supposed to, then loops 2 more times without waiting for the user to enter a number or letter... here is the code, it should run for you... import java.io.*; public class Counter2 { public static void main( String[] args ) throws java.io.IOException { int Main = 0; int All = 0; char selection; System.out.println("Welcome to Dustin McCulloch''s Bingo Program!"); System.out.println(" "); do { int i = 0; System.out.println("Press the number 0-5 for the equivalent verse, press a for all, and q to quit"); System.out.flush(); selection = (char) System.in.read(); if (selection == ''a''){ Main = 20; All = 6; } if (selection == ''5''){ Main = 10; } if (selection == ''4''){ Main = 5; } if (selection == ''3''){ Main = 4; } if (selection == ''2''){ Main = 3; } if (selection == ''1''){ Main = 2; } if (selection == ''0''){ Main = 1; } if (selection == ''q''){ Main = 0; } System.out.println(" "); if (selection != ''q''){ System.out.println("Farmer Brown, he had a dog and Bingo was his name. Oh!"); while ( i < 3 ) { i++; if (selection == ''0''){ System.out.println("B "+ "I " +"N "+ "G "+ "O"); } if (selection == ''1''){ System.out.println(" "+ "I "+ "N "+ "G "+ "O"); } if (selection == ''2''){ System.out.println(" "+ " "+ "N "+ "G "+ "O"); } if (selection == ''3''){ System.out.println(" "+ " "+ " "+ "G "+ "O"); } if (selection == ''4''){ System.out.println(" "+ " "+ " "+ " "+ "O"); } if (selection == ''5''){ System.out.println(" "+ " "+ " "+ " "+ " "); } } System.out.println("and Bingo was his name. Oh."); } } while (selection != ''q''); } }
Mess With the Best, go down like the rest, but please visit:http://members.tripod.com/nu_bgameprogramming/
Advertisement

I believe that System.in.read() will only read in a single byte, so you''re getting the 5,
then the carriage return, then the linefeed
(assuming you''re running a WinTel system)

If you want to read just an integer, you need to read in a string, then call Integer.parseInt(s);

Hope this helps
I have heard someone talk about that in class, yes, it makes sense that it also reads the carriage return, where do I put that code you offered???
Mess With the Best, go down like the rest, but please visit:http://members.tripod.com/nu_bgameprogramming/

This topic is closed to new replies.

Advertisement