Results 1 to 6 of 6
Thread: why this doesn't work?
- 08-08-2010, 10:18 AM #1
Member
- Join Date
- Aug 2010
- Posts
- 9
- Rep Power
- 0
why this doesn't work?
Hello friends, I am very new to programming. I want to know why this code doesn't work.
Java Code:void linkedListMenu() throws IOException { int c; BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); do { System.out.println("Choose one of the following options:"); System.out.println("\t1.Create the Linked List"); c = br.read(); System.out.println("how" + br.read() + "are you"); } while(c != 1); }
But when i use this form this works:
Java Code:void linkedListMenu() throws IOException { char c; BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); do { System.out.println("Choose one of the following options:"); System.out.println("\t1.Create the Linked List"); c = (char) br.read(); System.out.println("how" + br.read() + "are you"); } while(c != '1'); } }Last edited by Fubarable; 08-08-2010 at 11:53 AM. Reason: Moderator edit: code tags added
- 08-08-2010, 10:30 AM #2
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 7
Please describe "doesn't work", in detail.
-
Moderator edit: italics removed, code tags added for readability. To learn how to do this yourself, please check out the links in my signature.
Much luck!
- 08-08-2010, 07:55 PM #4
You should really specify exactly what isn't working.
However, I believe I see the problem. Read up on BufferedReader.read() and you should be able to figure it out too. BufferedReader (Java 2 Platform SE v1.4.2)
- 08-09-2010, 07:42 AM #5
Member
- Join Date
- Jun 2010
- Posts
- 2
- Rep Power
- 0
When after prompting for the user value and storing it into a variable, e.g
System.out.println("\t1.Create the Linked List");
c = br.read();
This is trying to force a string value into an int variable.
To implicitly cast the value into the variable you would write:
c = Integer.parseInt(br.read());
or explicit by
c = (int) br.read();
- 08-09-2010, 08:07 AM #6
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 7
No. The read method returns an int. (besides "casting" a String to an int, like your second example, wouldn't work any way)
I have a feeling that the OP is complaining that the first time it prints a number rather than a character, and the second time it prints a character. This is perfectly understandable, since he is reading an int the first time a char (character) the second time, but until the OP describes exactly what his "problem" is this conjecturing is useless and probably counter-productive, when not outright wrong, like the previous post.
Similar Threads
-
This code doesn't work. Why? thanks.
By seanzhang in forum New To JavaReplies: 10Last Post: 07-09-2010, 05:53 AM -
JMS work in JBOSS 5??
By kkc85 in forum Enterprise JavaBeans (EJB)Replies: 3Last Post: 01-20-2010, 08:22 PM -
Why doesn't this work?
By Corder10 in forum New To JavaReplies: 1Last Post: 07-04-2009, 10:33 PM -
Int does not initialize, will this work?
By starchildren3317 in forum New To JavaReplies: 2Last Post: 07-09-2008, 10:42 PM -
how would i get this to work...?
By deeadeed in forum New To JavaReplies: 6Last Post: 12-06-2007, 02:58 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks