Question

In: Computer Science

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 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

Program in java:

//importing required package
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.StringTokenizer;

//declaring class
public class ReadingTextFile {
    //declaring main method
    public static void main(String[] args) throws FileNotFoundException, IOException {
        //declaring the variables
        String line;
        int size;
       //creating file reader with specified file
        FileReader fr=new FileReader("input.txt");
        BufferedReader br=new BufferedReader(fr);
        //reading the file and storing in the variable
        StringTokenizer stk;
        size=Integer.parseInt(br.readLine().trim());
        int person[][]=new int[size][size-1];
        int len=0;
        //reading files
        while((line=br.readLine())!=null){
            stk=new StringTokenizer(line," ");
            for(int i=0;i<size-1;i++){
                //storing the value in the array
                person[len][i]=Integer.parseInt(stk.nextToken());
            }
            len++;
        }
        //printing the content of array
        for(int i=0;i<size;i++){
            System.out.print("Person #"+(1+1)+" prefers ");
            for(int j=0;j<size-1;j++){
                System.out.print(person[i][j]+" then ");
            }
            //going to new Line
            System.out.println();
        }
    }
}

Output;


Related Solutions

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...
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...
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.
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...
Using C++, write a code that this program always stores text file output into a text...
Using C++, write a code that this program always stores text file output into a text file named "clean.txt". -The program should read one character at a time from "someNumbers.txt", and do the following. -If it is a letter, print that letter to the screen, AND also store it in the text file. All letters should be converted to lowercase beforehand. -If it is a number, print that number to screen, but do NOT store it in the text file....
Make a java program of Mickey I have the starter program but I need to add...
Make a java program of Mickey I have the starter program but I need to add eyes and a smile to it. import java.awt.Canvas; import java.awt.Color; import java.awt.Graphics; import java.awt.Rectangle; import javax.swing.JFrame; public class Mickey extends Canvas { public static void main(String[] args) { JFrame frame = new JFrame("Mickey Mouse"); Canvas canvas = new Mickey(); canvas.setSize(400, 400); canvas.setBackground(Color.white); frame.add(canvas); frame.pack(); frame.setVisible(true); } public void paint(Graphics g) { Rectangle bb = new Rectangle(100, 100, 200, 200); mickey(g, bb); } public void...
Java Question I have a Queue containing String type taking in a text file with an...
Java Question I have a Queue containing String type taking in a text file with an iterator. I need to use ArrayList and HashMap for freqHowMany to get a frequency table in descending order by the highest frequency words in the text. Any help would be much appreciated and thanks! final Iterator input = new Scanner(System.in).useDelimiter("(?U)[^\\p{Alpha}0-9']+"); final Queue queueFinal = new CircularFifoQueue<>(wordsLast); while (input.hasNext()) { final String queueWord = input.next(); if (queueWord.length() > minimumLength) { queueFinal.add(queueWord); // the oldest item...
Your task is to modify the program from the Java Arrays programming assignment to use text...
Your task is to modify the program from the Java Arrays programming assignment to use text files for input and output. I suggest you save acopy of the original before modifying the software. Your modified program should: contain a for loop to read the five test score into the array from a text data file. You will need to create and save a data file for the program to use. It should have one test score on each line of...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT