Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 07-26-2007, 06:05 PM
Senior Member
 
Join Date: Jul 2007
Posts: 130
cruxblack will become famous soon enough
An unexpected jumper bug on my IO code?
This is the code
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
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
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?
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
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 07-28-2007, 01:11 AM
Senior Member
 
Join Date: Dec 2006
Posts: 748
levent is on a distinguished road
Why don't you read the chraacter as usual:

Code:
holder = input.readLine();
and then convert to character?
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 07-28-2007, 06:52 AM
Senior Member
 
Join Date: Jul 2007
Posts: 130
cruxblack will become famous soon enough
Already tried that
Code:
holder = input.readLine(); chara = (char)holder;
it says inconvertible
Code:
testbuff.java:25: inconvertible types found : java.lang.String required: char chara = (char)holder; ^
The String wont convert to char
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 07-28-2007, 09:25 PM
Senior Member
 
Join Date: Dec 2006
Posts: 748
levent is on a distinguished road
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:

Code:
chara = holder.charAt(0);
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 07-29-2007, 04:35 AM
Senior Member
 
Join Date: Jul 2007
Posts: 130
cruxblack will become famous soon enough
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?
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 07-29-2007, 04:57 AM
Senior Member
 
Join Date: Jul 2007
Posts: 130
cruxblack will become famous soon enough
Yup, the charAt() works Mr.levent

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
Bookmark Post in Technorati
Reply With Quote
  #7 (permalink)  
Old 07-29-2007, 07:55 AM
Senior Member
 
Join Date: Jul 2007
Posts: 134
brianhks will become famous soon enough
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.
Bookmark Post in Technorati
Reply With Quote
  #8 (permalink)  
Old 07-29-2007, 10:24 AM
Senior Member
 
Join Date: Jul 2007
Posts: 130
cruxblack will become famous soon enough
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

Thanks for helping me eliminating the grasshopper brianhks and Mr. levent, reps for both of u
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
I need help fixing my code.. or non code? MrHuggykins New To Java 1 03-20-2008 12:12 AM
code help chitwood New To Java 0 02-10-2008 02:08 AM
help with oop code nhlfan New To Java 1 11-27-2007 10:21 PM
Error: unexpected type silvia New To Java 1 08-07-2007 07:41 AM
Generating Code Automatically Using Custom code Template In Eclipse JavaForums Eclipse 1 04-26-2007 05:52 PM


All times are GMT +3. The time now is 10:59 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org