In: Computer Science
A. Write a program called OnesZerosA.java to do all of the following. Please note, for this problem you are not allowed to use ArrayList.
Read a file containing ones and zeros (such as the example below, also available at http://www2.hawaii.edu/~esb/2020fall.ics111/oneszeros.txt) into a two-dimensional int array. Your program must create the array with the same number of rows as the number of lines in the file, and the same number of columns as the number of ones and zeros in each line.
Your program must (a) read the name of the file from the command-line arguments, (b) open the file, (c) check that each line has the same number of characters, and (d) check that the only characters in a line are ones and zeros. If there is no such command-line argument, or no such file, or if any of the the checks fail, your program should exit -- it is OK if your program exits by returning from main, by throwing an exception, or in any other way.
Once you have completed all your checks, allocate the int array and read the file again to fill it with the ones and zeros.
Next, print each of the elements of the array by row and column, giving a result such as:
0000000000001000000000000
0000000000010100000000000
0000000000100010000000000
0000000001000001000000000
0000000010000000100000000
0000000100000000010000000
0000001000000000001000000
0000010000000000000100000
0000100000000000000010000
0001000000000000000001000
0010000000000000000000100
0100000000000000000000010
1000000000000000000000001
0100000000000000000000010
0010000000000000000000100
0001000000000000000001000
0000100000000000000010000
0000010000000000000100000
0000001000000000001000000
0000000100000000010000000
0000000010000000100000000
0000000001000001000000000
0000000000111110000000000
0000000000000000000000000
0000000000000000000000000
Complete code in java:-
import java.util.*;
import java.io.*;
class OnesZerosA {
public static void main(String ... args) {
// Taking file name in 'filename'
from command line argument.
String filename = args[0];
try {
// Creating file
object.
File file = new
File(filename);
// Creating
Scanner object to from 'file'.
Scanner sc = new
Scanner(file);
// 'rows' and
'cols' will define number of rows and number of columns for the
array
int rows,
cols;
rows = 0;
cols = 0;
// Check
variable will tell that file is valid or invalid for the desired
conditions.
boolean check =
true;
// Reading from
file one row at a time.
while(sc.hasNextLine()) {
String line = sc.nextLine();
rows++;
// Checking if length of all rows are
same.
if(cols != 0 && line.length() != cols)
{
check = false;
}
cols = line.length();
// Checking if each character of row is either
'1' or '0'
for(int i = 0; i < cols; i++) {
if(line.charAt(i) != '0'
&& line.charAt(i) != '1') {
check =
false;
break;
}
}
if(!check) {
break;
}
}
// If file is
valid, make integer matrix.
if(check)
{
// Again creating scanner object to read from
file.
sc = new Scanner(file);
// Creating of matrix of required size.
int a[][] = new int[rows][cols];
int i = 0;
// Reading file, one row at a time.
while(sc.hasNextLine()) {
String line =
sc.nextLine();
// Storing value in
matrix.
for(int j = 0; j <
line.length(); j++) {
a[i][j] =
Character.getNumericValue(line.charAt(j));
}
i++;
}
// Printing matrix.
for(i = 0; i < rows; i++) {
for(int j = 0; j < cols;
j++) {
System.out.print(a[i][j]);
}
System.out.println();
}
}
else {
System.out.println("File is invalid");
}
} catch(FileNotFoundException e)
{
System.out.println("File not found! Try again with another
file.\n");
}catch(Exception e) {
System.out.println("An exception occurred! Try again\n"+e);
}
}
}
Content of input file:-
01110101100011111
00110010011001011
10111100101011000
00000001000000011
00010011100011100
01011011010010110
10000101111111111
10111100100101001
11010011000100101
11011111110001111
01100010001100000
00111111011101110
11100111111011010
10011111000000110
01101011101101110
01101011011111001
00011000100100101
00011001101010001
10100001011100010
00010001000000111
Screenshot of output:-