ArrayList打印值丢失了吗?

时间:2016-05-05 22:06:20

标签: java arraylist

  1. 以这种格式输入游戏时:

    测试:120:120
    游戏:120:120
    退出

  2. 我的txt文件只显示我的第一个条目,而不是我的第二个条目,在我的文本文件中显示如下:

    玩家:亚当

    游戏:测试,得分= 120,分钟= 120

  3. 但我希望它显示:

    游戏:测试,得分= 120,出场时间= 120
    游戏:feff,得分= 3412,分钟= 434

  4. 到目前为止,我的代码是:

    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.PrintWriter;
    import java.util.ArrayList;
    import java.util.List;
    import java.util.Scanner;
    
    public class Generator {
       private static char[] input;
    
       public static void main(String[] args) {
           int[] minutesPlayed = new int [100];
           String gamerName, gamerReport;
           String[] gameNames = new String[100];
           int[] highScores = new int[100];
           @SuppressWarnings("resource")
           Scanner Scan = new Scanner(System.in);
    
           System.out.println("-------------- Game Score Report Generator --------------");
           System.out.println("     ");
           System.out.print("Enter your Name.");
           System.out.println("   ");
           gamerName = Scan.nextLine();
    
           boolean isEmpty = gamerName == null || gamerName.trim().length() == 0;
           if (isEmpty) {
               System.out.print("Enter your Name.");
               gamerName = Scan.nextLine();
           }
    
           System.out.println("Enter details in this format - " + " --> Game : Achievement Score : Minutes Played");
           System.out.println("    ");
           System.out.println("Game : Achievement Score : Minutes Played");
    
           gamerReport = Scan.nextLine();
           Scanner scanner = new Scanner(System.in);     
           List<String> al = new ArrayList<String>();    
           String word;                                  
           while (scanner.hasNextLine()) {               
             word = scanner.nextLine();                  
             if (word != null) {                        
               word = word.trim();                      
               if (word.equalsIgnoreCase("quit")) {      
                 break;                                  
               }
               al.add(word);                             
    
             } else {
               break;                                   
             }
           }
    
           String[] splitUpReport; 
           splitUpReport = gamerReport.split(":"); 
           int i = 0;
    
           gameNames[i] = splitUpReport[0];
           highScores[i] = Integer.parseInt(splitUpReport[1].trim() );
           minutesPlayed[i] = Integer.parseInt(splitUpReport[2].trim());
    
           try
          {
             PrintWriter writer = new PrintWriter(new FileOutputStream("Gaming Report Data.txt", true));
             writer.println("Player : " + gamerName);
             writer.println();
             writer.println("--------------------------------");
             writer.println();
             String[] report = gamerReport.split(":");
             writer.println("Game: " + report[0] + ", score= " +report[1] + ", minutes played= " +report[2]);
             writer.close();
    
         } catch (IOException e)
         {
           System.err.println("File does not exist!");
         }    
       }                   
    public static char[] getInput() {
       return input;
    }
    }
    

1 个答案:

答案 0 :(得分:0)

您的第一个得分输入存储在名为gamerReport的变量中,但之后输入的每个得分都会添加到名为al的ArrayList中。在代码的最后,您只从gamerReport变量中提取数据(仅包含第一个分数)。

您可能希望重写分数输入代码,以便在循环期间将所有代码添加到al ArrayList,然后让文本输出部分迭代al

捕获和存储输入的示例代码

Scanner scanner = new Scanner(System.in);
ArrayList<String> al = new ArrayList<>();
String word = "";

while (true) {
    word = scanner.nextLine();
    if (word != null) word = word.trim();
    if (word.equalsIgnoreCase("quit")) break;
    al.add(word);
}

System.out.println(al);

此代码解决了将数据存储在两个不同变量中的问题(它现在全部存储在al中,而gamerReport已被删除,因为它不需要)。要稍后将其打印到文件中,您应该对其进行迭代(使用类似for (String str : al) { ... }的内容)。

请注意,此代码根本不是用户证明的。查看正则表达式以防止用户输入无效数据可能是明智的。