Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

How to exit a while loop with Enter

807588Apr 12 2009 — edited Apr 12 2009
I 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);

}
}
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on May 10 2009
Added on Apr 12 2009
26 comments
2,005 views