Question

In: Computer Science

Java Linked Lists I want a simple program that reads two text files that contains an...

Java Linked Lists

I want a simple program that reads two text files that contains an integers matrix and store each file into a linked lists matrix

so I can later preform operations such as addition and subtraction on the matrices

an example of the input text files:

sample a

2 6 2
6 2 18
17 11 20

sample b

3 13 5
4 11 20
13 18 20

Solutions

Expert Solution

import java.io.File;
import java.io.FileNotFoundException;
import java.util.LinkedList;
import java.util.Scanner;

public class MatrixReader {
        
        public static LinkedList<Integer> readMatrix(String fileName) {
                
                try {
                        Scanner in = new Scanner(new File(fileName));
                        LinkedList<Integer> matrix = new LinkedList<>();
                        
                        while(in.hasNextInt()) {
                                matrix.add(in.nextInt());
                        }
                        
                        in.close();
                        
                        return matrix;
                } catch (FileNotFoundException e) {
                        return null;
                }
        }
        
        public static LinkedList<Integer> addMatrices(LinkedList<Integer> l1,
                        LinkedList<Integer> l2) {

                LinkedList<Integer> result = new LinkedList<>();
                
                int size = l1.size();
                if(l2.size() < size) {
                        size = l2.size();
                }
                for(int i=0; i<size; i++) {
                        result.add(l1.get(i) + l2.get(i));
                }
                
                return result;
        }
        
        public static void showMatrix(LinkedList<Integer> l) {
                for(int i=0; i<3; i++) {
                        for(int j=0; j<3; j++) {
                                System.out.printf("%3d", l.get(i * 3 + j));
                        }
                        System.out.println();
                }
                System.out.println();
        }
        
        public static void main(String[] args) {
                LinkedList<Integer> mat1 = readMatrix("mat1.txt");
                LinkedList<Integer> mat2 = readMatrix("mat2.txt");
                
                System.out.println("Matrix 1:");
                showMatrix(mat1);
                System.out.println("Matrix 2:");
                showMatrix(mat2);
                
                System.out.println("Addition: ");
                showMatrix(addMatrices(mat1, mat2));
        }

}

mat1.txt:
2 6 2
6 2 18
17 11 20

mat2.txt:
3 13 5
4 11 20
13 18 20

**************************************************

Thanks for your question. We try our best to help you with detailed answers, But in any case, if you need any modification or have a query/issue with respect to above answer, Please ask that in the comment section. We will surely try to address your query ASAP and resolve the issue.

Please consider providing a thumbs up to this question if it helps you. by Doing that, You will help other students, who are facing similar issue.


Related Solutions

a python function that reads two text files and merges in to one Linked List, be...
a python function that reads two text files and merges in to one Linked List, be able to print each Item in the new single Linked List class Node(object): item = -1 next = None def __init__(self, item, next): self.item = item self.next = next ================================ textfile! 979 2744 5409 1364 4948 4994 5089 703 1994 4637 2228 4004 1088 2812 170 5179 2614 238 4523 4849 3592 3258 1951 3440 3977 1247 4076 1824 4759 4855 5430 347 974...
File Compare Write a program that opens two text files and reads their contents into two...
File Compare Write a program that opens two text files and reads their contents into two separate queues. The program should then determine whether the files are identical by comparing the characters in the queues. When two nonidentical characters are encountered, the program should display a message indicating that the files are not the same. If both queues contain the same set of characters, a message should be displayed indicating that the files are identical. // Copyright (c) 2013 __Pearson...
In python i want to create a function. The function will take in two linked lists...
In python i want to create a function. The function will take in two linked lists as the parameters. If one is shorter than the other then the shorter will be the length. I want to take the values from both linked lists and turn them into tuples. I then want these tuples to be put into a new linked list. I want to return that linked list. I want to do this using recursion and no helper functions or...
Write Java program Lab52.java which reads in a line of text from the user. The text...
Write Java program Lab52.java which reads in a line of text from the user. The text should be passed into the method: public static String[] divideText(String input) The "divideText" method returns an array of 2 Strings, one with the even number characters in the original string and one with the odd number characters from the original string. The program should then print out the returned strings.
I want to write this program in java. Write a simple airline ticket reservation program in...
I want to write this program in java. Write a simple airline ticket reservation program in java.The program should display a menu with the following options: reserve a ticket, cancel a reservation, check whether a ticket is reserved for a particular person, and display the passengers. The information is maintained on an alphabetized linked list of names. In a simpler version of the program, assume that tickets are reserved for only one flight. In a fuller version, place no limit...
Could you write a c- program that reads a text file into a linked list of...
Could you write a c- program that reads a text file into a linked list of characters and then manipulate the linked list by making the following replacements 1. In paragraph 1 Replace all “c” with “s” if followed by the characters “e”, “i” or “y”; otherwise 2. In pragraph 2 Replace "We" with v"i" This is the text to be manipulated: Paragraph1 She told us to take the trash out. Why did she do that? I wish she would...
Could you write a c- program that reads a text file into a linked list of...
Could you write a c- program that reads a text file into a linked list of characters and then manipulate the linked list by making the following replacements 1. Replace all “c” with “s” if followed by the characters “e”, “i” or “y”; otherwise 2. Replace "sh" with ph This is the text to be manipulated: Paragraph1 She told us to take the trash out. Why did she do that? I wish she would not do that Paragraph 2 We...
In JAVA : There are two text files with the following information stored in them: The...
In JAVA : There are two text files with the following information stored in them: The instructor.txt file where each line stores the id, name and affiliated department of an instructor separated by a comma The department.txt file where each line stores the name, location and budget of the department separated by a comma You need to write a Java program that reads these text files and provides user with the following menu: 1. Enter the instructor ID and I...
Write a simple text-formating.cpp file that reads (asks for then reads) a text file and produces...
Write a simple text-formating.cpp file that reads (asks for then reads) a text file and produces another text file in Which blank lines are removed, multiple blanks are replaced with a single blank, and no lines are longer than some given length (let say 80). Put as many words as possible on the same line (as close as possible to 80 characters). You will have to break some lines of the given file, but do not break any words or...
I just finished this program where the program reads a text file of full names ,...
I just finished this program where the program reads a text file of full names , first and last name, and a zip code. It reads the items then stores the first name and last name and zipcodes as an objects and prints the items.   However, when i use my text file i get this error "Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 1 out of bounds for length 1 " I notice this issue only happens when the names are...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT