Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Java issue

Options
  • 17-06-2011 12:56pm
    #1
    Registered Users Posts: 483 ✭✭


    Hey guys,
    Just getting into development again recently and was doing some simple examples. I have an examply below.
    Can anyone tell me why "parseInt" keeps showing up as an error in the code?
    Thanks


    package rev;


    import java.io.*;
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;


    public class KeyboardInput {


    public static void main(String args[])
    {
    int radius = 0;

    System.out.println("Please enter the radius of a circle");
    // DataInputStream in = new DataInputStream(System.in);
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

    radius = Integer.parseInt(br.readLine());

    double area = Math.PI * radius * radius;

    System.out.println("Area of a circle is " + area);
    }


    }


Comments

  • Registered Users Posts: 1,375 ✭✭✭DoesNotCompute


    what error are you getting?

    Also, does readLine() return a String? Is parseInt() expecting a String, or an int as a parameter?


  • Registered Users Posts: 428 ✭✭Joneser


    I don't have access to an IDE atm but maybe you could try surrounding the readLine with a try catch statement where you are catching an IOException.

    eg:
    try{
         radius = Integer.parseInt(br.readLine());
    }
    catch(IOException e){
         System.err.println("The error is: " + e);
    }
    
    


  • Registered Users Posts: 2,089 ✭✭✭henryporter


    or add a throws declaration after the public static void main(String[] args)

    throws NumberFormatException, IOException


  • Registered Users Posts: 15,065 ✭✭✭✭Malice


    I don't do Java so I could be way off but according to here, Integer.ParseInt() takes a string parameter and throws a NumberFormatException if it can't parse the input. My guess is that you're inputting something like "2.5" and it's falling over. Do as Jonser suggested but replace IOException with NumberFormatException and you'll get the exact error message anyway.


  • Registered Users Posts: 483 ✭✭BornIn84


    what im saying is parseInt is coming up with only thing underlined in red as an error. Take this example with exceptions in it.
    "parseInt" is still giving an error


    package rev;
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;

    public class Rectangle {

    public static void main(String[] args) {

    int width = 0;
    int length = 0;

    try
    {
    //read the length from console
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

    System.out.println("Please enter length of a rectangle");
    length = Integer.parseInt(br.readLine());

    //read the width from console
    System.out.println("Please enter width of a rectangle");
    width = Integer.parseInt(br.readLine());


    }
    //if invalid value was entered
    catch(NumberFormatException ne)
    {
    System.out.println("Invalid value" + ne);
    System.exit(0);
    }
    catch(IOException ioe)
    {
    System.out.println("IO Error :" + ioe);
    System.exit(0);
    }

    /*
    * Area of a rectangle is
    * length * width
    */

    int area = length * width;

    System.out.println("Area of a rectangle is " + area);
    }

    }


  • Advertisement
  • Registered Users Posts: 2,800 ✭✭✭voxpop


    Dunno what you are doing but threw your code into eclipse there and it compiled and ran fine


  • Registered Users Posts: 483 ✭✭BornIn84


    i use eclipse too but again still giving me an error...am i missing .jars or anything do you think?


  • Registered Users Posts: 2,800 ✭✭✭voxpop


    in eclipse, hover you mouse pointer over parseInt - it will tell you why its giving the error in a yellow popup box


  • Registered Users Posts: 5,015 ✭✭✭Ludo


    What is the error? If it is underlined in red, there should be some way of seeing the exact error.


  • Registered Users Posts: 483 ✭✭BornIn84


    sorry my bad.have it working now. i think one of the other classes must be causing a problem in that package.


  • Advertisement
  • Registered Users Posts: 2,800 ✭✭✭voxpop


    You havnt created you own class called Integer or anything have you :pac:


  • Registered Users Posts: 558 ✭✭✭wobbles-grogan


    Is it not because your not waiting for input? I.e your calling read line but theres nothing in the buffer so It returns null...

    Try putting the readline call into a while loop, saving the readline to a string, exiting the loop once string is NOT equal to null, then do your calculation on the string ...

    I may be way off there...


Advertisement