In: Computer Science
Read a text file into arrays and output - Java Program
------------------------------------------------------------------------------
I need to have a java program read an "input.txt" file (like below) and store the information in an respective arrays. This input.txt file will look like below and will be stored in the same directory as the java file. The top line of the text represents the number of people in the group for example. the lines below it each represent the respective persons preferences in regards to the other people in the group. I would then like the java program to output as a string each array and its elements.
Example of "input.txt" that would be read
4
2 3 4
1 3 4
1 2 4
1 2 3
As you can see the top line says there are 4 people in this group and person #1 which is line #2 prefers to be in a group with persons 2, 3, and 4 respectively. So for Person #1 it would be an Array with 3 elements being 2, 3, and 4 in that order.
The java program in this case would read the .txt file. create 4 arrays based on line #1 saying there are 4 people and then it would output as a string the 4 arrays and their respective elements in order,
Example Output:
Person #1 prefers 2, then 3, then 4
Person #2 prefers 1, then 3, then 4
.
.
.
The java program should be able to work with text files that have different numbers of people, it should create # of arrays based on the number on line 1.
ReadTextArrayFile.java
package practice;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Arrays;
public class ReadTextArrayFile {
public static void main(String[] args) {
try {
String
totalArrays =
Files.readAllLines(Paths.get("input.txt")).get(0);
//reading first line from the file
int arraySize =
Integer.parseInt(totalArrays); //setting number of
arrays to be created
BufferedReader
textReader = new BufferedReader(new FileReader("input.txt"));
textReader.readLine();//ignoring the first line
String data =
null;
int arrayNumner
= 1;
while((data =
textReader.readLine())!=null) {
String[] splitArray = data.split(" ");//spliting
each line in file with space
int size = splitArray.length;//getting total
numbers length in each line
int myArray[] = new int[size];//creating an
integer array to store individual values from each line
for(int i=0;i<size;i++) {
myArray[i] =
Integer.parseInt(splitArray[i]);//converting each character into
number
}
if(arrayNumner <= arraySize) {
System.out.print("Person #"+
arrayNumner +" prefers "+myArray[0]+", then "+myArray[1]+", then
"+myArray[2]);//printing the output array
}
System.out.println();
arrayNumner++;
}
System.out.println();
} catch (Exception e) {
// TODO Auto-generated catch
block
e.printStackTrace();
}
}
}
====================================================================
input.txt
4
2 3 4
1 2 3
2 2 3
1 1 3
====================================================================
Output:
Person #1 prefers 2, then 3, then 4
Person #2 prefers 1, then 2, then 3
Person #3 prefers 2, then 2, then 3
Person #4 prefers 1, then 1, then 3
====================================================================