• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

need help to fix the code!

 
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When I run the following code it throws an java.lang.NumberFormatException and terminates.I check the code very carefully but can not find the wrong place!Can anybody help me?
I thought the character encoding may be the problem! I am in china now and the character
encoding of my platform is GB2312!
#######
import java.io.*;
public class A{
public static void main(String args[]) throws IOException{
String qty="";
int c;
while ((c = System.in.read()) != '\n') {
qty +=(char)c;
}
System.out.print(Integer.parseInt(qty));
}
}
------------------
 
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You could simplify your program by using this code to read input instead:
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
String qty = in.readLine();
 
Ranch Hand
Posts: 1492
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi William,
Just in case you care, the reason your code didn't work is because on Windows the character you are looking for is '\r'. On Windows the carriage return (Enter) key actually generates two characters: \r\n. You were only removing the '\n' and sending in the '\r' character into the parseInt method.
Use Junilu's code because it handles the case for you ...
Regards,
Manfred.
 
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
When you try to compare != , it would promote the wider of two types which in this case is int. Im not sure how '/n' is converted into some interger value . I think this might be the reason. but cant be sure. If someone knows better, please enlighten us.
 
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Clever Manfred! Had to check it out to be sure that you were correct. (Not that I would doubt you ).
Sure enough, - this works
 
william shen
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you all. This really clear me up!
------------------
 
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I input one "1" and press enter.The output is three lines and there is one "1" in per line.
Who can explain why?
[This message has been edited by Niu Xiuyuan (edited June 13, 2001).]
 
william shen
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Niu:
The first 1 is the 1 you input,the second 1 is outputed by the System.out.println(qty) in the loop,the third 1 is outputed by System.out.print(Integer.parseInt(qty)). Wish this help you!

Originally posted by Niu Xiuyuan:
[B]
I input one "1" and press enter.The output is three lines and there is one "1" in per line.
How can explain why? [/B]



------------------
 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
It isn't good style to use hardcoded '\r' or '\n'.
You should call method:
System.getProperty("line.separator")
It returns char sequence that uses on your platform as line separator.
reply
    Bookmark Topic Watch Topic
  • New Topic