Question

In: Computer Science

Read a text file into arrays and output - Java Program ------------------------------------------------------------------------------ I need to have...

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.

Solutions

Expert Solution

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

====================================================================


Related Solutions

Java - Text File to Arrays and output ------------------------------------------------------------------------------ I need to have a java program...
Java - Text File to Arrays and output ------------------------------------------------------------------------------ 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...
I need to write a java program (in eclipse) that will read my text file and...
I need to write a java program (in eclipse) that will read my text file and replace specific placeholders with information provided in a second text file. For this assignment I am given a text file and I must replace <N>, <A>, <G>, with the information in the second file. For example the information can be John 22 male, and the template will then be modified and saved into a new file or files (because there will be multiple entries...
Using java, I need to make a program that reverses a file. The program will read...
Using java, I need to make a program that reverses a file. The program will read the text file character by character, push the characters into a stack, then pop the characters into a new text file (with each character in revers order) EX: Hello World                       eyB       Bye            becomes    dlroW olleH I have the Stack and the Linked List part of this taken care of, I'm just having trouble connecting the stack and the text file together and...
Please write a java program to write to a text file and to read from a...
Please write a java program to write to a text file and to read from a text file.
Java Program. Please read carefully. i need the exact same output as below. if you unable...
Java Program. Please read carefully. i need the exact same output as below. if you unable to write the code according to the question, please dont do it. thanks To the HighArray class in the highArray.java program (Listing 2.3), add the following methods: 1.      getMax() that returns the value of the highest key (value) in the array without removing it from the array, or –1 if the array is empty. 2.      removeMax() that removes the item with the highest key from the...
Write a java program: Write a program that creates a text file. Write to the file...
Write a java program: Write a program that creates a text file. Write to the file three lines each line having a person's name. In the same program Append to the file one line of  'Kean University'.  In the same program then Read the file and print the four lines without lines between.
How do I do this: Write a program that can read a text file of numbers...
How do I do this: Write a program that can read a text file of numbers and calculate the mean and standard deviation of those numbers. Print the result in another text file. Put the result on the computer screen. EACH LINE OF THE PROGRAM MUST BE COMMENTED!
1. I need a java program that prints the following rows of arrays, like shown [3,5,6...
1. I need a java program that prints the following rows of arrays, like shown [3,5,6 7, 61, 71, 9, 99, 999, 4, 1, 0] 2. I also need a program that prints the sum of all the arrays 3. and a third program that only prints the elements from row one. Thanks!
I need a brief text file to explain why the program “filesize1v.c” gets stuck in the...
I need a brief text file to explain why the program “filesize1v.c” gets stuck in the “do/while” loop (i.e., explain why “ch != EOF” is always true) ?? example.txt : hello ÿhello everyone. _________________ //filesize1v.c #include #include int main(int argc, char *argv[]){ FILE *fd; unsigned char ch; //what will happen if you uncomment this line and comment the //next line //char ch; int fileSize=-1; fd = fopen(argv[1], "r"); do{ ch=getc(fd); //0xFF fileSize++; printf("fileSize=%d\n", fileSize); printf("Char read is ch=%c, in hex...
Complete the program to read in values from a text file. The values will be the...
Complete the program to read in values from a text file. The values will be the scores on several assignments by the students in a class. Each row of values represents a specific student's scores. Each column represents the scores of all students on a specific assignment. So a specific value is a specific student's score on a specific assignment. The first two values in the text file will give the number of students (number of rows) and the number...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT