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.
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...
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.
In Java. Modify the attached GameDriver2 to read characters in from a text file rather than...
In Java. Modify the attached GameDriver2 to read characters in from a text file rather than the user. You should use appropriate exception handling when reading from the file. The text file that you will read is provided. -------------------- Modify GaveDriver2.java -------------------- -----GameDriver2.java ----- import java.io.File; import java.util.ArrayList; import java.util.Scanner; /** * Class: GameDriver * * This class provides the main method for Homework 2 which reads in a * series of Wizards, Soldier and Civilian information from the user...
Write a Java program that reads words from a text file and displays all the non-duplicate...
Write a Java program that reads words from a text file and displays all the non-duplicate words in ascending order. The text file is passed as a command-line argument. Command line argument: test2001.txt Correct output: Words in ascending order... 1mango Salami apple banana boat zebra
C++ coding question From the text file given to you- “worldpop.txt”, perform the following tasks using...
C++ coding question From the text file given to you- “worldpop.txt”, perform the following tasks using Boolean function. PS-*Write separate codes for each task* Task 1. Display the names of the countries with: 1. Population >=1000,000,000 2. Population <= 1000,000 Task 2. Display the names of the first 10 countries Task 3. Display the names of the last 10 countries contents of worldpop.txt: Afghanistan 32738376 Akrotiri 15700 Albania 3619778 Algeria 33769669 Andorra 72413 Angola 12531357 Anguilla 14108 Argentina 40677348 Armenia...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT