Home > Blockchain >  Is there anyone that can help me with this Runtime error?
Is there anyone that can help me with this Runtime error?

Time:12-20

I am completely new to programming.I created a program that uses two coordinates entered by the user and calculates the gradient and also comments when the function is increasing or decreasing just like in Mathematics Functions.I get a runtime error whenever i enter a fraction in this format 9/4 .I

import java.util.*;
public class mathfunctions {
public static void main(String[] args) {
Scanner coordinates = new Scanner (System.in).useLocale(Locale.US);
System.out.println("Enter 2nd y-coordinate:");

Double y2 = coordinates.nextDouble();

System.out.println("Enter 1st y-coordinate:");
Double y1 = coordinates.nextDouble();
System.out.println("Enter 2nd x-coordinate:");
Double x2 = coordinates.nextDouble();
System.out.println("Enter 1st x-coordinate:");

Double x1 = coordinates.nextDouble();
double numerator= (y2-y1);
double denominator= (x2-x1);
double gradient= numerator/denominator;
if
(numerator==0){
 System.out.print("Math Error!!!"); 
}
else{
System.out.print("Gradient=");
System.out.println(gradient);
}
if 
(gradient<0){
  System.out.println("The function is decreasing");    
}
else{
  System.out.println("The function is increasing");
}
  }
/*****************************************
This simple code is used to calculate the slope/gradient also referred to rate of change
*****************************************/
}

The error

java.util.InputMismatchException
    at java.util.Scanner.throwFor(Scanner.java:876)
    at java.util.Scanner.next(Scanner.java:1502)
    at java.util.Scanner.nextDouble(Scanner.java:2433)
    at com.mathfunctions.mathfunctions.main(mathfunctions.java:8)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.duy.android.compiler.java.Java.run(Java.java:115)
    at com.duy.ide.javaide.run.activities.ExecuteActivity.executeDex(ExecuteActivity.java:147)
    at com.duy.ide.javaide.run.activities.ExecuteActivity.exec(ExecuteActivity.java:124)
    at com.duy.ide.javaide.run.activities.ExecuteActivity.access$100(ExecuteActivity.java:45)
    at com.duy.ide.javaide.run.activities.ExecuteActivity$1.run(ExecuteActivity.java:88)
    at java.lang.Thread.run(Thread.java:764)

CodePudding user response:

This exception is usually thrown when the user input does not match the expected input. I do not think you are supposed to give a fraction format input. Instead of using (9/4) format input just go with 2.25.

CodePudding user response:

The reason that you are getting mismatch input error is your input is not valid in programming, in this case, you have to represent fraction numbers with a point like so: 9/4 => 2.25

CodePudding user response:

So the exception you are getting is java.util.InputMismatchException this is basically telling you that the input by the user doesn't match the expected input, which is what @SuppaGonzalo has also noted.

You can easily find the location of the error by looking at the stacktrace which appears below the exception:

java.util.InputMismatchException
at java.util.Scanner.throwFor(Scanner.java:876)
at java.util.Scanner.next(Scanner.java:1502)
at java.util.Scanner.nextDouble(Scanner.java:2433)
at com.mathfunctions.mathfunctions.main(mathfunctions.java:8) <-- THIS LINE IS IMPORTANT
at java.lang.reflect.Method.invoke(Native Method)
at com.duy.android.compiler.java.Java.run(Java.java:115)
at com.duy.ide.javaide.run.activities.ExecuteActivity.executeDex(ExecuteActivity.java:147)
at com.duy.ide.javaide.run.activities.ExecuteActivity.exec(ExecuteActivity.java:124)
at com.duy.ide.javaide.run.activities.ExecuteActivity.access$100(ExecuteActivity.java:45)
at com.duy.ide.javaide.run.activities.ExecuteActivity$1.run(ExecuteActivity.java:88)
at java.lang.Thread.run(Thread.java:764)

As you can see in the stacktrace you are calling nextDouble on line 8 of mathFunctions class.

at java.util.Scanner.nextDouble(Scanner.java:2433) <-- THE METHOD WE'RE CALLING.
at com.mathfunctions.mathfunctions.main(mathfunctions.java:8) <-- THE LINE WE ARE CALLING NEXTDOUBLE ON.

You have successfully entered nextDouble which is fine, but the input the user has entered is NOT a Double, so InputMismatchException is thrown.

If you look at the code you have provided you have nothing on line 8, that is because you haven't shared the whole code, you seem to have missed of the package part; which if you add that in your error is happening this line: Double y2 = coordinates.nextDouble();.

From what you have said, you have entered a fraction of 9/4, this is not a Double, so will not get parsed with nextDouble().

What you probably want is something like this to convert your fraction to a Double.

public static Double convertFractionToDouble(String fraction) throws ParseException {
    if (fraction != null) {
        if (fraction.contains("/")) {
            String[] numbers = fraction.split("/");
            if (numbers.length == 2) {
                BigDecimal numerator = BigDecimal.valueOf(Double.valueOf(numbers[0]));
                BigDecimal denominator = BigDecimal.valueOf(Double.valueOf(numbers[1]));
                BigDecimal response = numerator.divide(denominator, MathContext.DECIMAL128);
                return response.doubleValue();
            }
        } else {
            return Double.valueOf(fraction);
        }
    }
    throw new ParseException(fraction, 0);
}

and call it like:

Double y2 = convertFractionToDouble(coordinates.next());

I would probably add some more validation to the convertFractionToDouble method but that is the general idea of what you want.

CodePudding user response:

Scanner.nextDouble() doesn't evaluate expressions: it just reads a literal double.

  •  Tags:  
  • java
  • Related