2016-10-02 126 views
0

我在使用java中的所有FileReader和FileWriters時遇到了一些問題。我希望我的程序能夠得到這個人的名字,然後在詢問他們幾個問題後評估他們的分數。這是很容易的,但現在我想要做的就是記錄它在這樣的日誌文件:每次這些人打開程序會檢索其當前得分對java中的文件進行操作

Jacob : 10 
Mark : 15 
Steve : 7 

然後,或者如果它是一個新的它會追加他們的名字和分數。 我無法找到搜索文件的方法,然後在最後檢索整數。

編輯:

import java.io.FileInputStream; 
import java.io.FileOutputStream; 
import java.io.ObjectInputStream; 
import java.io.ObjectOutputStream; 
import java.util.Scanner; 

public class Main { 
    static Scores scoreClass = new Scores(); 

    private static void setupMap() { 
     try { 
      FileInputStream fin = new FileInputStream("scores.map"); 
      @SuppressWarnings("resource") 
      ObjectInputStream ois = new ObjectInputStream(fin); 
      scoreClass = (Scores) ois.readObject(); 
      } catch (Exception e) { 
       System.err.println("Could not load score data"); 
       e.printStackTrace(); 
      } 
    } 
    private static void saveMap() { 
     try { 
      FileOutputStream fout = new FileOutputStream("scores.map"); 
      @SuppressWarnings("resource") 
      ObjectOutputStream oos = new ObjectOutputStream(fout); 
      oos.writeObject(scoreClass); 
     } catch (Exception e) { 
      System.err.println("Could not save score data"); 
      e.printStackTrace(); 
     } 
    } 

    public static void main(String[] args) { 
     setupMap(); 
     int score = 0; 
     String guess; 
     String[][] questions_with_answers = {{"I __________ to school.(go)", "She __________ tennis. (play)", "She _______ a bitch. (be)"}, {"am going", "was playing", "is being"}}; 
     Scanner answer = new Scanner(System.in); 
     Scanner studentInput = new Scanner(System.in); 
     System.out.println("What is your name?"); 
     String name = answer.nextLine(); 
     if(Scores.getScore(name) == -1){ 
      Scores.setScore(name, score); 
     } else { 
      Scores.getScore(name); 
     } 

     System.out.println("Hello " + name + ". Your score is: " + score); 
     for(int i = 0; i < (questions_with_answers[0].length); i++){ 
      System.out.println(questions_with_answers[0][i]); 
      guess = studentInput.nextLine(); 
      if(guess.equals(questions_with_answers[1][i])){ 
       System.out.println("Correct! You get 1 point."); 
       score = score + 1; 
       System.out.println("Your score is " + score); 
      } else { 
       System.out.println("You made a mistake! No points..."); 
      } 
     } 
     System.out.printf("Congrats! You finished the test with "+ score +" points out of " + questions_with_answers[0].length);  
     Scores.setScore(name, score); 
     saveMap(); 
     answer.close(); 
     studentInput.close(); 
    } 
} 

這不會引發任何異常,但仍然沒有從文件中讀取輸入:/

+3

那你試試這麼遠嗎?向我們展示一些代碼 – nachokk

+0

您可以在每行中使用'String [] array = line.split(「:」);'這將返回一個數組,在您的情況下,如果它被正確保存,數組的大小應該是2 。然後,只需使用'int score = Integer.valueOf(array [1])' – nachokk

回答

0

您可以將數據存儲爲一個哈希表,然後序列化到一個保存文件

我寫了一個示例,其中包含一個名爲Scores的類,該類具有讀取分數並將分數添加到包含的散列圖的方法。我還在主類中添加了保存和打開包含分數類數據的文件的方法。

主類:

public class Main { 
    static Scores scoreClass = new Scores(); 

    public static void main(String[] args) { 
     setupMap(); 
     //Do score calculations 
     saveMap(); 

    } 
    private static void setupMap() { 
     try { 
      FileInputStream fin = new FileInputStream("scores.map"); 
      @SuppressWarnings("resource") 
      ObjectInputStream ois = new ObjectInputStream(fin); 
      scoreClass = (Scores) ois.readObject(); 
      } catch (Exception e) { 
       System.err.println("Could not load score data"); 
       e.printStackTrace(); 
      } 
    } 
    private static void saveMap() { 
     try { 
      FileOutputStream fout = new FileOutputStream("scores.map"); 
      @SuppressWarnings("resource") 
      ObjectOutputStream oos = new ObjectOutputStream(fout); 
      oos.writeObject(scoreClass); 
     } catch (Exception e) { 
      System.err.println("Could not save score data"); 
      e.printStackTrace(); 
     } 
    } 

} 

成績等級:

public class Scores implements Serializable{ 
    private static final long serialVersionUID = 1L; 

    public static Map<String, Integer> scoreMap = new HashMap<String, Integer>(); 

    public static int getScore(String name) { 
     if (scoreMap.containsKey(name)) { 
      return scoreMap.get(name); 
     } else { 
      return -1; 
     } 
    } 

    public static void setScore(String name, int score) { 
     scoreMap.put(name, score); 
    } 
} 
+0

非常感謝幫助人:)仍然由於某種原因得分和名稱不從文件讀取:/任何想法爲什麼那可能是? –

+0

使用[java.util.Properties](https://docs.oracle.com/javase/8/docs/api/java/util/Properties.html)類將負責爲您進行序列化:-) – Selim