Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Hello, I am working on a Java language program that reads data from a file. I ha

ID: 3883387 • Letter: H

Question

Hello, I am working on a Java language program that reads data from a file.

I have two examples of the data files below.

_________________________________

6

0 0 1 0 0 1

0 0 1 0 1 1

1 1 0 1 1 1

0 0 1 0 1 1

0 1 1 1 0 1

1 1 1 1 1 0

______________________________

10

0 1 0 0 1 1 0 0 0 0

1 0 1 0 0 0 1 0 0 0

0 1 0 1 0 0 0 1 0 0

0 0 1 0 1 0 0 0 1 0

1 0 0 1 0 0 0 0 0 1

1 0 0 0 0 0 0 1 1 0

0 1 0 0 0 0 0 0 1 1

0 0 1 0 0 1 0 0 0 1

0 0 0 1 0 1 1 0 0 0

0 0 0 0 1 0 1 1 0 0

___________________________

The first row is the number of nodes and the rest denotes the connections for each node.

The program should read two parameters from the command line.

The first is a data filename.

the second is an integer value saved in variable k (this value wont be used in the current program but will be used when i work on it later).

It should allocate a 2d array for the data

It should also print out the graph data(it say this should be a method so it can be reused, but im not sure how to do that.

for example on the parameters, if i run the program from the command line it would look like:

>>java Prog data1.txt 4

On the first test file the output would look like

0 0 1 0 0 1

0 0 1 0 1 1

1 1 0 1 1 1

0 0 1 0 1 1

0 1 1 1 0 1

1 1 1 1 1 0

If you know how to make this, I would greatly appreciate the help. Thank you.

Explanation / Answer

package Online;

import java.io.File;

import java.util.Scanner;

//Class ReadFileDisplayData definition

public class ReadFileDisplayData

{

//To store file name

String fileName;

//Instance variable to store the first number in the file

int number;

//Instance variable for 2D array

int numberArray[][];

//Method to read data from file

void readData(String fileName)

{

//File object created

File file = new File(fileName);

//For row and column counter of array

int counterR = 0, counterC = 0;

//Handles exception

try

{

//Scanner object created

Scanner scanner = new Scanner(file);

//Extracts the first number in the file

number = scanner.nextInt();

//Creates an array

numberArray = new int[number][number];

//Checks for the data availability

while(scanner.hasNextInt())

{

//Checks if the counter for column is less then number

if(counterC < number)

{

//Reads a number from the file and stores in row and column position

numberArray[counterR][counterC++] = scanner.nextInt();

}//End of if

//if the counter for column is not less then number

else

{

//Increase the row by one

counterR++;

//Set the column counter to zero

counterC = 0;

}//End of else

}//End of while

}//End of try

//Catch block

catch(Exception e)

{

e.printStackTrace();

}//End of catch

}//End of method

//Method to display the contents of array

void displayData()

{

//Displays number of elements present in the array

//Loops till end of row

for(int r = 0; r < number; r++)

{

//Loops till end of column

for(int c = 0; c < number; c++)

System.out.print(numberArray[r][c] + " ");

System.out.println();

}//End of for loop

}//End of method

//Main method definition

public static void main(String ss[])

{

//Creates an object of the file

ReadFileDisplayData rfd = new ReadFileDisplayData();

//Extracts first command line argument and store it in fileName

rfd.fileName = ss[0];

//Extracts second command line argument, converts it to integer and stores it in number

rfd.number = Integer.parseInt(ss[1]);

//Calls the method to read the data from file and stores it in 2D array

rfd.readData(rfd.fileName);

//Display the data

rfd.displayData();

}//End of method

}//End of class

Sample Run 1:

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

Sample Run 2:

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

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote