Results 1 to 8 of 8
  1. #1
    cruxblack is offline Senior Member
    Join Date
    Jul 2007
    Posts
    130
    Rep Power
    0

    Default An unexpected jumper bug on my IO code?

    This is the code
    Java 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
    Java 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
    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
    Java Code:
    //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?: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

  2. #2
    levent Guest

    Default

    Why don't you read the chraacter as usual:

    Java Code:
    holder = input.readLine();
    and then convert to character?

  3. #3
    cruxblack is offline Senior Member
    Join Date
    Jul 2007
    Posts
    130
    Rep Power
    0

    Default

    Already tried that
    Java Code:
    holder = input.readLine();
    chara = (char)holder;
    it says inconvertible
    Java Code:
    testbuff.java:25: inconvertible types
    found   : java.lang.String
    required: char
                            chara = (char)holder;
                                          ^
    The String wont convert to char :(

  4. #4
    levent Guest

    Default

    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);

  5. #5
    cruxblack is offline Senior Member
    Join Date
    Jul 2007
    Posts
    130
    Rep Power
    0

    Default

    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:

  6. #6
    cruxblack is offline Senior Member
    Join Date
    Jul 2007
    Posts
    130
    Rep Power
    0

    Default

    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

  7. #7
    brianhks is offline Senior Member
    Join Date
    Jul 2007
    Posts
    135
    Rep Power
    0

    Default

    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.

  8. #8
    cruxblack is offline Senior Member
    Join Date
    Jul 2007
    Posts
    130
    Rep Power
    0

    Default

    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

  1. Error: unexpected type
    By silvia in forum New To Java
    Replies: 3
    Last Post: 02-05-2010, 09:54 PM
  2. I need help fixing my code.. or non code?
    By MrHuggykins in forum New To Java
    Replies: 1
    Last Post: 03-19-2008, 10:12 PM
  3. code help
    By chitwood in forum New To Java
    Replies: 0
    Last Post: 02-10-2008, 12:08 AM
  4. help with oop code
    By nhlfan in forum New To Java
    Replies: 1
    Last Post: 11-27-2007, 08:21 PM
  5. Replies: 1
    Last Post: 04-26-2007, 03:52 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •