0

How would you be able to import a .txt file and then place each line into an array list as an object, but have each object be an array.

The .txt file looks like this:

1 a 1 2 
1 b 0 1 
2 a 2 3 
2 b 0 3 
3 a 3 1 
3 b 0 3 

I wanted to be able to make each line an object array like so where so that i could call upon the object and the specific point in the array:

<1,a,1,2> 
<1,b,0,1> 
<2,a,2,3> 
<2,b,0,3> 
<3,a,3,1>
<3,b,0,3> 

and then have an array list where each line (there can be any amount of line hence why i chose an array list) is one thing in the array list. kinda like <line1,line2,line3,etc..> so i can use the array list and then the values in the individual arrays within the array list.

This is the code that I currently have and i don't know where to go from here. I am using a buffered reader


        Scanner input = new Scanner(System.in);  // Create a Scanner object
        String inputString = input.nextLine(); 



        try {

            BufferedReader FSMreader = new BufferedReader(new FileReader(args[0]));      
            List<FSMline> line = new ArrayList<>(); 
            List<String> currentState = new ArrayList<>();
            List<String> inputChoice = new ArrayList<>(); 
            List<String> outputFunction = new ArrayList<>();
            List<String> nextState = new ArrayList<>();



            String lines;
            while ((lines = FSMreader.readLine()) != null) {

                line.add(new FSMline(lines.split(" ")));

            }

Any help is appreciated because i'm running out of time on a deadline in a class that i am struggling to understand.

thanks

6
  • @VinayHegde I wanted each line to be an array that contains the contents of each line. And then make an array list that contains each array Commented Oct 1, 2019 at 17:59
  • You already have an array, but you pass it to the FSMline constructor. Don't do that anymore and fix the type of line, then you should be done. Commented Oct 1, 2019 at 18:01
  • @Tom No it's not done . After getting an ArrayList then I think he wants to access the objects in that each ArrayList and store that to different list/array Commented Oct 1, 2019 at 18:03
  • @Tom i'm sorry, i'm fairly new to CS, i dont understand Commented Oct 1, 2019 at 18:04
  • @LiamKr If I'm understanding correctly, essentially you're looking for an ArrayList of ArrayLists, each of which contain the Strings? In that case, unless you want to use a custom object, you can change List<FSMline> line = new ArrayList<>() to List<ArrayList<String>> line = new ArrayList<>(). If you do want to use a custom object, you will need to assign your inner arrays to that object Commented Oct 1, 2019 at 18:39

1 Answer 1

1

Because you didn't mention the datatype of FSMline, I create a collection(array list of arrays) - List<String[]> - to store the content of given text file as follows:

BufferedReader fsmReader = new BufferedReader(new FileReader(args[0]));

List<String[]> contentList = new ArrayList<>();
String lines;
while ((lines = fsmReader.readLine()) != null) {
    contentList.add(lines.split(" "));
}

//print the list     
for (String[] content : contentList) {
    System.out.println(Arrays.toString(content));
}

Console output:

[1, a, 1, 2]
[1, b, 0, 1]
[2, a, 2, 3]
[2, b, 0, 3]
[3, a, 3, 1]
[3, b, 0, 3]

UPDATED

And if you want to get the third element of first array in the array list, please use get(int index) and [int index] to retrieve element from list and array, respectively:

System.out.println(contentList.get(0)[2]);

Or, you can transform an array into a list by using Arrays.asList(), then you can also use get(int index):

System.out.println(Arrays.asList(contentList.get(0)).get(2));
Sign up to request clarification or add additional context in comments.

2 Comments

ok, this is super helpful. How then would I access the numbers inside the arrays inside the array list. Would it be something like ArrayList.get(0,2) to access to 1 on th first line?
@i_like_trucks Hi, I updated the way in my post, please check!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.