Java 比较包含字符串的文本文件和程序中的整数

Java 比较包含字符串的文本文件和程序中的整数,java,java.util.scanner,bufferedreader,Java,Java.util.scanner,Bufferedreader,我试图将我的应用程序中的分数与已经保存在单独文本文件中的分数进行比较,但没有成功。当不涉及字符串时,比较分数是很容易的,但是当我保存分数并为其指定名称时,程序无法工作,因为它无法解析字符串和整数 示例文本文件: Name, 8 Name, 1 Name, 4 我用来比较的代码: int highScore = 0; try { BufferedReader reader = new BufferedReader(new FileReader("t

我试图将我的应用程序中的分数与已经保存在单独文本文件中的分数进行比较,但没有成功。当不涉及字符串时,比较分数是很容易的,但是当我保存分数并为其指定名称时,程序无法工作,因为它无法解析字符串和整数

示例文本文件:

Name, 8
Name, 1
Name, 4
我用来比较的代码:

        int highScore = 0;
        try {
        BufferedReader reader = new BufferedReader(new FileReader("txt.txt"));
        String line = reader.readLine();

        while (line != null)                  
        {
            try {
                int score = Integer.parseInt(line.trim());   
                if (score > highScore)                       
                { 
                    highScore = score; 
                }
            } catch (NumberFormatException e1) {
                //ignore invalid scores
                //System.err.println("ignoring invalid score: " + line);
            }
            line = reader.readLine();
        }
        reader.close();

    } catch (IOException ex) {
        System.err.println("ERROR");
    }
代码的其余部分很好,当游戏完成将其与文件中的分数进行比较时,会生成分数,当它读取字符串时,只会生成一个0值,并且不起作用。我不知道如何使用扫描器/分隔符

编辑: 我希望程序执行并显示获得高分的用户的姓名。所以期望的输出是

The all time high score was 8 by Name1

目前它只显示高分(根据Michu93的输入)。

从字符串中删除数字并将其解析为int:

int score = Integer.valueOf(line.replaceAll("[^\\d.]", ""));
@编辑


请注意,当您阅读整行时,您会得到一个
字符串
,其中包含名称和要获取的
整数。我会做以下几件事:

int highScore = 0;
String name = "";
try {
   BufferedReader reader = new BufferedReader(new FileReader("txt.txt"));
   String line = null;
   while ((line = reader.readLine()) != null) {
       try {
           String[] parts = line.split(",");
           if(parts.length > 1){
             int score = Integer.parseInt(parts[1].trim());
             if (score > highScore) {
                highScore = score;
                name = line[0];
             }
           }
       } catch (NumberFormatException e1) {
        //ignore invalid scores
        //System.err.println("ignoring invalid score: " + line);
      }
   }
   System.out.println("The all time high score was %s by %s", highScore, name);
   reader.close();

运行下面的程序。它会给你想要的输出。请纠正其他的事情,我只是专注于输出

    public class Test {
    static int highScore = 0;
    static String highscorer = "";
    public static void main(String[] args) {
        try {
            BufferedReader reader = new BufferedReader(new FileReader("src/com/test/package1/txt.txt"));
            String line = null;
            while ((line = reader.readLine()) != null) {
                try {
                    String[] parts = line.split(",");
                    int tempScore = Integer.parseInt(parts[1].trim());
                    String tempHigScorer = (parts[0]);
                    if (tempScore > highScore) {
                        highScore = tempScore;
                        highscorer = tempHigScorer;
                    }
                } catch (NumberFormatException e1) {
                    // handle NumberFormatException if any
                }
            }
            reader.close();
        } catch (IOException ex) {
            System.err.println("ERROR");
        }
        System.out.println("The all time high score was " + highScore + " by name " + highscorer);
    }
}

谢谢,这很有帮助,但我还想打印出获得高分的球员的姓名。这只打印最高分数。我想我不清楚。@j.hashi21现在检查一下。好吧,我搞错了。如果您还需要名称,那么我将创建一个变量'String name;'当
score
高于
highscore
时,将此名称分配给
name
这会向我抛出一个“java.lang.ArrayIndexOutOfBoundsException:1”错误。@j.hashi21我现在在cel phone,所以我现在无法运行,但可能有一行是空的,或者没有“,”,因此您需要验证“部分”长度,以验证是否可以获取部分[1],如果可能,则将其转换为integerNot,而不使用另一个catch/finally表达式。我刚才添加了IOException,可能是因为我的方法没有任何抛出。我已将我的txt.txt放在src/com/test/p ackage1/txt.txt位置。您需要更改此位置。此解决方案对您有效吗?不,它不断抛出java.lang.ArrayIndexOutOfBoundsException:1错误:/可能是因为我在另一个类中运行main方法?它调用这个方法并执行它。你的文本文件是否包含格式名为8的数据,然后是下一行。
    public class Test {
    static int highScore = 0;
    static String highscorer = "";
    public static void main(String[] args) {
        try {
            BufferedReader reader = new BufferedReader(new FileReader("src/com/test/package1/txt.txt"));
            String line = null;
            while ((line = reader.readLine()) != null) {
                try {
                    String[] parts = line.split(",");
                    int tempScore = Integer.parseInt(parts[1].trim());
                    String tempHigScorer = (parts[0]);
                    if (tempScore > highScore) {
                        highScore = tempScore;
                        highscorer = tempHigScorer;
                    }
                } catch (NumberFormatException e1) {
                    // handle NumberFormatException if any
                }
            }
            reader.close();
        } catch (IOException ex) {
            System.err.println("ERROR");
        }
        System.out.println("The all time high score was " + highScore + " by name " + highscorer);
    }
}