Question

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 1 local variable: a String that we will use to store the data read from a file. Declare this variable inside of the main function.

// A String for the strings that we will read from file.
// -->

Step 3: Declare the Array

Make an 2D array with 12 elements (4 rows and 3 columns) This declaration is very similar to the way we have declared Scanner, Person, Student, and Employee objects in the past. Make sure to declare a Stringarray.

// For reference, the following is an EXAMPLE declaration of an
// String [] [] Array1 = new String [4][3];
// -->

Step 4: Create a text file

Make a text file and write 12 strings in it, one string on each line, and then save it in the folder that you want to save your program in it. The text file and your java program must be in the same folder.

Step 5: read from the file

In this step you must open the file that you made in the step 4 and read the file.

// put all the commands of this step in a try block
try {
    // Make a fileReader object to open the file that you want
    // You the name of the file that you made in step 6
    FileReader fr = new FileReader (name of the file);
    // define a BufferedReader named inFile to read from the file
    // -->
    // use a nested for loop structure to read all lines of file
    for (int row = 0; row < 4; row++) {
        for (int col = 0; col < 3; col++) {
            // read one line of file at a time
            // store the line in a string variable using something like this:
            line = inFile.readLine();
            // store this line in the array
            Array1[row][col] = line;
        }
        //close the file
        inFile.close();
    }
} catch (FileNotFoundException ex) {
    System.out.println("No file with that name found!");
} catch (IOException ex) {
 System.out.println("An error occurred while trying to read the file!");
}

Step6: Print the array contents

for (int row = 0;row < 4; row++) {
    for (int col = 0; col < 3; col++) {
        System.out.print(Array1[row][col] + ” “);
    }
    System.out.println ();
}

Sample input: A text file with 12 lines like this

Apple
Banana
Cherry
Durian
Elderberry
Fig
Grape
Huckleberry
Jackfruit
Kumquat
Lime
Mango

Sample output.

Apple Banana Cherry
Durian Elderberry Fig
Grape Huckleberry Jackfruit
Kumquat Lime Mango
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Dear Student ,

As per the requirement submitted above , kindly find the below solution.

Here a new java program with name "Lab12.java" is created, which contains following code.

Lab12.java :

import java.util.Scanner;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
public class Lab12 {

   public static void main(String[] args) {
       // A String for the strings that we will read from file.
       String line;
       String [] [] Array1 = new String [4][3];
       // put all the commands of this step in a try block
       try {
       // Make a fileReader object to open the file that you want
       // You the name of the file that you made in step 6
       FileReader fr = new FileReader("fruits.txt");
       // define a BufferedReader named inFile to read from the file
       BufferedReader inFile=new BufferedReader(fr);
       // use a nested for loop structure to read all lines of file
       for (int row = 0; row < 4; row++) {
       for (int col = 0; col < 3; col++) {
       // read one line of file at a time
       // store the line in a string variable using something like this:
           line = inFile.readLine();
       // store this line in the array
       Array1[row][col] = line;
         
       }
         
       }
       //close the file
   inFile.close();
       } catch (FileNotFoundException ex) {
       System.out.println("No file with that name found!");
       } catch (IOException ex) {
       System.out.println("An error occurred while trying to read the file!");
       }
       for (int row = 0;row < 4; row++) {
       for (int col = 0; col < 3; col++) {
       System.out.print(Array1[row][col] + " ");
       }
       System.out.println ();
       }

   }

}

======================================================

Output : Compile and Run above program to get the screen as shown below

Screen 1 :fruits.txt

Screen 2:Lab12.java

NOTE : PLEASE FEEL FREE TO PROVIDE FEEDBACK ABOUT THE SOLUTION.

