0

I've been using eclipse and OnlineGDB for my Java works. I recently started to use IntelliJ IDEA and I'm enjoying this a lot. But i tried to work one of my simple projects in IntelliJ IDEA about file operations. But it gives me an error during running. I'm experiencing no problem in OnlineGDB with the exact same codes.

import java.util.InputMismatchException;
import java.util.Scanner;
import java.io.File;
import java.io.PrintWriter;

public class Main

{
    static void createFile() throws Exception
    {
        PrintWriter output = new PrintWriter(new File("input.txt"));
        output.print("2.5");
        output.close();
    }


public static void main(String[] args) throws Exception
{
    Scanner input = new Scanner(System.in);
    Circle[] circles = new Circle[3];

    circles[0] = new Circle(1.0);

    System.out.print("Radius of the second circle => ");
    circles[1] = new Circle(input.nextFloat());
    input.close();

    createFile();
    input = new Scanner(new File("input.txt"));
    circles[2] = new Circle(input.nextFloat());
    input.close();

    for (int i = 0; i<=circles.length-1; i++)
    {
        System.out.println( "c(" + i + ") radius = " + circles[i].getRadius()
        + " area = " + circles[i].area() );
    }

    float total = 0;
    for (int i = 0; i<3; i++)
    {
        total +=circles[i].getRadius();
    }
    System.out.println("Total radius of the circles = " + total );

    total = 0;
    for (int i = 0; i<3; i++)
    {
        total+= circles[i].area();
    }
    System.out.println("Total area of the circles = " + total);
}
}

    public class Circle
{
    private double radius;

    public Circle (double radius)
    {
        this.radius = radius;
    }

    public double getRadius()
    {
        return radius;
    }

    public double area()
    {
        return(Math.PI * radius * radius);
    }
}

It gives me the "java.util.InputMismatchException" error during these lines:

System.out.print("Radius of the second circle => ");
        circles[1] = new Circle(input.nextFloat());
        input.close();
Christopher Moore
  • 15,626
  • 10
  • 42
  • 52
Gökhan Uçar
  • 191
  • 15
  • Your code works for me in Intellij, I gave the input as 5. What input did you give? – Anon May 31 '20 at 14:49
  • I see your problem, perhaps you entered 2.5 as input. Well, 2.5 is not an integer. Integers are like 2,3,.. so when you give a non-interger as an input and your code expects to read integer, it will be problem. You are reading integer when you call - input.nextInt(). – Anon May 31 '20 at 14:52
  • Oops, i forgot to change "input.nextInt()" part to "input.nextFloat()" in the sample code above, my bad. But it is not the problem. It is still gives me the InputMismatchException exception even though I changed to nextInt() to nextFloat() . And Im' giving both integer and float inputs like 2, 2.5, 0 etc... Nothing changes. – Gökhan Uçar May 31 '20 at 14:59
  • You'r inside IDEA, you be able to put a break point at ```throw new InputMismatchException(nfe.getMessage());``` inside Scanner.java to see what exactly going on. That'd be faster than guessing or analyzing the static code. – HoaPhan May 31 '20 at 16:39
  • I think surround your `input.nextFloat();` with try-catch to the handle the exception. It's working fine on mine as well unless I put in a string then we get that exception. – Tony May 31 '20 at 17:07

1 Answers1

0
System.out.print("Radius of the second circle => ");
circles[1] = new Circle(input.nextFloat());
input.close();

I changed input.nextInt() to input.nextFloat() Change part of code that expects integer to read float(or double, if you wish). You should be able to enter values like 2.5 and not get any error.

I explained in comment why you get error if you enter 2.5

Sample output:

Radius of the second circle => 2.5
c(0) radius = 1.0 area = 3.141592653589793
c(1) radius = 2.5 area = 19.634954084936208
c(2) radius = 2.5 area = 19.634954084936208
Total radius of the circles = 6.0
Total area of the circles = 42.4115
Anon
  • 2,608
  • 6
  • 26
  • 38
  • Yeah i made that change too, but nothing changed. What surprises me, is that I get no error in other IDEs or compilers, whatever they are. It only happens in IntelliJ IDEA. I try to surround with try/catch for Mismatch Exception but still same. – Gökhan Uçar May 31 '20 at 15:03
  • I ran the code in Intellij. I copy pasted your code as it is - with one change - I removed the import java.util.InputMismatchException; What input did you give? Did you just enter the number and press enter or did you do something else? – Anon May 31 '20 at 15:09
  • I removed the import java.util.InputMismatchException; line now and gave the value of 2.5 (and then 2, 0 ....) and pressed enter. But nothing changes. – Gökhan Uçar May 31 '20 at 15:12
  • Aah, I see, could it be that you are from Europe and "," and "." mean different thing? The locale in Europe is different from say US or other Anglophone countries. Can you just enter 2 (without comma and without dot)? If that does not work, please tell me the EXACT input (including space,comma, dot etc) that you entered. – Anon May 31 '20 at 15:14
  • Entered just 2 and still gives the exception. 2, 2.5 or 2,5; the result is always same, throws the exception. – Gökhan Uçar May 31 '20 at 15:16
  • Can you use some screen recording tool and record yourself giving the input and seeing the exception? There is no problem with the code. It works fine. – Anon May 31 '20 at 15:18