2013-05-05 81 views
0

编辑:计划现在只写了第一个现有的评分,如下:
文件大小(行) -
0-写入正常。
1-提示首字母缩写,输出显示用户分数已添加到myscore,但第一个分数是添加到文件中的分数。
2 + - 类似于第二,没有提示首字母,第一个分数被写入文件两次。文件写入问题瓦特/ ArrayList的

import java.io.*; 
import java.util.*; 
import java.nio.file.*; 
import java.text.*; 

/* Notes: 
* High Scores Class: 
* Used to read/write high scores text file 
* */ 

/* 
* To do: 
* Fix reading/writing file 
* Fix sort as needed 
* FILTER OUT WHITESPACE WHEN READING 
*/ 


public class HighScores 
{ 
    //user will input their 3 initials, must be 3 
    public ArrayList<String> filearr;//hold file contents 
    public BufferedReader hsds;//read initials in 
    public ArrayList<Score> myscores;//hold all existing scores 
    public ArrayList<String> vals;//hold filearr contents to be split and parsed 
    public HighScores() //constructor 
    { 
     myscores = new ArrayList<Score>(); 
     vals = new ArrayList<String>(); 
     hsds = new BufferedReader(new InputStreamReader(System.in));//for user input 
     filearr = new ArrayList<String>();//holds values to be written to file 
     System.out.println("File created."); 
    } 

    public void populate(Score uscore) 
    //argument is score generated by user 
    //this is called after game finishes 
    //handles score(write or no? 
    { 
    Integer thescore = uscore.myscore; 
    switch (filearr.size()) 
    { 
    case 2://doesnt prompt for initials, writes first score twice each on a new line 
    case 3: 
    case 4: 
    case 5: 
    case 6: 
    case 7: 
    case 8: 
    case 9: 
    case 10: 
     String strpos = "" + ((vals.get(0)).charAt(3));//remember strings are arrays! 
     for (int i = 0; i < vals.size(); i++) 
     { 
      Score temp = new Score(); 
      String tempstr = vals.get(i); 
      temp.initials = (tempstr.split(strpos)[0]); 
      temp.myscore = Integer.parseInt((tempstr.split(strpos)[2])); 
      myscores.add(temp); 
     } 
     int ssize = myscores.size(); 
     BubbleSort bsort = new BubbleSort(); 
     bsort.bubbleSort(myscores, ssize);//ssize is current size of scores 
     System.out.println("Unsorted scores"); 
     for(int i = 0; i < myscores.size(); i++) 
     { 
      System.out.println("Score " + i + " : "); 
      System.out.println("inits: " + (myscores.get(i).initials)); 
      System.out.println("myscore: " + ("" +(myscores.get(i).myscore))); 
     } 
    int max = myscores.size(); 
    int y = 0; 
    Score sscore = new Score(); 
    sscore.myscore = thescore; 

     if(sscore.myscore > (myscores.get(y)).myscore)//sorting algorithm 
     { 
      try 
      { 
      System.out.println("Please enter 3 initials to identify your high score."); 
      String tinitials = "" + ((hsds.readLine()).toUpperCase()); 
      //initials are always formatted as upper case 
       if (tinitials.length() > 3) 
       { 
        String temp = tinitials.split(strpos)[0]; 
        sscore.initials = temp; 
       } 
       else 
       { 
        sscore.initials = tinitials; 
       } 
      hsds.close(); 
      } 
      catch (Exception e) 
      { 
       e.printStackTrace(); 
      } 
      if(sscore.myscore < (myscores.get(max - 1)).myscore)//smaller than largest 
      { 
       System.out.println("bigger, sm max"); 
       myscores.set(y, sscore);//in at 0 
       y++;//now 1 
       while ((myscores.get(y)).myscore < sscore.myscore) 
       { 
        myscores.set(y-1, myscores.get(y));//shift this one down 
        myscores.set(y, sscore);//fill the hole 
        y++;//ends up at 5 
       } 
      } 

      else if(sscore.myscore > (myscores.get(max-1)).myscore)//bigger than largest 
      { 
       System.out.println("bigger, gr max"); 
       Score temp = (myscores.get(max-1)); 
       myscores.set(max-1, sscore);//in at end 
       y++; 
       while(y < (max-1)) 
       { 
        myscores.set(y-1, myscores.get(y));//shift this one down 
        myscores.set(y, myscores.get(y+1));//fill the hole 
        y++;//ends up at 5 
       } 
       myscores.set(max-2, temp); 
      } 

      else if((sscore.myscore).equals((myscores.get(max-1)).myscore))//equal to largest 
      { 
       System.out.println("bigger, eq max"); 
       y++;//now 1 
       while ((myscores.get(y)).myscore < sscore.myscore) 
       { 
        myscores.set(y-1, myscores.get(y)); 
        myscores.set(y, sscore); 
        y++; 
       } 
       myscores.set(y-1, sscore); 
      } 

      else 
      { 
       System.out.println("Oops."); 
      } 
     } 
     else//smaller than first element 
     { 
      System.out.println("too small"); 
     }//end sort 
     System.out.println("Sorted scores"); 
     for(int i = 0; i < myscores.size(); i++) 
     { 
      System.out.println("Score " + i + " : "); 
      System.out.println("inits: " + (myscores.get(i).initials)); 
      System.out.println("myscore: " + ("" +(myscores.get(i).myscore))); 
     } 
    break; 

    case 0://writes first score once 
     Score newscore = new Score(); 
     newscore.myscore = thescore; 
     try 
     { 
     System.out.println("Please enter 3 initials to identify your high score."); 
     String tinitials = "" + ((hsds.readLine()).toUpperCase()); 
     //initials are always formatted as upper case 
      if (tinitials.length() > 3) 
      { 
       strpos = "" + (tinitials.charAt(3)); 
       String temp = tinitials.split(strpos)[0]; 
       newscore.initials = temp; 
      } 
      else 
      { 
       newscore.initials = tinitials; 
      } 

     } 
     catch (Exception e) 
     { 
      e.printStackTrace(); 
     } 
     myscores.add(newscore); 
    break; 

    case 1://writes first score again on separate line 
    newscore = new Score(); 
    newscore.myscore = thescore; 
    strpos = "" + ((vals.get(0)).charAt(3));//remember strings are arrays! 
    try 
    { 
    System.out.println("Please enter 3 initials to identify your high score."); 
    String tinitials = "" + ((hsds.readLine()).toUpperCase()); 
    //initials are always formatted as upper case 
     if (tinitials.length() > 3) 
     { 
      strpos = "" + (tinitials.charAt(3)); 
      String temp = tinitials.split(strpos)[0]; 
      newscore.initials = temp; 
     } 
     else 
     { 
      newscore.initials = tinitials; 
     } 

    } 
    catch (Exception e) 
    { 
     e.printStackTrace(); 
    } 
    for (int i = 0; i < vals.size(); i++) 
    { 
     Score temp = new Score(); 
     String tempstr = vals.get(i); 
     temp.initials = (tempstr.split(strpos)[0]); 
     temp.myscore = Integer.parseInt((tempstr.split(strpos)[2]));//works, idk why 
     myscores.add(temp); 
    } 
    bsort = new BubbleSort(); 
    if (newscore.myscore > (myscores.get(0)).myscore) 
    { 
     myscores.add(newscore); 
    } 
    else 
    { 
     bsort.bubbleSort(myscores, myscores.size()); 
    } 
    break; 

    default: 
    System.out.println("Invalid file supplied."); 
    break; 

    } 

    } 

    public void Save(PrintWriter writeme)//write everything to a file, filearr should be 
    //the same size as it was upon object construction 
    { 
     for (int i = 0; i < myscores.size(); i++) 
     { 
      filearr.add(myscores.get(i).initials + " " + ("" + (myscores.get(i).myscore))); //copy the finished list over 
      writeme.println(filearr.get(i)); //write the list to the high scores file 
      //writeme is myout in dsapp.java 
     } 
    } 
} 

未示出的类是得分(分数对象模板),加密器(排列生成和困难选择),冒泡(冒泡排序算法),和DSApp(应用程序文件,绘制GUI并执行文件操作)。评论应该解释一切;我可以根据需要提供更多的代码。任何和所有的帮助将不胜感激。

回答

1
  • IndexOutOfBoundsExceptions上:

    temp.myscore = Integer.parseInt((tempstr.split(strpos)[1])); 
    

    意味着由tempstr.split(strpos)返回的数组具有小于2项(0或1)。在那里添加打印语句或逐步调试程序以找出原因应该相当容易。

  • 不能正确地写入到文件

    这是一个有点模糊(你会得到一个异常?在文件被创建,但无效?等等)