Java stringtokenizer. I need a solution

Asked 2 years ago, Updated 2 years ago, 23 views

StringTokenizer st; for (int k = 0; k < sentence.length; k++) { System.out.print(sentence[k]); st = new StringTokenizer(sentence[k], " ");

        for (int i = 0; i < cnt_line; i++) {
            op[i] = st.nextToken();
            for (int j = 0; j < 2; j++) {
                n[i][j] = Integer.parseInt(st.nextToken());
                if (j == 1)
                    System.out.println(": " + Calc(n[i][0], n[i][1], op[i]));
            }
        }
    }

I'm putting the sentences in the Sentence array back into the stringtokenizer as a repetition After k is 0, the error appears as shown below.

Exception in thread "main" java.util.NoSuchElementException at java.util.StringTokenizer.nextToken(StringTokenizer.java:349) at prac.practice.main(practice.java:47)

Doesn't the pointer pointing to the sentence go to the front again? Find a solution.

java

2022-09-21 17:31

3 Answers

It is difficult to understand the author's intention because there is no whole code, but if there is an error in nextToken, it would be better to use hasMoreToken().

StringTokenizer st; for (int k = 0; k < sentence.length; k++) { System.out.print(sentence[k]); st = new StringTokenizer(sentence[k], " ");

Here, it's going to be a form of overwriting st every time k goes, but I don't know what you're trying to do.


2022-09-21 17:31

package prac;

import java.io.BufferedReader; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.FileWriter; import java.util.ArrayList; import java.util.Collections; import java.util.StringTokenizer;

public class practice { static FileReader reader;

public static void main(String args[]) throws Exception {
    try {
        reader = new FileReader("input.txt");
    } } catch (FileNotFoundException e) {
        System.out.println ("The file cannot be opened."");
        System.exit(1);
    }
    BufferedReader rd = new BufferedReader(reader);
    int cnt_line = 0;// Number of rows in the file
    String line = null;// to save fragments read from the file
    while(((line = rd.readLine())!=null) {// Read one line at a time
        cnt_line++;
    }
    rd.close();

    String sentence[] = new String[cnt_line];// connecting the lines to form a single sentence
    reader = new FileReader("input.txt");
    BufferedReader rd2 = new BufferedReader(reader);
    int a = 0;
    while(((line = rd2.readLine())!=null) {// Read one line at a time
        System.out.println(line);
        sentence[a++] = line;
    }
    rd2.close();

    String op[] = new String[cnt_line];// Contains operators by row
    int n[][] = new int[cnt_line][2];

    StringTokenizer st;
    for (int k = 0; k < sentence.length; k++) {
        System.out.print(sentence[k]);
        st = new StringTokenizer(sentence[k], " ");

        for (int i = 0; i < cnt_line; i++) {
            op[i] = st.nextToken();
            for (int j = 0; j < 2; j++) {
                n[i][j] = Integer.parseInt(st.nextToken());
                if (j == 1)
                    System.out.println(": " + Calc(n[i][0], n[i][1], op[i]));
            }
        }
    }
}

static int Calc(int a, int b, String op) {
    int result = 0;
    switch (op) {
    case "ADD":
        result = a + b;
        break;
    case "SUB":
        result = a - b;
        break;
    case "MUL":
        result = a * b;
        break;
    case "DIV":
        result = a / b;
        break;
    default:
        result = 923;
        break;
    }

    return result;
}

}


2022-09-21 17:31

I don't know what information input.txt contains, but you made a string talker with sentence[k] as a factor, and if you condition int i = 0; i < cnt_line; i++ as a for statement, then every string in input.txt should have the same number of tokens as the input. It seems more appropriate to use hasMoreToken() with a while statement instead of a for statement.


2022-09-21 17:31

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.