Add a comment
Know the answer?
Add Answer to:
Step 1: Getting Started Create a new .java file named Lab12.java. At the beginning of this...
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
  • package Lab11; import java.io.BufferedReader; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; public class Lab10 {    public...

    package Lab11; import java.io.BufferedReader; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; public class Lab10 {    public static void main (String [] args)    {    // ============================================================    // Step 2. Declaring Variables You Need    // These constants are used to define 2D array and loop conditions    final int NUM_ROWS = 4;    final int NUM_COLS = 3;            String filename = "Input.txt";    // A String variable used to save the lines read from input...

  • Modify the program that you wrote for the last exercise in a file named Baseball9.java that...

    Modify the program that you wrote for the last exercise in a file named Baseball9.java that uses the Player class stored within an array. The program should read data from the file baseball.txt for input. The Player class should once again be stored in a file named Player.java, however Baseball9.java is the only file that you need to modify for this assignment. Once all of the input data from the file is stored in the array, code and invoke a...

  • composed the following java code to read a string from a text file but receiving compiling...

    composed the following java code to read a string from a text file but receiving compiling errors. The text file is MyNumData.txt. Included the original java script that generated the output file. Shown also in the required output results after running the java program. I can't seem to search for the string and output the results. Any assistance will be greatly appreciated. import java.io.BufferedReader; import java.io.FileReader; import java.util.ArrayList; public class Main {   public static void main(String[] args) {     System.out.print("Enter the...

  • This is for Java. Create ONE method/function that will return an array containing the row and...

    This is for Java. Create ONE method/function that will return an array containing the row and column of the largest integer i the 2D array. If the largest number is located on row 2, column 1, the method needs to return row 2 and column one. Do not use more than one method. Use the code below as the main. Please comment any changes. in java Given the main, create a method that RETURNS the largest number found in the...

  • Implement a class CSVReader that reads a CSV file, and provide methods: int numbOfRows() int numberOfFields(int...

    Implement a class CSVReader that reads a CSV file, and provide methods: int numbOfRows() int numberOfFields(int row) String field(int row, int column) Please use the CSVReader and CSVReaderTester class to complete the code. I have my own CSV files and cannot copy them to here. So if possible, just use a random CSV file. CSVReader.java import java.util.ArrayList; import java.util.Scanner; import java.io.*; /**    Class to read and process the contents of a standard CSV file */ public class CSVReader {...

  • I need help writing this code for java class. Starter file: Project3.java and input file: dictionary.txt...

    I need help writing this code for java class. Starter file: Project3.java and input file: dictionary.txt Project#3 is an extension of the concepts and tasks of Lab#3. You will again read the dictionary file and resize the array as needed to store the words. Project#3 will require you to update a frequency counter of word lengths every time a word is read from the dictionary into the wordList. When your program is finished this histogram array will contain the following:...

  • Please help me fix my errors. I would like to read and write the text file...

    Please help me fix my errors. I would like to read and write the text file in java. my function part do not have errors. below is my code import java.io.FileInputStream; import java.io.InputStreamReader; import java.io.FileWriter; import java.io.IOException; public class LinkedList {    Node head;    class Node    {        int data;        Node next;       Node(int d)        {            data = d;            next = null;        }    }    void printMiddle()    {        Node slow_ptr...

  • JAVA Code: Complete the program that reads from a text file and counts the occurrence of...

    JAVA Code: Complete the program that reads from a text file and counts the occurrence of each letter of the English alphabet. The given code already opens a specified text file and reads in the text one line at a time to a temporary String. Your task is to go through that String and count the occurrence of the letters and then print out the final tally of each letter (i.e., how many 'a's?, how many 'b's?, etc.) You can...

  • 1. Copy the file secret.txt into a path that you can access. Read FilePath.doc if you...

    1. Copy the file secret.txt into a path that you can access. Read FilePath.doc if you have questions on file path. Copy SecretMessage.java into your NetBeans or other IDE tools. 2. Finish the main method that will read the file secret.txt, separate it into word tokens.You should process the tokens by taking the first letter of every fifth word, starting with the first word in the file. These letters should converted to capitals, then be appended to StringBuffer object to...

  • Please do the following project in C++ programming language. You can use a bag to create...

    Please do the following project in C++ programming language. You can use a bag to create a spell checker. The bag serves as a dictionary and contains a collection of correctly of correctly spelled workds. To see whether a word is spelled correctly, you see whether it is contained in the dictionary. Use this scheme to create a spell checker for the words in an external file. To simplify your task, restrict your dictionary to a manageable size. The dictionary...

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