简体   繁体   English

Java-读取文本文件,将内容存储在ArrayList中,打印ArrayList

[英]Java - Read Text File, Store Contents in ArrayList, Print ArrayList

I have to write a program that: 我必须编写一个程序:

  1. prompts the user for the name of the input file, 提示用户输入文件的名称,
  2. reads the data from the input file and store information about the cars in an array list of Car objects, 从输入文件中读取数据,并将有关汽车的信息存储在Car对象的数组列表中,
  3. prints the unsorted data from the array list to the screen, as shown below: 将数组列表中未排序的数据打印到屏幕上,如下所示:
Enter the name of the input file -> carlot.txt

The Unsorted Array List of Cars

[Honda, Prelude, 1998] 
[Honda, Accord, 1998]
[Honda, Ridgeline, 2006]
[Ford, Taurus, 1996]
[Mitsubishi, Eclipse, 1996]
[Mitsubishi, Galant, 2015]
[Ford, Fusion, 2010]
[Mazda, Protege 5, 2003]
[Mazda, Protege 5, 2002]
[Isuzu, Trooper, 2002]

I cannot figure out how to do these first three steps. 我无法弄清楚如何执行前三个步骤。

My code: 我的代码:

package carsorter;
import java.util.*;
import java.io.*;

public class CarSorter 
{
    public static void main(String[] args) 
    {
        try 
        {
            String userFileName;
            Scanner in = new Scanner(System.in);
            System.out.print("Enter the name of the input file - > ");
            userFileName = in.next();
            System.out.println();

            Scanner file = new Scanner(new File(userFileName));
            ArrayList<Car> list1 = new ArrayList<>();
            while (file.hasNext()) 
            {
                list1.add(new Car(file.nextInt(), file.next(), file.next()));
            }
            Car[] list2 = list1.toArray(new Car[list1.size()]);
            System.out.println("The Unsorted Array List of Cars");
            int i;
            for (i=0; i < list1.size(); i++) 
            {
                System.out.println(list1.get(i));
            }
            System.out.println();

        } 
        catch (IOException e) 
        {
            System.out.println(e);
        }
    }

}

It gives me the file not found exception when I compile the program even though I just created the file and saved it to my computer. 即使我刚创建文件并将其保存到计算机中,它也为我提供了在编译程序时找不到文件的异常。

I also have two classes that go with this project and will add them if necessary/wanted. 我也有两个与此项目一起使用的类,如果需要/需要,将添加它们。 So basically just need help figuring out how to read a text file from the user, add the contents of the text file to an ArrayList, and print out the contents of the ArrayList in a formatted way (I have a toString() method in one of my classes). 所以基本上只需要帮助弄清楚如何从用户读取文本文件,将文本文件的内容添加到ArrayList并以格式化的方式打印出ArrayList的内容(我有一个toString()方法我的课程)。

I suppose that you only enter the name of the file you want to read (Ej. foo.txt). 我想您只输入要读取的文件的名称 (Ej。foo.txt)。 If that is the case then you shouldn't pass it as is to the new File() constructor since that constructor expects an absolute path (or a path relative to the JVM being executed) so you should enter the full absolute file path following the file name in your prompt (Ej. /home/user/foo.txt or c:\\documents\\foo.txt). 如果是这种情况,则您不应按原样将其传递给new File()构造函数,因为该构造函数需要一个绝对路径(或相对于正在执行的JVM的路径),因此您应在该路径之后输入完整的绝对文件路径。提示中的文件名(例如/home/user/foo.txt或c:\\ documents \\ foo.txt)。

If that is not the case, you at first should print the path entered in the prompt into the variable userFileName to see if the path is correct. 如果不是这种情况,则首先应将在提示中输入的路径打印到变量userFileName以查看该路径是否正确。

I would recommend you after you create your File instance with the path, you check if it exists and if it can be read by its exists() and canRead() instance methods. 我建议你在创建后File的路径例如,你检查它是否存在,如果它可以通过它来读取exists()canRead()实例方法。

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

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