比较文本文件中的整数和涉及字符串的程序

时间:2018-04-26 18:09:24

标签: java java.util.scanner bufferedreader

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

示例文本文件:

Name, 8
Name, 1
Name, 4

代码I用于比较:

        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&输入之后)。

3 个答案:

答案 0 :(得分:0)

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

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

@Edit

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

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

} catch (IOException ex) {
    System.err.println("ERROR");
} finally {
    reader.close(); // close stream always in finnaly block or use try with resources!!!
}

答案 1 :(得分:0)

运行以下程序。它会给你想要的输出。请更正其他内容,我只关注输出。

    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);
    }
}

enter image description here

答案 2 :(得分:-1)

请注意,当您阅读整行时,您将获得一个String,其中包含姓名和您想要获得的Integer。我会做以下事情:

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();