简体   繁体   中英

StringTokenizer NoSuchElements

I have created a method that reads a file set like this (//... are comments, ignore them):

5 // n jobs 
2 // n tools
1 4 5 6 2
1 5 4 2 3

The matrix represents the tools used for each job but it doesn't really matter here.

Here is the method :

public static JobS inputJobMatrix(){
    String line = ""; // Line in tokenizer
    int jobN = inputJobN(); //First number of the file (jobs) works
    int toolN = inputToolN(); //Second number of the file (tools) works 
    //Instancing JobS object 
    JobS inputJobS = new JobS(jobN, toolN);
    int[][] tabFill = new int[jobN][toolN];
    int[] tabFillOrder = new int[jobN];
    try {
        // Initializing reader.
        FileReader fr = new FileReader("input.txt");
        BufferedReader br = new BufferedReader(fr);
        StringTokenizer st = new StringTokenizer(line);
        for(int i=0; i<3; i++){ //ReachFirstLine of matrix
            line = br.readLine();
            //System.out.println(line);
        }
        //Instancing tab for Job Order 1...n
        int[] a = new int[jobN];
        for (int i=0; i<jobN; i++){
            a[i]=i+1;
        }
        //Filling Order tab with Job order
        JobS.fillLine(tabFillOrder, a, 0); //Fills the tab with the tab a (make a copy of it we could say)
        //Reading the matrix line by line and filling tab line
        for(int i=0; i<jobN; i++){
            for(int j=0; j<toolN; j++){
                String str = st.nextToken();
                System.out.println(str);
                tabFill[i][j] = Integer.parseInt(str);
            }
            line = br.readLine();
        }
        inputJobS.setJobS(tabFill);
        br.close();

    } catch (IOException e) {
        System.out.println("File not found exception in inputJobMatrix.");
    }
    return inputJobS;

}

Which results in :

Exception in thread "main" java.util.NoSuchElementException
at java.util.StringTokenizer.nextToken(Unknown Source)
at pProgra.ReadJobS.inputJobMatrix(ReadJobS.java:84)
at pProgra.PPMain.main(PPMain.java:14)

I've tried looking for problems in my loops but didnt find any, and i can't understand why it doesn't work. The goal here is to fill a bidimensionnal int array with the matrix of the input file (for exemple the one i've given previously with jobs and tools) and use that array for my object (JobS, i'll give the constructor here too if it can help) :

public class JobS {
private int[] jobOrder;
private int[][] jobS;

public JobS(int jobs, int tools){// Creates one more line for the title (jobOrder).
    super();
    int[][] tab = new int[jobs][tools]; 
    int[] tab2 = new int[jobs];
    this.jobS = tab;
    this.jobOrder = tab2;
}

And the setter i use at the end:

public void setJobS(int[][] jobS) {
    this.jobS = jobS;
}

I tried detailing the code as much as possible with comments, I hope you will understand what i want to do.

This is the first time i'm trying to do a "complex" application so maybe i'm just stupid and forgot something, but right now i've been searching for an hour and still have no clue what is causing this ..

Hope you can help, thanks in advance ! LL

as you can see the String line is empty:

 String line = ""; // Line in tokenizer

so here st is empty:

StringTokenizer st = new StringTokenizer(line);

hence when you call this:

String str = st.nextToken();

an exception occurs.

ensure that the line has some data first, by instantiating the StringTokenizer after the for loop.

Example

change this:

StringTokenizer st = new StringTokenizer(line);
for(int i=0; i<3; i++){ //ReachFirstLine of matrix
       line = br.readLine();
       //System.out.println(line);
}

to this:

for(int i=0; i<3; i++){ //ReachFirstLine of matrix
        line = br.readLine();
        //System.out.println(line);
}
StringTokenizer st = new StringTokenizer(line);

side note - this code:

line = br.readLine();

will overwrite the value of line at each iteration within the loop, that could be what you wanted but if you want to append all the lines of text the readLine() gets then you can do this:

line += br.readLine();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM