编辑:程序现在只编写第一个现有分数,如下:
文件大小(以行为单位) -
0-正常写入。
1-提示输入首字母,输出显示用户分数已添加到myscores,但第一个分数是添加到文件中的分数。
2 + - 与第二个类似,没有提示首字母,第一个分数被写入文件两次。
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
}
}
}
未显示的类是Score(得分对象模板),Encrypter(数组生成和难度选择),BubbleSort(冒泡排序算法)和DSApp(应用程序文件,绘制GUI并执行文件操作)。评论应该解释一切;我可以根据需要提供更多代码。任何和所有的帮助将不胜感激。
答案 0 :(得分:1)
IndexOutOfBoundsExceptions
on:
temp.myscore = Integer.parseInt((tempstr.split(strpos)[1]));
表示tempstr.split(strpos)
返回的数组少于2个项(0或1)。在那里添加print语句或逐步调试程序以找出原因应该相当容易。
无法正确写入文件
这有点模糊(你得到一个例外吗?文件已创建但无效?等等)