如何解析文本文件/需要在 switch 语句中使用第一个字符

发布于 2024-12-29 17:48:55 字数 3770 浏览 2 评论 0原文

我需要使用分隔符“:”解析文本文件

文本文件如下所示:

a : 29173 : He : James : 474937 : 2 : 4 : 1

t : 27184 : She : Susan: 474930 : 6 : 4 : 2

c : 28174 : 他 : 梅格 : 474931 : 5 : 4 : 1

p : 29190 : She : Robin : 474947 : 4 : 4 : 4

我的第一个问题是如何解析 switch 语句中使用的第一个字符? 我在想类似下面的代码(我把它们放在一起......但我很确定它是不正确的)。

我知道我可以读取第一个字符 &我知道我可以使用带有字符的 switch 语句,但我不知道如何将两者放在一起。

File file = new File("test.txt");

    // Read the file
    try {
        Scanner scanner = new Scanner(file).useDelimiter(":");
        String line = scanner.nextLine();

        while (scanner.hasNextLine())
        {
            line = line.trim();
            int index; 
            String type;
            String name; 

            String identifier = scanner.next();
            switch(line.charAt(0)) {

            case 'p': 
                index = scanner.nextInt();
                name = scanner.next();      
                break;

            case 'c':
                index = scanner.nextInt();
                type = scanner.next();
                name = scanner.next();
                int partyC = scanner.nextInt();
                int empathyC = scanner.nextInt();
                int carryingCapacityC = scanner.nextInt();
                break;

            case 't':
                index = scanner.nextInt();
                type = scanner.next();
                int creatureT = scanner.nextInt();
                int weightT = scanner.nextInt();
                int valueT = scanner.nextInt();
                break;

            case 'a':
                index = scanner.nextInt();
                type = scanner.next();
                int creatureA = scanner.nextInt();


            }
        } 

    } catch (IOException e) {
        e.printStackTrace();
    }
}

VS。

好吧,我已经阅读并尝试了一些,并且认为我可以使用 if-else 循环。但是,我遇到了许多错误(在代码下方)。

        File file = new File("test.txt");

    // Read the file
    try {
        Scanner scanner = new Scanner(file).useDelimiter(":");

        while (scanner.hasNextLine())
        {

            String line = scanner.nextLine();
            line = line.trim();

            int index;
            String type;
            String name; 
            char identifier = line.charAt(0);

            if (identifier == 'p') {
                index = scanner.nextInt();
                name = scanner.next();  

            } else if (identifier == 'c') {
                index = scanner.nextInt();
                type = scanner.next();
                name = scanner.next();
                int partyC = scanner.nextInt();
                int empathyC = scanner.nextInt();
                int carryingCapacityC = scanner.nextInt();

            } else if (identifier == 't') {
                index = scanner.nextInt();
                type = scanner.next();
                int creatureT = scanner.nextInt();
                int weightT = scanner.nextInt();
                int valueT = scanner.nextInt();

            } else if (identifier == 'a') {
                index = scanner.nextInt();
                type = scanner.next();
                int creatureA = scanner.nextInt();

            } else {
                System.out.println("This is not a valid line of input");
            } System.out.println("Identifier: " + identifier);
        } 

    } catch (IOException e) {
        e.printStackTrace();
    }
}
}

错误:

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 SorcerersCave.main(SorcerersCave.java:33)

I need to parse a text file using the delimiter ":"

Text file looks like this:

a : 29173 : He : James : 474937 : 2 : 4 : 1

t : 27184 : She : Susan: 474930 : 6 : 4 : 2

c : 28174 : He : Meg : 474931 : 5 : 4 : 1

p : 29190 : She : Robin : 474947 : 4 : 4 : 4

My first question is how can/can I parse the first character to use in a switch statement?
I was thinking something like the code below (which I put together... but am pretty sure is incorrect).

I know that I can read the first char & I know that I can use a switch statement with char(s) but I'm stuck on how to put the two together.

File file = new File("test.txt");

    // Read the file
    try {
        Scanner scanner = new Scanner(file).useDelimiter(":");
        String line = scanner.nextLine();

        while (scanner.hasNextLine())
        {
            line = line.trim();
            int index; 
            String type;
            String name; 

            String identifier = scanner.next();
            switch(line.charAt(0)) {

            case 'p': 
                index = scanner.nextInt();
                name = scanner.next();      
                break;

            case 'c':
                index = scanner.nextInt();
                type = scanner.next();
                name = scanner.next();
                int partyC = scanner.nextInt();
                int empathyC = scanner.nextInt();
                int carryingCapacityC = scanner.nextInt();
                break;

            case 't':
                index = scanner.nextInt();
                type = scanner.next();
                int creatureT = scanner.nextInt();
                int weightT = scanner.nextInt();
                int valueT = scanner.nextInt();
                break;

            case 'a':
                index = scanner.nextInt();
                type = scanner.next();
                int creatureA = scanner.nextInt();


            }
        } 

    } catch (IOException e) {
        e.printStackTrace();
    }
}

VS.

Alright so I've read-up and played around with this some and think I can just use an if-else loop. However, I'm getting a number of errors (below the code).

        File file = new File("test.txt");

    // Read the file
    try {
        Scanner scanner = new Scanner(file).useDelimiter(":");

        while (scanner.hasNextLine())
        {

            String line = scanner.nextLine();
            line = line.trim();

            int index;
            String type;
            String name; 
            char identifier = line.charAt(0);

            if (identifier == 'p') {
                index = scanner.nextInt();
                name = scanner.next();  

            } else if (identifier == 'c') {
                index = scanner.nextInt();
                type = scanner.next();
                name = scanner.next();
                int partyC = scanner.nextInt();
                int empathyC = scanner.nextInt();
                int carryingCapacityC = scanner.nextInt();

            } else if (identifier == 't') {
                index = scanner.nextInt();
                type = scanner.next();
                int creatureT = scanner.nextInt();
                int weightT = scanner.nextInt();
                int valueT = scanner.nextInt();

            } else if (identifier == 'a') {
                index = scanner.nextInt();
                type = scanner.next();
                int creatureA = scanner.nextInt();

            } else {
                System.out.println("This is not a valid line of input");
            } System.out.println("Identifier: " + identifier);
        } 

    } catch (IOException e) {
        e.printStackTrace();
    }
}
}

Errors:

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 SorcerersCave.main(SorcerersCave.java:33)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

你在看孤独的风景 2025-01-05 17:48:55

我认为你可以逐行读取文件。获取要切换的第一个字符,如下所示:

switch(line.charAt(0)) {

    case 'a':
        //...
    case 'p':
       //...
    //...
}

不要使用分隔符并执行 .next(),而是使用 String[] parts = line.split(":")这给了你字符串数组。并修剪每个元素,对于整数使用 Integer.parseInt(parts[index].trim()) 。

另外,您不需要 while (scanner.hasNext())。相反做

while (scanner.hasNextLine()) {
    String line = scanner.nextLine();
    //split
    //switch

I think you can just read the file line-by-line. Get the first char to switch as follows:

switch(line.charAt(0)) {

    case 'a':
        //...
    case 'p':
       //...
    //...
}

And instead of using a delimiter and doing .next(), use String[] parts = line.split(":") that gives you array of strings. And trim each element and for ints use Integer.parseInt(parts[index].trim()).

Also, you don't need while (scanner.hasNext()). Instead do

while (scanner.hasNextLine()) {
    String line = scanner.nextLine();
    //split
    //switch
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文