My input file has 84 integers and I'm not sure what I'm doing wrong.

import java.util.*;
import javax.swing.*;
import java.awt.*;
import java.util.Scanner;
import java.io.*;

public class Museum
    private int Week=3;
    private int Hour=4;
    private int Day=7;

    int[][][] visitors = new int[Week][Day][Hour];
    private int[] allWeeks;
    private int[] allHours;
    private int[] allDays;
    Scanner scanToken;
    String line;
    Scanner scan = new Scanner(new File("Museum_Duguay.txt"));

    public Museum() throws IOException
        line = scan.nextLine();
        scanToken = new Scanner(line);

        for(int a=0; a < Week; a++)
                for(int b = 0; b < Day; b++)
                      for(int c=0; c < Hour; c++)
                    visitors[a][b][c] = scanToken.nextInt();

it highlights "visitors[a][b][c] = scanToken.nextInt();" when the error occurs when i try to run it.

Edited by Nick Evan: Fixed formatting

7 Years
Discussion Span
Last Post by BestJewSinceJC

That's because scanToken = new Scanner(line); creates a new Scanner object based on the String, line, that you passed as the argument. Obviously, since line is a String, it is not an integer, and trying to read ints from it will not succeed. The first declaration of Scanner that you did Scanner scan = new Scanner(new File("Museum_Duguay.txt")); was the correct way to do it. Now all you need to do is use the nextInt() method to get the Integers from that file.

Votes + Comments
I haven't given you rep in a while and you've had a lot of good posts lately. :)
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.