This is the 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
}
}
}
And this is the output it gave
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
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 number
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
//input.readLine(); //if i turn this line on, the unexpected jump wont occur
But honestly, i don't understand why it acts that way
Can anyone explain to me why does the code act that way, im bit 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