-4

My code is not working. The text file is in the same folder as my classes. I used the pathname, which worked, but I don't think that would work if I send the file to someone else. And converting the Strings to primitive type using parse methods isn't working, either. Not sure what I'm doing wrong. Can anyone help?

Here is my code:

import java.util.Scanner;
import java.util.StringTokenizer;
import java.io.FileNotFoundException;
import java.io.FileInputStream;

public class TestInventory {
    public static void main(String[] args) {
        // TODO Auto-generated method stub

        Inventory movieList = new Inventory();
        Scanner inputStream = null;
        try{
            inputStream = new Scanner(new FileInputStream("movies_db.txt"));
        }
        catch(FileNotFoundException e){
            System.out.println("File not found or could not be opened");
            System.exit(0);
        }
        while(inputStream.hasNextLine()){
            String s = inputStream.nextLine();
            StringTokenizer st = new StringTokenizer(s, " - ");
            String t1 = st.nextToken();
            String t2 = st.nextToken();
            String t3 = st.nextToken();
            String t4 = st.nextToken();
            int y = Integer.parseInt(t2);
            double r = Double.parseDouble(t4);
            int d = Integer.parseInt(t3);
            Movie m = new Movie(t1, y, r, d);
            movieList.addMovie(m);
        }
    }
}

And this is the output I get:

run:
Exception in thread "main" java.lang.NumberFormatException: For input string: "America:"
    at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
    at java.lang.Integer.parseInt(Integer.java:580)
    at java.lang.Integer.parseInt(Integer.java:615)
    at TestInventory.main(TestInventory.java:29)
C:\Users\customer\AppData\Local\NetBeans\Cache\8.1\executor-snippets\run.xml:53: Java returned: 1
BUILD FAILED (total time: 0 seconds)
Blasanka
  • 21,001
  • 12
  • 102
  • 104
J Joseph
  • 1
  • 3
  • 3
    What is the output that you get? – Subu Aug 10 '16 at 00:42
  • run: Exception in thread "main" java.lang.NumberFormatException: For input string: "America:" at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65) at java.lang.Integer.parseInt(Integer.java:580) at java.lang.Integer.parseInt(Integer.java:615) at TestInventory.main(TestInventory.java:29) C:\Users\customer\AppData\Local\NetBeans\Cache\8.1\executor-snippets\run.xml:53: Java returned: 1 BUILD FAILED (total time: 0 seconds) – J Joseph Aug 10 '16 at 00:49
  • @JJoseph what does `movies_db.txt` look like? – Arthur Aug 10 '16 at 01:04
  • Has multiple lines of text. In each line of text, there's a movie title, followed by the year the movie was released (an integer), followed by its duration in minutes ((an integer), followed by a rating (a double). Something like this: The Avengers - 2012 - 151 - 3.5 – J Joseph Aug 10 '16 at 01:19

2 Answers2

1

The error message occurs because you are parsing the String "America:" into parseInt().


All characters in the delim argument are the delimiters for separating tokens. source

That means that instead of splitting the text when ever there is a " - " it will split whenever there is a " " or "-".

I think you would be better of using string.split(String regex). This would allow you to parse " - " and get a String array in return.

Arthur
  • 1,246
  • 1
  • 15
  • 19
0

You've fallen into one trap with the StringTokenizer class, the second parameter is read as a set of distinct characters to use as a delimiter, not as a string that must be present as a whole.

This means that instead of splitting on the exact string " - ", it will split where-ever there is a space or an -. This means that t2 probably does not contain what you think it will contain.

Assuming each line should always contain 4 tokens, you can test this be checking if st.hasMoreTokens() is true, in which case it has split the string into more parts than you intended.

Arthur
  • 1,246
  • 1
  • 15
  • 19
Kiskae
  • 24,655
  • 2
  • 77
  • 74