gpt4 book ai didi

java -/ArrayList 的文件写入问题

转载 作者:行者123 更新时间:2023-12-01 14:37:05 25 4
gpt4 key购买 nike

编辑:程序现在仅写入第一个现有分数,如下所示:
文件大小(以行为单位)-
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 并执行文件操作)。评论应该解释一切;我可以根据需要提供更多代码。任何和所有的帮助将不胜感激。

最佳答案

  • IndexOutOfBoundsExceptions 于:

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

    表示 tempstr.split(strpos) 返回的数组少于 2 个项目(0 或 1)。在那里添加打印语句或逐步调试程序以找出原因应该相当容易。

  • 无法正确写入文件

    这有点模糊(您是否遇到异常?文件已创建但无效?等等)

关于java -/ArrayList 的文件写入问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16389003/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com