我做了两个类,一个叫做Card,另一个叫Pack。 卡片类具有以下属性:如何将字符串数组作为枚举类型存储
private String Name;
private int Magic;
private int Cunning;
private int Courage;
private int Wisdom;
private int Temper;
在包类,我做了,上面写着我的电脑上的文件,它的每一行存储为一个字符串数组文件阅读器的方法。例如,这是文本的一部分(不是代码):
Pansy_Parkinson
42
21
18
19
9
Dean_Thomas
40
10
35
22
4
My String array []将每行存储为不同的索引。 我想做什么,就是把这个String数组转换成Card类型的数组。 它应该在每个索引中存储一个具有6个属性的卡..
所以我想我需要有一个方法来转换它,我会有这样的基于前面的文本我的新的2D卡阵列:
Card [][] ArrayOfCards = new Card [1][5];
请,任何想法我可以做到这一点?
.............................................. ............ ..................................... .....................
非常感谢大家的宝贵帮助! 我试过所有的代码,他们似乎很棒!但我不知道为什么它会向我显示错误,无论是在我的主程序还是方法本身中。
这是我的FileReader类!
import java.io.File;
import java.util.Arrays;
import java.io.*; //To deal with exceptions
import java.util.*;
import java.util.Scanner;
import java.util.ArrayList;
public class ReadFile {
private Scanner x;
private String Path;
public ReadFile (String ThePath){
Path = ThePath;
}
public String[] openFile() throws IOException /*To throw any errors up the line*/
{
FileReader FR = new FileReader(Path);
BufferedReader TextReader = new BufferedReader(FR);
int NoOfLines = readLines();
String[] TextData = new String[NoOfLines];
for (int i = 0; i < NoOfLines; i++)
TextData[i] = TextReader.readLine(); //Accesses the lines of text and stores them in the array
TextReader.close();
return TextData;
}
int readLines() throws IOException //Return the number of lines in the text
{
FileReader FR2 = new FileReader(Path);
BufferedReader BF = new BufferedReader(FR2);
String ALine;
int NoOfLines = 0;
while ((ALine = BF.readLine()) != null)//Read each line of text & stop when a null value's reached
NoOfLines++;
BF.close();
return NoOfLines;
}
}
而且刚才我看到它尚未在主要的是这样的:
public static void main(String[] args) {
// TODO code application logic here
String FileName = "C:/Users/Anwar/Desktop/Potter.txt"; //Path of the file on my PC
try {
ReadFile File = new ReadFile(FileName);
String[] ArrayLines = File.openFile();
for(int i = 0; i < ArrayLines.length; i++)
System.out.println(ArrayLines[i]);
}
catch (IOException e) /*Defiend object of type IOException*/ {
System.out.println(e.getMessage());
}}
任何人都可以帮我在这?
'Card [] ArrayOfCards'应该足够了。为什么它必须是2D?或者可能'List'将会更好 –
ASh
个人而言,我会在'Card'类上创建一个静态'Parse'方法,该类需要一个'string'并返回一个'Card'或者在'string'没有正确格式化。然后,您只需将代表“Card”的字符串输入到它中。另外我建议使用列表而不是数组。我认为你的潘西帕金森的脾气太低了。 – juharr
为什么不使用序列化方法? –