2015-12-02 147 views
1

我做了两个类,一个叫做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()); 
     }} 

任何人都可以帮我在这?

+1

'Card [] ArrayOfCards'应该足够了。为什么它必须是2D?或者可能'List '将会更好 – ASh

+0

个人而言,我会在'Card'类上创建一个静态'Parse'方法,该类需要一个'string'并返回一个'Card'或者在'string'没有正确格式化。然后,您只需将代表“Card”的字符串输入到它中。另外我建议使用列表而不是数组。我认为你的潘西帕金森的脾气太低了。 – juharr

+0

为什么不使用序列化方法? –

回答

0

因为你有一个C#标签与你的问题,我决定给一个C#的例子,你应该能够很容易地转换到Java。

基本上我创建了一个Card类,它有一个构造函数接受关于该卡的6个统计信息。我已将这些字段保存为private,因为这是他们在您的问题中的情况。 我创建了一个for循环,它遍历数组中的每个项目,一次有效地取6个项目,并将它们传递给Card构造函数。然后,我们将card添加到我们的ListCards

我还投掷了一个快速的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; 
     } 
    } 
0

也许尝试OD它这样:

与PARAM String数组[] [],并且将数组从文本文件和第二PARAM创建卡,这将是整型数创建构造函数会说在哪里搜索数据构造

构造应该这样工作:

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]; 
} 

那么你可以使用这个构造函数把新卡对象阵列,列表或任何你想要的

0

你应该为你的类卡创建getter/setter,然后你可以使用这个函数。我不知道你使用哪种语言(c#或java),所以你将不得不调整解决方案的波纹管。

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(); 
0

它接缝你输入数组每行一个卡参数。所以我认为你只是在寻找这样的事情:

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; 
} 
0

这可以工作:

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]; 
} 
0

不少答案。这里是另一个。

Your class;

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; 
相关问题