haasbury
haasbury

Reputation: 13

Works fine when I build it, outputs an error when I run it

The runtime error I keep getting is

Exception in thread "main" java.util.InputMismatchException at java.util.Scanner.throwFor(Scanner.java:840) at java.util.Scanner.next(Scanner.java:1461) at java.util.Scanner.nextInt(Scanner.java:2091) at java.util.Scanner.nextInt(Scanner.java:2050) at lab4b2.main(lab4b2.java:12)

My code is

import java.util.*;
import java.io.*;
public class lab4b2
{
    public static void main (String [] args)
        throws IOException
    {
        Scanner fileIN = new Scanner(new File("lab4b2.txt"));
        char unit;
        double amount,conv;
        unit = fileIN.next().charAt(0);
        amount = fileIN.nextInt();
        switch (unit)
        {
            case 'p': conv = amount*4.9;
                System.out.printf("%.2f pounds = %.2f newtons",amount,conv);
                 break;
            case 'P': conv = amount*4.9; 
                System.out.printf("%f pounds = %f newtons",amount,conv);
                 break;
            case 'f': conv = amount*0.30488; 
                System.out.printf("%f feet = %f meters",amount,conv);
                 break;
            case 'F': conv = amount*0.30488;
                System.out.printf("%f feet = %f meters",amount,conv);
                 break;
            case 'm': conv = amount*1.61; 
                System.out.printf("%f miles = %f kilometers",amount,conv);
                 break;
            case 'M': conv = amount*1.61; 
                System.out.printf("%f miles = %f kilometers",amount,conv);
                 break;
            default: System.out.println("Please enter the possible units only."); 
                break;
        }
    }
}

What is the problem? This is only happening after I click run. I'm using JCreator if that helps. I believe the problem is somewhere in the printf but I don't see any reasonable answers to the problem. My file input is M 3.106 P 125 F 120 P 7.2 F 56 M 100

Upvotes: 0

Views: 79

Answers (3)

Kevin
Kevin

Reputation: 2182

I believe the error is here:

amount = fileIN.nextInt();

The first number you read is 3.106, which is not an int. You probably want to make the following changes:

float amount, conv;
amount = fileIN.nextFloat();

The error output in Java is great for walking you onto the problem line. This:

java.util.Scanner.nextInt(Scanner.java:2050) at lab4b2.main(lab4b2.java:12)

indicates that the error happens in filelab4b.java, line 12.

Upvotes: 0

mikemil
mikemil

Reputation: 1241

This sounds like someone's homework or school project. That said, your problem is on line 12. You are scanning for an int but the data file has 3.106, which is not an int.

Upvotes: 0

Sionnach733
Sionnach733

Reputation: 4736

The error is occurring on this line:

amount = fileIN.nextInt();

the first number in the file is not an int so it causes the InputMismatchException. Provide more info on what you are trying to achieve for a better answer.

Upvotes: 1

Related Questions