• 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
  • Devaka Cooray
  • Tim Cooke
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
Saloon Keepers:
  • Piet Souris
Bartenders:

while loop

 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


I am trying to take input from user as a form of int how could I do that
 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Ranch Hand
Posts: 162
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try this




This will work.
Thanks
 
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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:
 
lowercase baba
Posts: 13091
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think Integer.parseInt will solve the problem.
 
nitin ratra
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
but the code is giving exceptions then
 
Ranch Hand
Posts: 2908
1
Spring Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Which exception ?, in Java 90 % of exceptions are self explanatory !!
 
nitin ratra
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
io exceptions
 
fred rosenberger
lowercase baba
Posts: 13091
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Marshal
Posts: 82459
594
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic