Question

In: Computer Science

java question: How would you be able to store a matrix from a text file into...

java question: How would you be able to store a matrix from a text file into a linked or doubly linked list, if you cannot use 2D arrays?


input example:
1 2 3 4
1 3 2 4
4 2 3 1

Solutions

Expert Solution

Input:

Linked2DArray.java

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

public class Linked2DArray { //Define a class

    LinkedList<String> list; //Declare a member variable

    Linked2DArray(){
       list = new LinkedList<>(); //Initialize linkedlist
    }

    public static void main(String[] args) throws FileNotFoundException {
        Scanner scanner = new Scanner(new File("input.txt")); //Initialize scanner with file

        Linked2DArray arr = new Linked2DArray(); //Create an object of class

        while (scanner.hasNextLine()) //Read file one line at a time
        {
            Scanner inner_scanner = new Scanner(scanner.nextLine()); //Parse the line and split by whitespaces

            while (inner_scanner.hasNext()) //Read until the end of line
            {
                arr.list.add(inner_scanner.next());
            }
            arr.list.add("x"); //Add an indicator for next row
        }

        for (String s: arr.list) //Read elements of linkedlist
        {
            if (s.equals("x")) //If indicator found, switch to next line
            {
                System.out.print("\n");
                continue;
            }
            System.out.print(s+" "); //Print elements
        }
    }
}


Output:

Brief Explanation:

  • Let's consider the given 2D array and approach the solution step-by-step.
  • In order to read lines as well as each number, we are using 2 different Scanner objects. For the first Scanner object, we are passing the input file. This scanner is responsible for dividing input into the number of lines. Afterward, we are using the second scanner to read numbers from each line.
  • So in the first iteration, LinkedList is storing
  • The loop is exiting as soon as the end of the line is reached. At this point, we are storing an identifier that will help us identify the next row. So now our LinkedList is
  • The first Scanner is checking if there is another line and passing it to second Scanner. So the second Scanner is taking the 2nd line and inserting elements into the LinkedList.
  • The final representation of LinkedList is,
  • In the for-loop below, we are printing elements of a row on a single line. We are using identifier to jump to next line.
  • input.txt has the same content as given input and following the same format. While executing the code, please store the file in the same directory as code.


Related Solutions

How to read a text file and store the elements into a linked list in java?...
How to read a text file and store the elements into a linked list in java? Example of a text file: CS100, Intro to CS, John Smith, 37, 100.00 CS200, Java Programming, Susan Smith, 35, 200.00 CS300, Data Structures, Ahmed Suad, 41, 150.50 CS400, Analysis of Algorithms, Yapsiong Chen, 70, 220.50 and print them out in this format: Course: CS100 Title: Intro to CS Author: Name = John Smith, Age = 37 Price: 100.0. And also to print out the...
Computer Science - Java Programming How do you read a text file and store three different...
Computer Science - Java Programming How do you read a text file and store three different pieces of information in the database when the given text file contains this info.: 12345 Computer Science Bob Stone 23456 Art James G. Ocean? These are written in the format as ID Class Name. I was going to take the three different pieces of information by separating them by spaces, but the number of spaces is random and I don't know how to adjust...
using java, parse a text file to be able to list the word(s) with the highest...
using java, parse a text file to be able to list the word(s) with the highest frequency in a sentence across all sentences in the whole file, also print its frequency and the corresponding sentence. cannot use hash maps. assume text file will be multiple paragraphs long.
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.
Linux Question: you will practice on creating and editing a text file by VI. How to:...
Linux Question: you will practice on creating and editing a text file by VI. How to: Install JDK 1.8 by the following command: $ sudo yum    –y    install    java-1.8.0-openjdk-devel.x86_64 Use VI to create a file “YourFirstNameHomework.java” (e.g., “MollyHomework.java”) and add the following contents into it. (grading details: file name format:10 pts, a screenshot of the file content in the VI environment: 10pts, paragraphs: 10pts, empty lines and indentations: 10, text: 10pts) Here's the code import java.util.Scanner; // Import the Scanner...
Using JAVA The following code is able to read integers from a file that is called...
Using JAVA The following code is able to read integers from a file that is called "start.ppm" onto a 3d array called "startImage". Implement the code by being able to read from another file (make up any file name) and save the data onto another 3d array lets say you call that array "finalImage". The purpose of this will be to add both arrays and then get the average Save the average onto a separte 3darray,lets say you call it...
Using JAVA The following code is able to read integers from a file that is called...
Using JAVA The following code is able to read integers from a file that is called "start.ppm" onto a 3d array called "startImage". Implement the code by being able to read from another file (make up any file name) and save the data onto another 3d array lets say you call that array "finalImage". The purpose of this will be to add both arrays and then get the average Save the average onto a separte 3darray,lets say you call it...
C++ Question 2 You will read in data about the planets from a text file, and...
C++ Question 2 You will read in data about the planets from a text file, and then print out the data about that planet when the user requests the data. You will need to create a Planet Class (since this assignment does not cover templates, you are expected to split your class into .cpp and .h files), as well as a .cpp/.h library with the functions described below Planet Class For the first phase of the question, you will create...
using java, parse a text file to answer the following question: -list sentences with the maximum...
using java, parse a text file to answer the following question: -list sentences with the maximum number of occurences of the word “the” in the whole file and also list the corresponding frequency. (cannot use hash maps) example output: the:3:The day had came to leave before the storm. What hit the back bumper of the car before the window cracked? The classroom doors where shut closed before the students open the project.
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT