0

I'm working on a java application that reads in data from a .txt file, the scanner reads in each line of data from the .txt file converts it into an object, then sends that object to a database I'm running on phpMyAdmin. However my scanner is skipping lines of data in my .txt file. This results in input mismatch errors.

Heres the data in the .txt file:

12
Sylvia Plath
The bell jar
6.99
The Bell Jar
10.99
1

Here's the method for reading in the data from the .txt file

private static void readBookFile(Scanner kb, BookModel bmodel) //this method will allow the user to create a book, it then returns that book object
    {
        kb = new Scanner(System.in);
        Print("Input File name:");
        String inputFile = kb.nextLine();
        File file = new File(inputFile + ".txt");

        try {
            Scanner input = new Scanner(file);
            while (input.hasNextLine()) {
                String line = input.next();

                createBook(input, bmodel);

            }
            input.close();
        } catch (FileNotFoundException ex) {
            System.out.println("No such file exists");

        }

    }

And here is the method for converting that data to an object:

 public static void createBook(Scanner enter, BookModel bModel) {
        try {
           String xx = enter.nextLine();
            String an = enter.nextLine();
            Print(an);
            String ee = enter.nextLine();
            String bn = enter.nextLine();
            Print(bn);
            Double cp = enter.nextDouble();
            Print(cp);
            Double sp = enter.nextDouble();
            Print(sp);
            int pc = enter.nextInt();

            Book b = new Book(an, bn, cp, sp, pc);

            bModel.addBook(b);
        } catch (SQLException ex) {

        }
    }

I've tried adding an empty string to catch the skip but that still does not help. Here is the error I'm getting here, just to see where the data is being skipped:

=====================================================================
1. Read Books in from a text File.
2. Delete a Book
3. Add Product Category
4. Edit a Book.
5. View list of all current books.
6. Product menu
7. Exit
Please press select an option by using the numbers on your Keyboard
1
You Chose option: 1
Input File name:
in
Sylvia Plath
6.99
Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Scanner.java:909)
at java.util.Scanner.next(Scanner.java:1530)
at java.util.Scanner.nextDouble(Scanner.java:2456)
at retailcompanydatabase.Classes.Menu.createBook(Menu.java:138)
at retailcompanydatabase.Classes.Menu.readBookFile(Menu.java:216)
at retailcompanydatabase.Classes.Menu.Menu(Menu.java:54)
at Main.Main.main(Main.java:12)

Here's the instance variables of the object I'm adding for further reference:

 private int id;
 private double costPrice;
 private double sellPrice;
 private String authorName;
 private String bookName;
 private int productCategoryID;

Any ideas on how I can try and fix this? I'd like to know how to fix it if possible instead of a workaround, if at all possible.

Jamie Hyland
  • 116
  • 1
  • 1
  • 10
  • Is `6.99` really a valid input for your current Locale? Or do you use `6,99` for floating point values? If yes, then either correct your file or try `kb = new Scanner(System.in).useLocale(Locale.US);`. – Tom Feb 29 '16 at 13:19
  • I'm not sure what you mean by locale? I'm adding in books, I should have posted the class instance variables I apologize, I'll re-edit the post with the values in it. – Jamie Hyland Feb 29 '16 at 13:21
  • The `Locale` is used by `Scanner` to determine the decimal separator char for floating point number. If you life in the USA or England, then it would be a `.`, but in France (for example) it is a `,`, so a JVM with a french locale requests a `6,99`, whereas a british configured OS/JVM requests `6.99`. So if you're from France (for example) and like to use `.` instead of `,`, then you need to specify a `Locale`, so `Scanner` won't use the default one. – Tom Feb 29 '16 at 13:27
  • Oh, apologies, I'm from Ireland so I'd assume we would adhere to the British decimal separator. – Jamie Hyland Feb 29 '16 at 13:32
  • Can you please try this in a small project in your local Java environment `System.out.println(DecimalFormatSymbols.getInstance().getDecimalSeparator());`? This prints the currently expected decimal separator (just to be sure it is `.`, not `,`). Btw: have you read the duplicate question? The linked questions there may also fix your current issue here. – Tom Feb 29 '16 at 13:35
  • Ok, then it is not a problem with the number itself. Then please check the duplicate question, because the linked questions there should contain the solution to your problem. – Tom Feb 29 '16 at 13:38
  • Btw: you can also delete your comment about your home country, if you don't like to keep this for privacy reasons (which is very understandable). You don't also need to use your real name on Stackoverflow/StackExchange (if it is your real name). – Tom Feb 29 '16 at 13:41

0 Answers0