0

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.

ThatOneGoi
  • 29
  • 4
  • Your while loop tests if input is there to be read before you readInput. You can have only one readInput.nextLine for each hasNext. – Neeraj Agarwal Aug 29 '19 at 23:36
  • So I need to use more than one while loop? How would I go about reading the spaces after "New York"? – ThatOneGoi Aug 29 '19 at 23:43
  • 1
    Possible duplicate of [Scanner is skipping nextLine() after using next() or nextFoo()?](https://stackoverflow.com/questions/13102045/scanner-is-skipping-nextline-after-using-next-or-nextfoo) – Tom Aug 30 '19 at 01:18
  • Please see example code. – Neeraj Agarwal Aug 30 '19 at 01:34

2 Answers2

0

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
Neeraj Agarwal
  • 1,036
  • 3
  • 5
0

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.

Peter Li
  • 86
  • 2
  • 7