In: Computer Science
Write a program in Java to:
Read a file containing ones and zeros into a two-dimensional int array. Your program must (1) read the name of the file from the command-line arguments, (2) open the file, (3) check that each line has the same number of characters and (4) 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 checks fail, your program should exit, and it's ok if your program exits by returning from main, by throwing an exception, or in any other way.
Must use ArrayList instead of arrays, and this program must not
declare any int array variables. Your ones and zeros will be saved
in a variable declared and initialized as:
java.util.ArrayList<java.util.ArrayList<Integer>>
dataFromFile = new
java.util.ArrayList<java.util.ArrayList<Integer>>();
You may again need to read the file twice, but you may instead be able to read it just once -- both solutions are fine for this assignment, it is up to you.
Print the array by columns and rows, so that it prints (the below zeros and ones is the input text file):
0000000000001000000000000
0000000000010100000000000
0000000000100010000000000
0000000001000001000000000
0000000010000000100000000
0000000100000000010000000
0000001000000000001000000
0000010000000000000100000
0000100000000000000010000
0001000000000000000001000
0010000000000000000000100
0100000000000000000000100
1000000000000000000000100
0100000000000000000000100
0010000000000000000000100
0001000000000000000001000
0000100000000000000010000
0000010000000000000100000
0000001000000000001000000
0000000100000000010000000
0000000010000000100000000
0000000001000001000000000
0000000000100010000000000
0000000000010100000000000
0000000000001000000000000
All operations are performed successfully
1)read the name of the file from the command-line arguments
2)open the file
3)check that each line has the same number of characters
4)check that the only characters in a line are ones and zeros
5)Print the array by columns and rows
import java.util.Scanner;
import java.util.*;
import java.io.File;
import java.io.FileNotFoundException;
public class Main {
public static void main(String args[]) {
if(0 < args.length){ //check if fine name given as input or not
String filename = args[0];
try {
File myObj = new File(filename); //creating file object
Scanner fileobj = new Scanner(myObj); //reading file using file object
int len = 0; //declaring len variable which is used to check if each line contain same number of characters.
ArrayList<ArrayList<Integer> > datafromfile = new ArrayList<ArrayList<Integer> >();
int k = 0;
while (fileobj.hasNextLine()) {
String data = fileobj.nextLine();
datafromfile.add(new ArrayList<Integer>());
if(len == 0) len = data.length(); //for 1st line in file
else{
if(len!=data.length()){ //if any line contain extra number of characters/elements we simply return
return;
}
}
for(int i = 0;i<data.length();i++){
if(data.charAt(i)!= '0' || data.charAt(i)!='1'){ //check if given file contains only 0 and 1
return;
}else{
//if data is 0 or 1 then we store that in our array list
datafromfile.get(k).add(i,(int)data.charAt(i));
}
}
k++;
// System.out.println(data);
}
fileobj.close();
// data from input file is printed
for(int i = 0;i<datafromfile.size();i++){
for(int j = 0; j < datafromfile.get(i).size(); j++){
System.out.println(datafromfile.get(i).get(j));
}
System.out.println();
}
} catch (FileNotFoundException e) {
System.out.println("An error occurred.");
e.printStackTrace();
}
}else{
return;
}
}
}
OUTPUT :