Daniel
Daniel

Reputation: 23

Number types on Scanner

I made a simple program that calculates the delta based on 3 numbers from the formula. However, it has a problem with fractions and numbers after the decimal point (eg 3.33, 3/4). If I substitute it for any letter, an error appears:

Exception in thread "main" java.util.InputMismatchException at java.base/java.util.Scanner.throwFor(Scanner.java:939) at java.base/java.util.Scanner.next(Scanner.java:1594) at java.base/java.util.Scanner.nextInt(Scanner.java:2258) at java.base/java.util.Scanner.nextInt(Scanner.java:2212) at Main.main(Main.java:11)

How can I add a patch to make the program work correctly? My code below:

import java.util.Scanner;
import java.lang.Math;
import java.util.Locale;
public class Main {
    public static void main(String[] args) {
        final double n1; //a
        final double n2; //b
        final double n3; //c
        Scanner reader= new Scanner(System.in).useLocale(Locale.US);
        System.out.print("calculation of the delta. Enter the first number (a from the formula)");
        n1 = reader.nextDouble();
        System.out.print("give the second number (b from the formula)");
        n2 = reader.nextDouble();
        System.out.print("give the last number (c from the formula)");
        n3 = reader.nextDouble();
        final double delta = Math.pow(n2, 2) - 4 * n1 * n3;
        final double d01 = (-n2 + Math.sqrt(delta)) / (2 * n1);
        final double d02 = (-n2 - Math.sqrt(delta)) / (2 * n1);
        final String dinfo = "Delta is equal: ";
        if(delta>0) {
            System.out.println(dinfo + (int)delta + " , has [two] zero places, which is equal to: " + (int)d01 + " and " + (int)d02);
        }
        else if (delta == 0) {
            System.out.println(dinfo + (int)delta + ", has exactly [one] zero place, which is equal to: " + (int)d01);
        }
        else {
            System.out.println(dinfo + (int)delta + ", no zero places.");
        }
    }
}

Upvotes: 2

Views: 206

Answers (2)

Mizuki
Mizuki

Reputation: 2233

If you want to accept input numbers as decimal, you should use float or double type. e.g.

double n1;
n1 = reader.nextDouble();

About fractions case, you should use String type and then convert it to number type. e.g.

    String tmp;
    Scanner reader= new Scanner(System.in).useLocale(Locale.US);
    System.out.print("calculation of the delta. Enter the first number (a from the formula)");
    tmp = reader.next();
    n1 = fractionToDouble(tmp);
    tmp = reader.next();
    n2 = fractionToDouble(tmp);
....

Add fractionToDouble converting method. ref:mishadoff's answer

static double fractionToDouble(String ratio) {
        if (ratio.contains("/")) {
            String[] rat = ratio.split("/");
            return Double.parseDouble(rat[0]) / Double.parseDouble(rat[1]);
        } else {
            return Double.parseDouble(ratio);
        }
    }

Upvotes: 2

PushpikaWan
PushpikaWan

Reputation: 2525

Change int to double first

 final double n1; //a
 final double n2; //b
 final double n3; //c

 n1 = reader.nextDouble();
 n2 = reader.nextDouble();
 n3 = reader.nextDouble();

and you need to set Locale.us to resolve this InputMismatchException. Otherwise it may take "," or other characters as a decimal delimiter. This resolve your decimal issue

Scanner reader= new Scanner(System.in).useLocale(Locale.US);

Upvotes: 1

Related Questions