简体   繁体   中英

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,
  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).

I suppose that you only enter the name of the file you want to read (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).

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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