简体   繁体   中英

InputMismatchException while using Scanner to read from file

New to Java. I'm having a hard time understanding why my code isn't running. I'm getting a InputMismatchException when I try to run my code.

I did some testing and problems occur if there's white space in my file such as "New York." I've been trying different things such as looping with .hasNextLine() instead of .hasnext() as suggested in other threads but to no avail. Sometimes I can get it to run until the end it gives me a NoSuchElementException. If you could please put me in the right direction, that would help a lot thank you!

import java.util.*;
import java.io.*;

public class StandaloneReport  {

    public static void main(String[] args) {

        String fileInputName;
        String fileOutputName;

        String firstName;
        String lastName;
        String houseNumber;
        String street;
        String city;
        String state;
        String zip;
        String productDescription;
        double productPrice;

        //Scanner obj1
        Scanner input = null;
        input = new Scanner(System.in);

        System.out.printf("What is the file name?\n");
        fileInputName = input.nextLine();

        //Print out the name user inputed
        System.out.println("File name is: " + fileInputName);

                //Read the file
        FileReader filereader;
        Scanner readInput = null;

        try {
            readInput = new Scanner(filereader = new FileReader(fileInputName));
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        while (readInput.hasNext())
        {
            firstName = readInput.next();
            lastName = readInput.next();
            houseNumber = readInput.next();
            street = readInput.next();
            city = readInput.nextLine();
            state = readInput.next();
            zip = readInput.next();
            productDescription = readInput.nextLine();
            productPrice = readInput.nextDouble();

Textfile looks like this:

Jane
Doe
10
Broadway
New York
NY
10001
Galaxy S10
199.99
2
Samsung Bluetooth
29.99
1
Slim Fit Hard Plastic Case
2.99
2
Charger
17.99
3


Error I get:

Exception in thread "main" java.util.InputMismatchException
    at java.util.Scanner.throwFor(Unknown Source)
    at java.util.Scanner.next(Unknown Source)
    at java.util.Scanner.nextDouble(Unknown Source)
    at hey.bcs.hwk.purchases.standalonereport.StandaloneReport.main(StandaloneReport.java:55)

I expected it to read it smoothly so I can print it using PrintStream in another file but I cannot even get past this part.

This is for one set of data, one data item per line. You have to make adjustments for multiple sets of data.

int i = 0;
while (readInput.hasNext())
{
    if (i == 0)
    {
        firstName = readInput.nextLine();
    }
    else if (i == 1)
    {
        lastName = readInput.nextLine();
    }
    else if (i == 2)
    {
        houseNumber = readInput.nextLine();
    }
    else if (i == 3)
    {
        street = readInput.nextLine();
    }
    else if (i == 4)
    {
        city = readInput.nextLineLine();
    }
    else if (i == 5)
    {
        state = readInput.nextLine();
    }
    else if (i == 6)
    {
        zip = readInput.nextLine();
    }
    else if (i == 7)
    {
        productDescription = readInput.nextLine();
    }
    else if (i == 8)
    {
        productPrice = readInput.nextDouble();
    }

i += 1;
} // End while

To be honest your program is problematic in so many ways. But here's an explanation to fix the mismatch issue you mentioned.

readInput.nextLine()

will read the remainder of the current line . So after reading "Broadway" the Scanner stays in the same line and when you call nextLine, the Scanner yields whatever is left in the line for "Broadway", which is an empty String.

To avoid this situation, do

street = readInput.next();
readInput.nextLine();

To drop the current line("Broadway" for example). And then call

city = readInput.nextLine();

That way the program will read "New York" as you expected. As Tom mentioned in the comments, for more details, look at the question asked here .

Apart from the Scanner issue, your program is ambiguous as to where it ends – you did not provide closing brackets. That while loop seems redundant considering that your input is broken: it ceases to match what you have in your code after the "199.99" line. Please put your complete code on there and revise your sample input.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM