0

I've just started with Java and so I'm doing plenty of mistakes, I've solved and understood many of them, but now I'm having problems with input. I saw that there are two kinds of input: InputBufferedReader and Scanner, but I read that the first is better (I don't know why yet), so I' m using that.

The error that I get is: IOException

Code

import java.io.InputStreamReader;
import java.io.BufferedReader;

public class Cerchio{

  private float r;
  private float area;
  private float cfr;
  final double pi = 3.14;


  public static void main(String[] args){
    System.out.println("Programma cerchio\n");

    Cerchio cerchio = new Cerchio(); 
    cerchio.getR();
    cerchio.c_cfr();
    cerchio.c_area();
    System.out.println("La circonferenza è: " + cerchio.cfr);
    System.out.println("L area è: " + cerchio.area);
  }

  private float getR(){
    InputStreamReader input = new InputStreamReader(System.in);
    BufferedReader num = new BufferedReader(input);

    try{
      String sr = num.readLine();
      r = Float.valueOf(sr).floatValue();
    } catch(NumberFormatException nfe){
        System.out.println("Incorrect!!!");
      }
    return r;
  }

  private float c_cfr(){
    cfr =(float)(2 * pi * r); //casting
    return cfr;
  }

  private float c_area(){
    area = (float)(pi * (r*r));
    return area;
 }

}

Probably there are other mistackes, but I don't know how to go over this one.

What's wrong? Thank you!

PS: I read this but I didn't understand why and how that works

Community
  • 1
  • 1
Mitro
  • 1,230
  • 8
  • 32
  • 61
  • 1
    Berry120 is indeed correct, however on a matter of style; when you catch an exception you just print "incorrect" but then the program continues on its merry way; is this desired behaviour or is the program unable to continue or is it a fatal problem? When catching an exception you should always either fix the problem or throw a runtime exception to end the program, otherwise you can get strange bugs, the only clue to which is a little "incorrect" written somewhere amongst everything else the program has written – Richard Tingle Oct 24 '13 at 12:37
  • 2
    Or to summarise, this is the order of desirability for programs; (1) it works correctly, (2) it crashes to a runtime exception at the point of the problem, (3) it runs but gives the wrong answer or crashes at a confusing point. Avoid programs that come under 3 at all costs – Richard Tingle Oct 24 '13 at 12:39

3 Answers3

5

Alessio, In Java, you have 2 types of Exceptions :

  • Exception: they are checked
  • Runtime exceptions: not checked

When a method can trigger a "checked" exception, you have to handle it.

To do so you have 2 ways:

  • add a "throws IOException" to the end of your method's signature (which is what the link you posted is suggesting)
  • handle your exception with a try catch block.

In your case, IOException is a "checked" exception and thus you should add a "throws IOException" to your getR method or handle it with a try catch.

PS: If you are learning java, it would be better to use an IDE such as eclipse, you will understand the mistakes easier because it will point you to it with some text higlighting and it will provide you with solutions.

Adel Boutros
  • 10,205
  • 7
  • 55
  • 89
2

You're already catching a NumberFormatException, but you also need to catch the IOException round that statement (or throw it from that method, and catch it somewhere else.)

try{
    String sr = num.readLine();
    r = Float.valueOf(sr).floatValue();
} 
catch(NumberFormatException nfe){
    System.out.println("Incorrect!!!");
}
catch(IOException ex) {
    ex.printStackTrace();
}

If you're using Java 7 (or above) you could combine both exceptions in the same block if you wish:

catch(NumberFormatException|IOException ex){
    ex.printStackTrace();
}
Michael Berry
  • 70,193
  • 21
  • 157
  • 216
  • 1
    Don't you think that if he understood what the problem was, he would have not needed to come here and ask it. What you provided was the solution not "why" (He could be left wondering `why should i use catch or throws?`) – Adel Boutros Oct 24 '13 at 12:39
  • What ex.printStackTrace(); does? – Mitro Oct 24 '13 at 21:07
2

Try this, I ll explain this at the end

public class Cerchio{

  private float r;
  private float area;
  private float cfr;
  final double pi = 3.14;


  public static void main(String[] args){
    System.out.println("Programma cerchio\n");

    Cerchio cerchio = new Cerchio(); 
    cerchio.getR();
    cerchio.c_cfr();
    cerchio.c_area();
    System.out.println("La circonferenza è: " + cerchio.cfr);
    System.out.println("L area è: " + cerchio.area);
  }

  private float getR(){
    InputStreamReader input = new InputStreamReader(System.in);
    BufferedReader num = new BufferedReader(input);

    try{
      String sr = num.readLine();
      r = Float.valueOf(sr).floatValue();
    } catch(NumberFormatException nfe){
        nfe.printStackTrace();
        System.out.println("Incorrect!!!");
      } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return r;
  }

  private float c_cfr(){
    cfr =(float)(2 * pi * r); //casting
    return cfr;
  }

  private float c_area(){
    area = (float)(pi * (r*r));
    return area;
 }

}

You have to either catch the proper exception or add throws declaration in your method.

Also for the best practice always print the stacktrace in order to understand what the exception is all about.

shikjohari
  • 2,278
  • 11
  • 23