How to exit a while loop with Enter
807588Apr 12 2009 — edited Apr 12 2009I need to write an app which combines chars into a single string, using a while loop, and the loop exits when Enter is pressed. However, my code doesn't work. I also need to use Integer.parseInt(string) to convert the string into integer in the end. Here's the code. Please not it doesn't work in different ways in JCreator and netbeans.
class TBs {
public static void main (String args[]) throws Exception {
char a='1';
String str = "";
System.out.println ("Type: " );
System.out.println();
do {
a = (char) System.in.read();
str+=a;
}
while (a != '\r');
System.out.println (str);
int i = Integer.parseInt(str);
System.out.println (i);
}
}