Results 1 to 8 of 8
- 07-26-2007, 04:05 PM #1
Senior Member
- Join Date
- Jul 2007
- Posts
- 130
- Rep Power
- 0
An unexpected jumper bug on my IO code?
This is the code
And this is the output it gaveJava Code://class testbuff.java import java.io.*; public class testbuff { static BufferedReader input = new BufferedReader(new InputStreamReader(System.in)); public static void main(String[] args)throws IOException { String holder,str; char chara; int num,counter=0; while(counter<2) { System.out.print("Enter a word : "); str = input.readLine(); System.out.println("U write \""+str+"\""); System.out.print("Enter a number : "); holder = input.readLine(); num = Integer.parseInt(holder); System.out.println("The number u enter are " +num +" plus 1 is "+(num+1)); System.out.print("Enter a letter : "); chara = (char)System.in.read(); System.out.println("U write a letter "+chara); counter++; System.out.println(); //input.readLine(); //if i turn this line on, the unexpected jump wont occur } } }
Something doesn't work right the first time it loops, u can see that at the beginning of the loop, it didn't ask for an input for word, instead, it ignore the 1st input and went straight to the 2nd input, which was the one that request a numberJava Code:Enter a word : Word //asking for the 1st input U write "Word" Enter a number : 85 //requesting the 2nd input The number u enter are 85 plus 1 is 86 Enter a letter : P //3rd input U write a letter P Enter a word : U write "" //passed through the 1st input Enter a number : //went straight asking for the 2nd input
So far, my assumption was that the jump was caused when i convert the output from the System.in.read() method into a char type
I don't know why, but the letter input P that i enter, kinda turned into 2 chars it seems, a char of 'F' and a <null>,
causing the 'F' to be processed by the 3rd input, and the <null> to somehow be received as the 1st input of the next loop before i even type anything
The jump only stop after i type this on the end of the loop
But honestly, i don't understand why it acts that wayJava Code://input.readLine(); //if i turn this line on, the unexpected jump wont occur
Can anyone explain to me why does the code act that way, im bit confused?:confused:
Is there any other way or method to take the input in my code so the converted 3rd input wont jump?
This may sound real newbie, and this is quite a long post, sorry for that, but ill be glad if some1 could help, thank u :(
CruxBlack
- 07-27-2007, 11:11 PM #2levent Guest
Why don't you read the chraacter as usual:
and then convert to character?Java Code:holder = input.readLine();
- 07-28-2007, 04:52 AM #3
Senior Member
- Join Date
- Jul 2007
- Posts
- 130
- Rep Power
- 0
Already tried that
it says inconvertibleJava Code:holder = input.readLine(); chara = (char)holder;
The String wont convert to char :(Java Code:testbuff.java:25: inconvertible types found : java.lang.String required: char chara = (char)holder; ^
- 07-28-2007, 07:25 PM #4levent Guest
You can not convert String to char since a sring may contain more than one character. You will need to extract the character at a specified position. Use charAt(0) method like this:
Java Code:chara = holder.charAt(0);
- 07-29-2007, 02:35 AM #5
Senior Member
- Join Date
- Jul 2007
- Posts
- 130
- Rep Power
- 0
Ow :(
I'll try it right away
I thought the C++ and VB style converting would work, u know, in those languages they would get the 1st char from a string and dump the rest of the String if we convert it that way
Thank u Mr.levent, sorry for the trouble :(
Btw, did u found out why the int to char converting i did in my code results in a char and a null?:confused:
- 07-29-2007, 02:57 AM #6
Senior Member
- Join Date
- Jul 2007
- Posts
- 130
- Rep Power
- 0
Yup, the charAt() works Mr.levent :D
I still haven't found out why the int ==> char converting aren't working though :(
Btw, just a side track, is there any way to do a keyPress function like in VB?
That when we press a key, it will automatically execute the key without us having to press enter?
Thank u :D
- 07-29-2007, 05:55 AM #7
Senior Member
- Join Date
- Jul 2007
- Posts
- 135
- Rep Power
- 0
Here is why your reading a char from the command line did weird things.
The shell does not pass the input to the Java program until the enter is pressed. This way you can type something in and then modify it all you want and the Java program does not know about it until enter is pressed. Java does not get intimate with the shell like native programs can. I think the reason for this is that Java must work with shells from all different platforms and they do not all have the same features.
So back to why your program does weird things. In order to send Java the 'P' you must press enter. So the shell sends the standard input of the Java program the following string "P\n" (that is two characters). Because your System.in.read is only interested in one character the other is left in the buffer. Now the next time you call input.readLine() it pulls off the '\n' and returns. This is why it seems to skip the next call.
I hope that makes sense.
- 07-29-2007, 08:24 AM #8
Senior Member
- Join Date
- Jul 2007
- Posts
- 130
- Rep Power
- 0
Yup, i understand that it returns 2 chars thats why it went jumpy, i just didn't think that the 'Enter' itself were gonna be processed as an input
Just check the code, and yes, it did return 'Enter' as a char, the ASCII char number 13
I thought the C++ reading style would work, but i guess im wrong, gotta learn better :p
Thanks for helping me eliminating the grasshopper brianhks and Mr. levent, reps for both of u :D
Similar Threads
-
Error: unexpected type
By silvia in forum New To JavaReplies: 3Last Post: 02-05-2010, 09:54 PM -
I need help fixing my code.. or non code?
By MrHuggykins in forum New To JavaReplies: 1Last Post: 03-19-2008, 10:12 PM -
code help
By chitwood in forum New To JavaReplies: 0Last Post: 02-10-2008, 12:08 AM -
help with oop code
By nhlfan in forum New To JavaReplies: 1Last Post: 11-27-2007, 08:21 PM -
Generating Code Automatically Using Custom code Template In Eclipse
By JavaForums in forum EclipseReplies: 1Last Post: 04-26-2007, 03:52 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks