The Artima Developer Community
Sponsored Link

Java Answers Forum
BufferedReader problems

4 replies on 1 page. Most recent reply: Dec 7, 2002 8:30 AM by Thomas SMETS

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 4 replies on 1 page
Joe Molley

Posts: 3
Nickname: beef
Registered: Dec, 2002

BufferedReader problems Posted: Dec 5, 2002 1:41 PM
Reply to this message Reply
Advertisement
Ok, now i got a little problem here. It seems to me like it should be really simple, but i cant figure it out.
Heres the code:

System.out.println("Enter an integer:");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
try{num = br.read();} catch(IOException ioe){System.exit(1);}
System.out.println(num);

Now, everytime i enter an integer, whether 1 or a billion, it prints it out as "49" everytime. What is going on?


Joe Molley

Posts: 3
Nickname: beef
Registered: Dec, 2002

Re: BufferedReader problems Posted: Dec 5, 2002 1:48 PM
Reply to this message Reply
oops sorry about the java formatting things, i spaced it, here's it again with more in case anyone cares.
import java.io.*;
class foobar
{
int num;
 
public static void main (String arguments[])
{
foobar fb = new foobar();
fb.enterFoobar();
}
 
void enterFoobar()
{
System.out.println("Enter an integer:");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
try{num = br.read();}	catch(IOException ioe){System.exit(1);}
System.out.println(num);
}}

Venugopal Bandari

Posts: 5
Nickname: bandari
Registered: Dec, 2002

Re: BufferedReader problems Posted: Dec 6, 2002 1:39 AM
Reply to this message Reply
hai,

System.out.println("Enter an integer:");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
num = br.read();

here br.read returns byte. so, a byte can read -126 to 127 values. u enter any value at system.in, u r trying to read only first byte in reader object. try to read whole line, then make it what u want. if u need integer then this may help u.

import java.io.*;
class foobar
{
int num;
public static void main (String arguments[]){
foobar fb = new foobar();
fb.enterFoobar();
}
void enterFoobar(){
System.out.println("Enter an integer:");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
try{
String line = br.readLine();
num = Integer.parseInt(line);
} catch(Exception e){
System.exit(1);
}System.out.println(num);
}
}

Joe Molley

Posts: 3
Nickname: beef
Registered: Dec, 2002

Re: BufferedReader problems Posted: Dec 6, 2002 1:21 PM
Reply to this message Reply
neat cool sweet. thanks! that works fine

Thomas SMETS

Posts: 307
Nickname: tsmets
Registered: Apr, 2002

Re: BufferedReader problems Posted: Dec 7, 2002 8:30 AM
Reply to this message Reply
Just on little remark (which has little to do with question). Make sure you define the variables outside the the scope of a loop (like shown in the code herebelow).

I am spending hours looking at code for code reviews.
what I mention is not a code improvement & should be picked up by an Compiler doing simple optimization.

It's however a bad practice in my mind, as it's a proove that the people don't realize what is (really) going on !
import java.io.*;
 
class foobar
{
  int num;
  public static void main (String arguments[])
  {
    foobar fb = new foobar();
    fb.enterFoobar();
  }  // End of main
 
  void enterFoobar ()
  {
    System.out.println("Enter an integer:");
    BufferedReader br 
      = new BufferedReader(new InputStreamReader(System.in));
    try
    {
      String line = null;
      int num = -1;
      do 
      {
        line = br.readLine();
        num = Integer.parseInt(line);
        System.out.println(num); 
      } while (num != -1)
    } catch(Exception e)
    {
      e.printStackTrace ();
      System.exit(1);
    }
      }      // End of enterFoobar
}        // end of Class

Rgds,

Thomas SMETS,
SCJP2 - Brussels

p.s. : As there was not loop, Venugopal 's code was dead correct ( & efficient ).

Flat View: This topic has 4 replies on 1 page
Topic: Servlets Previous Topic   Next Topic Topic: Iplanet Application Server

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use