| Author |
while loop
|
nitin ratra
Greenhorn
Joined: Aug 11, 2008
Posts: 25
|
|
I am trying to take input from user as a form of int how could I do that
|
 |
Seetharaman Venkatasamy
Ranch Hand
Joined: Jan 28, 2008
Posts: 5575
|
|
Originally posted by nitin ratra: int i =br.readint(); [/CODE]
replace with this ---------------------------------------------------- int i =Integer.parseInt(br.readLine()); ---------------------------------------------------- remaining all fine Hope This Helps
|
 |
Kuladip Yadav
Ranch Hand
Joined: Jul 30, 2008
Posts: 162
|
|
Try this This will work. Thanks
|
 |
Paul Sturrock
Bartender
Joined: Apr 14, 2004
Posts: 10336
|
|
BufferedReader doesn't have a readint() method. Here is some code (there are problems with it that I'll leave you to fix), this should get you going in the right direction:
|
JavaRanch FAQ HowToAskQuestionsOnJavaRanch
|
 |
fred rosenberger
lowercase baba
Bartender
Joined: Oct 02, 2003
Posts: 9939
|
|
It helps people help you if you tell us what this code is doing wrong - does it compile, does it throw an exception, does it run but not give the expected output... The easier you make it for someone to help you, the more likely you'll get the help you need.
|
Never ascribe to malice that which can be adequately explained by stupidity.
|
 |
Veera Sundar
Greenhorn
Joined: Jun 08, 2007
Posts: 25
|
|
|
I think Integer.parseInt will solve the problem.
|
Thanks,<br />Veera |<a href="http://veerasundar.com/blog" target="_blank" rel="nofollow">Blog</a>
|
 |
nitin ratra
Greenhorn
Joined: Aug 11, 2008
Posts: 25
|
|
|
but the code is giving exceptions then
|
 |
Sagar Rohankar
Ranch Hand
Joined: Feb 19, 2008
Posts: 2896
|
|
|
Which exception ?, in Java 90 % of exceptions are self explanatory !!
|
[LEARNING bLOG] | [Freelance Web Designer] | [and "Rohan" is part of my surname]
|
 |
nitin ratra
Greenhorn
Joined: Aug 11, 2008
Posts: 25
|
|
|
io exceptions
|
 |
fred rosenberger
lowercase baba
Bartender
Joined: Oct 02, 2003
Posts: 9939
|
|
Please read my above post. The more information you can give, the easier it is for folks to help you. You could post the EXACT and COMPLETE text of the exception. Not only does it tell you what the exception is, but what line it's on. That alone is a BIG CLUE in solving the problem. If you don't post it, the only real way to help you is for someone to copy your code, save it, compile it, then run it. most folks don't have that kind of time, so they'll just move on to some other post. Further,you should post your current code. Nobody knows what exact changes you made, so nobody has any idea what, where, or why your code is now throwing an exception. People here love to help, but you have to make it as easy for them as possible. [ August 19, 2008: Message edited by: fred rosenberger ]
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32599
|
|
Three likely solutions to problems about IOException. But you are almost certainly not getting an IOException, you are getting a compiler error about IOException not being handled. Solution 1: Difficult and good practice. You are obliged by the fact that java.io.IOException is a "checked" exception to handle it before the compiler will let you compile. You want this code to read anything:The printStackTrace() call is pretty basic, but it will give you the information you need if an Exception really occurs. This is full-blown Exception handling and guarantees to close the BufferedReader whatever happens. Note the way I have written it, you can get away without declaring the InputStreamReader. Solution 2: Cheap and cheerful, easy, but not robust. Add "throws java.io.IOException" after the () at the end of the method signature and before the first { This tells the compiler you are willing to let the Exception propagate. If you have several methods calling each other, you need the "throws" declaration for each. Solution 3: Easy and complete. Forget about Readers, find the java.util.Scanner class which has a nextInt() method and use that to read from the keyboard. Just pass System.in to the Scanner constructor. Scanner was introduced in Java5, so it won't work for Java1.4 or earlier.
|
 |
ibrahim uzan
Greenhorn
Joined: Sep 17, 2008
Posts: 2
|
|
InputStreamReader inp = new InputStreamReader(System.in); BufferedReader br = new BufferedReader(inp); String i = br.readLine(); while (Integer.parseInt(i) < 25) { System.out.println(i); i = Integer.parseInt(i) * 3+""; } Integer.parseInt is enough..Do not forget throws declaration.. [ September 18, 2008: Message edited by: ibrahim uzan ]
|
 |
 |
|
|
subject: while loop
|
|
|