Question

Create a java project. Create a class called cls2DarrayProcessor.java. with the following: Reads numbers from a...

Create a java project.

Create a class called cls2DarrayProcessor.java. with the following:

  1. Reads numbers from a pre-defined text file.

A: Text file name should be data.txt

B: Text file should be in the same workspace directory of your project's folder

C: Text file name should be STORED in a String and called:

String strFile ='./data.txt'

  1. Defines a 2-D array of integers with dimensions of 5 rows by 5 columns.
  2. Inserts the data from the text file to the 2-D array following this logic:

A: If the read number matches the 2-D array columns and row numbers, then assign the read number to that array element, otherwise, assign the value 0 to that element.

  1. Prints out all elements in the array: separated by a space, one row per line.
0 0
Add a comment Improve this question Transcribed image text
Answer #1

The below code does the above job of reading data from a file by using Scanner and reading each line separately, and modifying the 2D array with the logic above.

CODE:-

import java.io.*; //to perform basic input output operations

import java.util.Scanner; //take input from the file

class cls2DarrayProcessor { //main class

    public static void main(String args[]) throws FileNotFoundException {

        String strFile = "./data.txt";

        File file = new File(strFile);

        Scanner sc = new Scanner(file); //a Scanner object to read from the file and throws a FileNotFoundException if the file is not found

        

        int[][] A = new int[5][5]; //Create a new 2d Array of integers.

        while(sc.hasNextLine()) //while there are lines in the file by using a hasNextLine method which checks if there exists, more lines to be processes, if no then returns a false, else true

        {

            int x = Integer.parseInt(sc.nextLine()); //converts the string line to its corresponding integer

            for(int i=0; i<5; i++) //Traverse the 2D array and check if any entry in the matrix has the row number and the column number as the read integer x,if yes then assign it to that element else assign 0.

            {

                for(int j=0; j<5; j++)

                {

                    if(i == x && j == x)

                    {

                        A[i][j] = x; //row number and column number matches and thus assigned to it

                    }

                    else{

                        A[i][j] = 0; //does not match, and hence 0 is assigned

                    }

                }

            }

        }

        //Print the elements of the 2D array

        for(int i=0; i<5; i++)

        {

            System.out.printf("\n"); //print a new line for every row

            for(int j=0; j<5; j++)

            {

                System.out.printf("%d ", A[i][j]); //print the elements of the array by using printf which is a C-style formatting for printing the elements

            }

        }

    }   

}

OUTPUT:-

0 0 0 0 0
0 1 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0

DATA.TXT:-

1

0

1

3

4

1

2

8

1

4

2

6

7

1

9

0

3

4

2

5

6

4

3

4

1

Add a comment
Know the answer?
Add Answer to:
Create a java project. Create a class called cls2DarrayProcessor.java. with the following: Reads numbers from a...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Step 1: Getting Started Create a new .java file named Lab12.java. At the beginning of this...

    Step 1: Getting Started Create a new .java file named Lab12.java. At the beginning of this file, include your assignment documentation code block. After the documentation block, you will need several import statements. import java.util.Scanner; import java.io.BufferedReader; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; Next, declare the Lab12 class and define the main function. public class Lab12 { public static void main (String [] args) { Step 2: Declaring Variables For this section of the lab, you will need to declare...

  • Following should be done in C++: Create a program that reads a text file and prints...

    Following should be done in C++: Create a program that reads a text file and prints out the contents of the file but with all letters converted to uppercase. The name of the file to be read by the program will be provided by the user. Here are some example session: Contents of "data.txt": Hello, World! User input: data.txt Program output: HELLO, WORLD! Contents of "data.txt": tHiS FiLe HaS mIxEd CaSeS User input: data.txt Program output: THIS FILE HAS MIXED...

  • create a new Java application called "WeightedAvgWithExceptions" (without the quotation marks), according to the following guidelines...

    create a new Java application called "WeightedAvgWithExceptions" (without the quotation marks), according to the following guidelines and using try-catch-finally blocks in your methods that read from a file and write to a file, as in the examples in the lesson notes for reading and writing text files. Input File The input file - which you need to create and prompt the user for the name of - should be called 'data.txt', and it should be created according to the instructions...

  • Write a menu based program implementing the following functions: (0) Write a function called displayMenu that...

    Write a menu based program implementing the following functions: (0) Write a function called displayMenu that does not take any parameters, but returns an integer representing your user's menu choice. Your program's main function should only comprise of the following: a do/while loop with the displayMenu function call inside the loop body switch/case, or if/else if/ ... for handling the calls of the functions based on the menu choice selected in displayMenu. the do/while loop should always continue as long...

  • There is a file called mat2.txt in our files area under the Assignments folder. Download it...

    There is a file called mat2.txt in our files area under the Assignments folder. Download it and save it in the same folder where this Matlab file (HW08_02.m) is saved. It may be worth opening that text file to see how it is set out. Note well, however, that the file might have a different number of lines and different number of numbers on each line. Write a Matlab program that does the following: Prompt the user for an input...

  • I've been assigned to create a new Java application called "CheckString" (without the quotation marks) according...

    I've been assigned to create a new Java application called "CheckString" (without the quotation marks) according to the following guidelines. ** Each method below, including main, should handle (catch) any Exceptions that are thrown. ** ** If an Exception is thrown and caught, print the Exception's message to the command line. ** Write a complete Java method called checkWord that takes a String parameter called word, returns nothing, and is declared to throw an Exception of type Exception. In the...

  • create a new Java application called "CheckString" (without the quotation marks) according to the following guidelines....

    create a new Java application called "CheckString" (without the quotation marks) according to the following guidelines. ** Each method below, including main, should handle (catch) any Exceptions that are thrown. ** ** If an Exception is thrown and caught, print the Exception's message to the command line. ** Write a complete Java method called checkWord that takes a String parameter called word, returns nothing, and is declared to throw an Exception of type Exception. In the method, check if the...

  • Programing in Scala: Create a class called “Array” that implements a fixed-sized two-dimensional array of floating-point...

    Programing in Scala: Create a class called “Array” that implements a fixed-sized two-dimensional array of floating-point numbers. Write separate methods to get an element (given parameters row and col), set an element (given parameters row, col, and value), and output the matrix to the console formatted properly in rows and columns. Next, provide an immutable method to perform array addition given two same-sized array.

  • Description: Create a program called numstat.py that reads a series of integer numbers from a file...

    Description: Create a program called numstat.py that reads a series of integer numbers from a file and determines and displays the name of file, sum of numbers, count of numbers, average of numbers, maximum value, minimum value, and range of values. Purpose: The purpose of this challenge is to provide experience working with numerical data in a file and generating summary information. Requirements: Create a program called numstat.py that reads a series of integer numbers from a file and determines...

  • write a Java console application that Create a text file called Data.txt. Within the file, use...

    write a Java console application that Create a text file called Data.txt. Within the file, use the first row for the name and data title, and use the second row for column headers. Within the columns, insure that the first column is a label column and other columns contain numeric data. In your application, read file Data.txt into parallel arrays, one for each column in the file. Create method printArrays to print the header row(s) and the (unsorted) data in...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT