简体   繁体   English

文本文件转换为arraylist的问题

[英]Issue with text file conversion to arraylist

I apologize in advance if the solution is relatively obvious; 如果解决方案相对明显,我先向您道歉; however, the AP compsci curriculum at my highschool included practically no involvement with IO or file components. 但是,我高中的AP Compsci课程实际上不涉及IO或文件组件。 I've been trying to write an elementary flashcard program - thus it would be much more practical to read strings off a text file than add 100 objects to an array. 我一直在尝试编写一个基本的抽认卡程序-因此,从文本文件中读取字符串比向数组中添加100个对象要实际得多。 My issue is that when I go to check the size and contents of the ArrayList at the end, it's empty. 我的问题是,当我最后检查ArrayList的大小和内容时,它为空。 My source code is as follows: 我的源代码如下:

public class IOReader
{
  static ArrayList<FlashCard> cards = new ArrayList<FlashCard>();
  static File file = new File("temp.txt");

  public static void fillArray() throws FileNotFoundException, IOException
  {
    FileInputStream fiStream = new FileInputStream(file);
    if(file.exists())
    {
      try( BufferedReader br = new BufferedReader( new InputStreamReader(fiStream) )
      {
        String line;
        String[] seperated;
        while( (line = br.readLine()) != null)
        {
          try
          {
            seperated = line.split(":");
            String foreign = seperated[0];
            String english = seperated[1];
            cards.add( new FlashCard(foreign, english) );
            System.out.println(foreign + " : " + english);
          }
          catch(NumberFormatException | NullPointerException | ArrayIndexOutOfBoundsException e)
          {
            e.printStackTrace();
          }
          finally{ 
            br.close(); 
          }
        }
      }
    }
    else{
      System.err.print("File not found");
      throw new FileNotFoundException();
    }
  }
  public static void main(String[] args)
  {
    try{
      fillArray();
    }
    catch (Exception e){}
    for(FlashCard card: cards)
      System.out.println( card.toString() );
    System.out.print( cards.size() );
  }
}

My text file looks as thus: 我的文本文件如下所示:

Volare : To Fly
Velle : To Wish
Facere : To Do / Make
Trahere : To Spin / Drag
Odisse : To Hate
... et alia

My FlashCard class is very simplistic; 我的FlashCard类非常简单; it merely takes two Strings as parameter. 它只需要两个字符串作为参数。 The issue though is that the output whenever I run this is that nothing is printed except for the 0 printed in the main method, indicating that the ArrayList is empty. 但是问题是,每当我运行此命令时,输出的内容就是除main方法中打印的0以外,什么都没有打印,这表明ArrayList为空。 I thank you in advance for any help, as any would be appreciated. 在此先感谢您的帮助,我们将不胜感激。

Some point to consider: 需要考虑的几点:

  1. in your fillArray() is good to throws exceptions and caught them inside agent portion of your program which is main() , so your code in fillArray() will be more readable and you will not hide the exceptions. fillArray()中抛出异常可以很好地引发异常并将它们捕获在程序的代理部分main() ,因此fillArray()代码将更具可读性,并且不会隐藏异常。
  2. I do not think there is any need to check whether the file exist because if it does not exist, the exception will be throw and main() function will be use it. 我认为没有必要检查文件是否存在,因为如果文件不存在,则会引发异常,并且main()函数将使用该文件。
  3. I use Igal class instead of FlashCard class which is as same as your FlashCard class 我使用Igal类而不是FlashCard类,该类与您的FlashCard类相同

Code for Igal Class: Igal类代码:

public class Igal {
    private String st1;
    private String st2;

public Igal(String s1, String s2){
    st1 = s1;
    st2 = s2;
}



/**
 * @return the st1
 */
public String getSt1() {
    return st1;
}

/**
 * @param st1 the st1 to set
 */
public void setSt1(String st1) {
    this.st1 = st1;
}

/**
 * @return the st2
 */
public String getSt2() {
    return st2;
}

/**
 * @param st2 the st2 to set
 */
public void setSt2(String st2) {
    this.st2 = st2;
}

@Override
public String toString(){
    return getSt1() + " " + getSt2();
}

} }

Code: 码:

static List<Igal> cards = new ArrayList<>();
static File file = new File("C:\\Users\\xxx\\Documents\\NetBeansProjects\\Dictionary\\src\\temp.txt");

public static void fillArray() throws FileNotFoundException, IOException {

        FileInputStream fiStream = new FileInputStream(file);
        BufferedReader br = new BufferedReader(new InputStreamReader(fiStream));
        String line;
        String[] seperated;

        while ((line = br.readLine()) != null) {
            seperated = line.split(":");
            String foreign = seperated[0];
            String english = seperated[1];
            System.out.println(foreign + " : " + english);
           Igal fc = new Igal(foreign, english);
           cards.add(fc);
      }
 }

public static void main(String[] args) {
    try {
        fillArray();
    } catch (IOException e) {
        System.out.println(e);
    }
    System.out.println("------------------");
    for (Igal card : cards) {
        System.out.println(card.toString());
    }
     System.out.print("the size is " + cards.size()+"\n");

temp.txt content are as follows temp.txt内容如下

Volare : To Fly
Velle : To Wish
Facere : To Do / Make
Trahere : To Spin / Drag
Odisse : To Hate

output: 输出:

------------------
Volare   To Fly
Velle   To Wish
Facere   To Do / Make
Trahere   To Spin / Drag
Odisse   To Hate
the size is 5

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM