简体   繁体   中英

Not able to assign a text for a string in array using split method

I have a question about my split function inside the file reading function. Here is my code. I tried to use split to put these text in to array. But the problem is I have this error. java.lang.NumberFormatException: For input string: "Sug" at java.base/java.lang.NumberFormatException.forInputString

public static SLL<train> readFile() {
    SLL<train> list = new SLL();
    try {
        FileReader fr = new FileReader("Train.dat");
        BufferedReader br = new BufferedReader(fr);
        String line = "";
        while (true) {
            line = br.readLine();
            if (line == null) {
                break;
            }
            String[] text = line.split(" | ");
            String tcode = text[0];
            String train_name = text[1];
            int seat = Integer.parseInt(text[2]);
            int booked = Integer.parseInt(text[3]);
            double depart_time = Double.parseDouble(text[4]);
            String depart_place = text[5];
            train t = new train(tcode, train_name, seat, booked, depart_time, depart_place);
            list.addLast(t);
        }
        br.close();
        fr.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return list;

This is my text file: 在此处输入图像描述

"SUG" should be added into train name because I declared train_name as a string.I think this error only appears when declaring the wrong data type, "12" should be added into seat, "3" should be added into booked, and so on. Can you guys explained to me what happened to "SUG". Thanks a lot:(

Note that String.split(regex) uses a regex to find the split location, ie it splits before and after any match produced by the regular expression.

Furthermore, in regex the pipe ( | ) has the special meaning "or" so your code right now reads as "split on a space or a space". Since this splits on spaces only, splitting "B03 | Sug | 12..." will result in the array ["B03","|","Sug","|","12",...] and hence text[2] yields "Sug" .

Instead you need to escape the pipe by either using line.split(" \\| ") or line.split(Pattern.quote(" | ")) to make it "split on a sequence of space, pipe, space". That would then result in ["B03","Sug","12",...] .

However, you also might need to surround Integer.parseInt() etc. with a try-catch block or do some preliminary checks on the string because you might run into a malformed file.

In addition to what @Thomas has said, it would be a whole lot easier to use a Scanner

sc.useDelimiter("\\s*\\|\\s*");
while (sc.hasNext()) {
    Train t = new Train(sc.next(), sc.next(), sc.nextInt(), sc.nextInt(), sc.nextDouble(), sc.next());
}

Naming is important in Java for readability and maintenance Note class names begin upper case in Java. There's no place for underscores except for as separators for blocks of capitals in the name of a constant.

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