2

This is what I have so far....

/**
 * @param args
 */
public static void main(String[] args) {
    final String DATA_FILE = "payroll_problem.txt";
      Scanner scan = null;
      try
      {
       scan = new Scanner(new File(DATA_FILE));
      }
      catch (FileNotFoundException e)
      {
       System.err.printf("Could not open file \"%s\".\n", DATA_FILE);
      }
    int [] arr = new int[scan.nextInt()];
    for(int i = 0; i < arr.length; i++)
    {
        arr[i] = scan.nextInt();
        System.out.print(arr[i]);
    }   

    scan.close();
}

I keep getting error code

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.nextInt(Unknown Source)
at java.util.Scanner.nextInt(Unknown Source)
at fvse.main(fvse.java:22)

The five ints stand for how many hours the person has worked from Monday-Friday. Here is the data file.

Mohnan  Maria        8 8 8 9 8 10.01
Blue   Shelly        8 10 8 8 6  10.00
Black  8 8 8 8 8 23.0
Fortuna Jorge   5 5 5 5 5 10.10
Jones Mitchel   10 5.5 10 10 10 15.05
Olafson Sven    10 10 10 10 10  10.00
Cruz Astrid 1 1 1 1 1 20.50.3
Adler Irene 10 12 8 8 8  22.50
1
  • 1
    Please post a fragment of your data file. Indent it four spaces as if it were code, so that we could see individual lines. Thanks! Commented Apr 15, 2013 at 1:15

2 Answers 2

1

The problem happen because you call scan.nextInt() but your input file actually contains string/characters.

Either add the integer indicating the number of lines on the top of your input file, or change your code read by line (eg: using BufferredReader.readLine())

If you choose the former, make sure you also read the first and last name using two invocation of scan.next()

Sign up to request clarification or add additional context in comments.

Comments

0

You are reading your file for integers, but more than likely that file is filled with strings or characters.

Edit: Try scanning for lines or characters, or just using a FileInputStream, and then parsing the data once it's been loaded in.

Edit: Now that i've seen your data file, I would read in the file using standard file input practices (check out http://www.javapractices.com/topic/TopicAction.do?Id=42 if you need a tutorial on that). Then split the string based on spaces, and go through each string in your new string array and handle the data. The first 2 strings being names, and then integers until you get another name, or the end of the string.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.