Question

In: Computer Science

Write a program in Java to: Read a file containing ones and zeros into a two-dimensional...

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

Solutions

Expert Solution

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 :


Related Solutions

Write an assembly program for the MSP430 to calculate the number of zeros and ones in...
Write an assembly program for the MSP430 to calculate the number of zeros and ones in an array. If the number of zeros is more than the number of ones, the red LED(connected to P1.0 on the MSP430 launchpad) will turn on. Otherwise, the green LED (connected to P1.6 on the MSP430 launchpad) will turn on.
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.
Write a program in Java, that creates a Jframe with a menu containing only file. Inside...
Write a program in Java, that creates a Jframe with a menu containing only file. Inside file there should be items: Open, Save, and Save As. Selecting open prompts the user to input a file name to a txt document containing employee information and displays a Jtable with the information, which can be edited. With column headers {"First Name" , "Last Name" , "Occupation" , "Office #"} Example: Gary Osbourn Teacher 113 Michelle Ramirez Teacher 101 Ava Gomez Principal 120...
Write a program in Java that reads a file containing data about the changing popularity of...
Write a program in Java that reads a file containing data about the changing popularity of various baby names over time and displays the data about a particular name. Each line of the file stores a name followed by integers representing the name’s popularity in each decade: 1900, 1910, 1920, and so on. The rankings range from 1 (most popular) to 1000 (least popular), or 0 for a name that was less popular than the 1000th name. A sample file...
Write a C++ program to read a data file containing the velocity of cars crossing an...
Write a C++ program to read a data file containing the velocity of cars crossing an intersection. Then determine the average velocity and the standard deviation of this data set. Use the concept of vector array to store the data and perform the calculations. Include a function called “Standard” to perform the standard deviation calculations and then return the value to the main function for printing. Data to use. 10,15,20,25,30,35,40,45,50,55. Please use something basic.
Write a Java program to read in words from the given file “word.txt”. a. Prompt the...
Write a Java program to read in words from the given file “word.txt”. a. Prompt the user for two words b. Print out how many words in the file fall between those words c. If one of the two words is not contained in the file, print out which word is not found in the file d. If both words are not found in the file, print out a message e. Sample output: Please type in two words: hello computer...
I need to write a java program (in eclipse) that will read my text file and...
I need to write a java program (in eclipse) that will read my text file and replace specific placeholders with information provided in a second text file. For this assignment I am given a text file and I must replace <N>, <A>, <G>, with the information in the second file. For example the information can be John 22 male, and the template will then be modified and saved into a new file or files (because there will be multiple entries...
Write a Java program that will use a two-dimensional array and modularity to solve the following...
Write a Java program that will use a two-dimensional array and modularity to solve the following tasks: Create a method to fill the 2-dimensional array with (random numbers, range 0 - 30). The array has rows (ROW) and columns (COL), where ROW and COL are class constants. Create a method to print the array. Create a method to find the largest element in the array Create a method to find the smallest element in the array Create a method to...
Write a Java program that will use a two-dimensional array and modularity to solve the following...
Write a Java program that will use a two-dimensional array and modularity to solve the following tasks: 1. Create a method to generate a 2-dimensional array (random numbers, range 0 - 500). The array has ROW rows and COL columns, where ROW and COL are class constants. 2. Create a method to print the array. 3. Create a method to find the largest element in the array 4. Create a method to find the smallest element in the array 5....
Write a Java program that will use a two-dimensional array and modularity to solve the following...
Write a Java program that will use a two-dimensional array and modularity to solve the following tasks: Create a method to generate a 2-dimensional array (random numbers, range 0 - 500). The array has ROW rows and COL columns, where ROW and COL are class constants. Create a method to print the array. Create a method to find the largest element in the array Create a method to find the smallest element in the array Create a method to find...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT