Java 如何将字符串数组存储为枚举类型
我上过两门课,一门叫卡片,另一门叫包装。 卡片类具有以下属性:Java 如何将字符串数组存储为枚举类型,java,arrays,type-conversion,readfile,Java,Arrays,Type Conversion,Readfile,我上过两门课,一门叫卡片,另一门叫包装。 卡片类具有以下属性: private String Name; private int Magic; private int Cunning; private int Courage; private int Wisdom; private int Temper; 在Pack类中,我创建了一个文件读取器方法,该方法在我的PC上读取一个文件,并将其每一行存储为字符串数组。例如,这是文本的一部分(不是代码): 我的字符串数组[]将每一行存储为不同的索引。
private String Name;
private int Magic;
private int Cunning;
private int Courage;
private int Wisdom;
private int Temper;
在Pack类中,我创建了一个文件读取器方法,该方法在我的PC上读取一个文件,并将其每一行存储为字符串数组。例如,这是文本的一部分(不是代码):
我的字符串数组[]将每一行存储为不同的索引。
我想做的是将这个字符串数组转换为类型卡的数组。
它应该在每个索引中存储一张具有6个属性的卡片
因此,我想我需要有一个方法来转换它,我将有一个基于前面文本的新2D卡阵列,如下所示:
Card [][] ArrayOfCards = new Card [1][5];
拜托,你知道我该怎么做吗
。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。
非常感谢大家的宝贵帮助!
我尝试了所有的代码,它们看起来很棒!但是我不知道为什么我的main或者方法本身会出现错误
这是我的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;
}
}
导入java.io.File;
导入java.util.array;
导入java.io.*//处理例外情况
导入java.util.*;
导入java.util.Scanner;
导入java.util.ArrayList;
公共类读取文件{
专用扫描仪x;
私有字符串路径;
公共读取文件(字符串路径){
路径=路径;
}
公共字符串[]openFile()抛出IOException/*以抛出任何错误*/
{
FileReader FR=新的FileReader(路径);
BufferedReader TextReader=新的BufferedReader(FR);
int NoOfLines=readLines();
String[]TextData=新字符串[NoOfLines];
for(int i=0;i
我刚刚在main上读到了如下内容:
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());
}}
publicstaticvoidmain(字符串[]args){
//此处的TODO代码应用程序逻辑
String FileName=“C:/Users/Anwar/Desktop/Potter.txt”;//我的电脑上文件的路径
试一试{
ReadFile File=新的ReadFile(文件名);
字符串[]ArrayLines=File.openFile();
对于(int i=0;i
任何人都可以在这方面帮助我?因为你的问题带有一个c标记,所以我决定给出一个c示例,你应该能够相当容易地将其转换为java
本质上,我创建了一个Card
类,该类有一个构造函数,接受关于该卡的6个统计信息。我将字段保留为私有
,因为在您的问题中它们就是这样的。
我创建了一个for
循环,该循环通过数组中的每个项运行,有效地一次获取6个项,并将它们传递给卡
构造函数。然后,我们将此卡添加到卡的列表中
我还加入了一个快速的ToInt()
扩展来整理Card
实例化
有更好的方法可以做到这一点,但这解决了您向我们提出的问题。本例中也没有错误处理
class Program
{
static void Main(string[] args)
{
var rawData = new string[]{
"Pansy_Parkinson",
"42",
"21",
"18",
"19",
"9",
"Dean_Thomas",
"40",
"10",
"35",
"22",
"4"
};
var Cards = new List<Card>();
for(int i = 0; i < rawData.Length; i+=6)
{
var card = new Card(rawData[0 + i], rawData[1 + i].ToInt(), rawData[2 + i].ToInt(), rawData[3 + i].ToInt(), rawData[4 + i].ToInt(), rawData[5 + i].ToInt());
Cards.Add(card);
}
}
}
public static class StringExtensions
{
public static int ToInt(this string item)
{
return Convert.ToInt32(item);
}
}
public class Card
{
private string Name;
private int Magic;
private int Cunning;
private int Courage;
private int Wisdom;
private int Temper;
public Card(string name, int magic, int cunning, int courage, int wisdom, int temper)
{
Name = name;
Magic = magic;
Cunning = cunning;
Courage = courage;
Wisdom = wisdom;
Temper = temper;
}
}
类程序
{
静态void Main(字符串[]参数)
{
var rawData=新字符串[]{
“潘西·帕金森”,
"42",
"21",
"18",
"19",
"9",
“迪安·托马斯”,
"40",
"10",
"35",
"22",
"4"
};
var Cards=新列表();
对于(int i=0;i
因为你的问题带有一个c标记,所以我决定给出一个c示例,你应该能够很容易地将其转换为java
本质上,我创建了一个Card
类,该类有一个构造函数,接受关于该卡的6个统计信息。我将字段保留为私有
,因为在您的问题中它们就是这样的。
我创建了一个for
循环,该循环通过数组中的每个项运行,有效地一次获取6个项,并将它们传递给卡
构造函数。然后我们将添加此
class Program
{
static void Main(string[] args)
{
var rawData = new string[]{
"Pansy_Parkinson",
"42",
"21",
"18",
"19",
"9",
"Dean_Thomas",
"40",
"10",
"35",
"22",
"4"
};
var Cards = new List<Card>();
for(int i = 0; i < rawData.Length; i+=6)
{
var card = new Card(rawData[0 + i], rawData[1 + i].ToInt(), rawData[2 + i].ToInt(), rawData[3 + i].ToInt(), rawData[4 + i].ToInt(), rawData[5 + i].ToInt());
Cards.Add(card);
}
}
}
public static class StringExtensions
{
public static int ToInt(this string item)
{
return Convert.ToInt32(item);
}
}
public class Card
{
private string Name;
private int Magic;
private int Cunning;
private int Courage;
private int Wisdom;
private int Temper;
public Card(string name, int magic, int cunning, int courage, int wisdom, int temper)
{
Name = name;
Magic = magic;
Cunning = cunning;
Courage = courage;
Wisdom = wisdom;
Temper = temper;
}
}
Card(String[][] array, int numberForConstructor){
this.name = array[numberForConstructor][0];
this.magic = array[numberForConstructor][1];
this.cunning = array[numberForConstructor][2];
this.courage = array[numberForConstructor][3];
this.temper = array[numberForConstructor][4];
}
public class Pack
{
public List<Card> cards = new List<Card>();
public Pack(string filename)
{
// Your parsing function
while ((line = readLine() != null)
{
Card c = new Card();
c.setName(line);
c.setMagic(Convert.ToInt32(readLine());
c.setCunning(Convert.ToInt32(readLine());
c.setCourage(Convert.ToInt32(readLine());
c.setWisdom(Convert.ToInt32(readLine());
c.setTemper(Convert.ToInt32(readLine());
cards.add(c);
}
}
}
Pack p = new Pack("cards.txt");
p.cards[0].getName();
public static Card[] convert(String[] arr){
if(arr.length % 6 != 0){
System.out.println("error in data");
return null;
}
Card[] res = new Card[arr.length / 6];
for(int i = 0; i < res.length; i++){
String Name = arr[(i * 6) + 0];
int Magic = Integer.parseInt(arr[(i * 6) + 1]);
int Cunning = Integer.parseInt(arr[(i * 6) + 2]);
int Courage = Integer.parseInt(arr[(i * 6) + 3]);
int Wisdom = Integer.parseInt(arr[(i * 6) + 4]);
int Temper = Integer.parseInt(arr[(i * 6) + 5]);
res[i] = new Card(Name, Magic, Cunning, Courage, Wisdom, Temper);
}
return res;
}
public Card ConvertArrayToCard(string[] array)
{
Card card = new Card;
card.Name = array[0];
card.Magic= array[1];
card.Cunning= array[2];
card.Courage= array[3];
card.Wisdom= array[4];
card.Temper= array[5];
}
public class Pack
{
public string Name { get; set; }
public int Magic { get; set; }
public int Cunning { get; set; }
public int Courage { get; set; }
public int Wisdom { get; set; }
public int Temper { get; set; }
}
var lines = File.ReadAllLines(@"C:\temp2.txt");
List<Pack> mypack = new List<Pack>();
Pack member = null;
int count = 0;
foreach (var line in lines)
{
int val;
count = (!int.TryParse(line, out val)) ? 0 : count + 1;
switch (count)
{
case 0:
member = new Pack() { Name = line.Trim() };
break;
case 1:
member.Magic = val;
break;
case 2:
member.Cunning = val;
break;
case 3:
member.Courage = val;
break;
case 4:
member.Wisdom = val;
break;
case 5:
member.Temper = val;
mypack.Add(member);
break;
}
}
return mypack;