Question

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 file
   // -->


   // ============================================================
   // Step 3. Declare the Array for File Input
   // Please involve the two constants NUM_ROWS and NUM_COLS in the
   // constructor.
   // -->


   // ============================================================
   // Step 4: Preparing an Input File
   //
   // Make a text file and write 12 strings in it, one string in each line,
   // and then save it in the folder where your Lab10.java is.
   //
   // Note: If you are using Eclipse, please save the file out of "src"
   // folder.
   //
   // !!!!! NO CODE REQUIRED IN THIS SECTION !!!!!


   // ============================================================
   // Step 5. Reading the File
   // (Please make sure your input file is prepared in Step 4)
  
   // try-catch statement handles the exception (runtime error)
   //
   // There are two Exceptions might occur when reading a file
   //
   // - FileNotFoundException: thrown by FileReader when the file doesn't exist
   // - IOException: thrown by BufferedReader's readLine() when some I/O error occurs
   //
   // Please check the textbook for details
   try
   {
   // Instantiate a FileReader object to open the input file
   // Note: "filename" should match the input file you made in Step 4
              
   //FileReader fr = new FileReader(filename);
   FileReader fr = new FileReader("/autograder/submission/" + filename);
              
   // BufferedReader is for efficient reading of characters
   BufferedReader bfReader = new BufferedReader(fr);
  
   // Just like scanner.nextLine(), whenever you invoke .readLine()
   // on BufferedReader, it will read a single line from the file.
   //
   // As we have determined the number of lines in our file, we will
   // use constants to define the loop conditions
   for (int i = 0; i <= NUM_ROWS - 1; i++)
   {
   for (int j = 0; j <= NUM_COLS - 1; j++)
   {
   // Read a line from the file
   // Please invoke .readLine() on the BufferedReadere bfReader
   // and save the returned value to the element at array position (i, j)
   // -->
   }
   }

   // It is very important to close your file after reading is done
   bfReader.close();
   }
   catch (FileNotFoundException e)
   {
   System.err.println("File not found");
   }
   catch (IOException e)
   {
   System.err.println("I/O error occurs");
   }


   // ============================================================
   // Step 6. Print the Elements in the 2D Array

   for (int i = 0; i <= NUM_ROWS - 1; i++)
   {
   for (int j = 0; j <= NUM_COLS - 1; j++)
   {
   // Print the element at position (i, j)
   // -->
       System.out.println();
   }
   System.out.println();
   }

   } // End of main
   } // End of class

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Note: Could you plz go through this code and let me know if u need any changes in this.Thank You
_________________

// Input.txt

Hello
How Are
You
Are So
Beautiful
When
I can
Meet You
Shall
We go
To Hotel To
Have Date
____________________

// Lab10.java

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 file
       // -->
String line;
       // ============================================================
       // Step 3. Declare the Array for File Input
       // Please involve the two constants NUM_ROWS and NUM_COLS in the
       // constructor.
       // -->
String strArray[][]=new String[NUM_ROWS][NUM_COLS];
       // ============================================================
       // Step 4: Preparing an Input File
       //
       // Make a text file and write 12 strings in it, one string in each line,
       // and then save it in the folder where your Lab10.java is.
       //
       // Note: If you are using Eclipse, please save the file out of "src"
       // folder.
       //
       // !!!!! NO CODE REQUIRED IN THIS SECTION !!!!!

       // ============================================================
       // Step 5. Reading the File
       // (Please make sure your input file is prepared in Step 4)

       // try-catch statement handles the exception (runtime error)
       //
       // There are two Exceptions might occur when reading a file
       //
       // - FileNotFoundException: thrown by FileReader when the file doesn't
       // exist
       // - IOException: thrown by BufferedReader's readLine() when some I/O
       // error occurs
       //
       // Please check the textbook for details
       try {
           // Instantiate a FileReader object to open the input file
           // Note: "filename" should match the input file you made in Step 4

           // FileReader fr = new FileReader(filename);
           FileReader fr = new FileReader(filename);

           // BufferedReader is for efficient reading of characters
           BufferedReader bfReader = new BufferedReader(fr);

           // Just like scanner.nextLine(), whenever you invoke .readLine()
           // on BufferedReader, it will read a single line from the file.
           //
           // As we have determined the number of lines in our file, we will
           // use constants to define the loop conditions
           for (int i = 0; i <= NUM_ROWS - 1; i++) {
               for (int j = 0; j <= NUM_COLS - 1; j++) {
                   // Read a line from the file
                   // Please invoke .readLine() on the BufferedReadere bfReader
                   // and save the returned value to the element at array
                   // position (i, j)
                   // -->
                   strArray[i][j]=bfReader.readLine();
               }
           }

           // It is very important to close your file after reading is done
           bfReader.close();
       } catch (FileNotFoundException e) {
           System.err.println("File not found");
       } catch (IOException e) {
           System.err.println("I/O error occurs");
       }

       // ============================================================
       // Step 6. Print the Elements in the 2D Array

       for (int i = 0; i <= NUM_ROWS - 1; i++) {
           for (int j = 0; j <= NUM_COLS - 1; j++) {
               // Print the element at position (i, j)
               // -->
               System.out.printf("%-15s",strArray[i][j]);
           }
           System.out.println();
       }

   } // End of main
} // End of class

__________________________

Output:

Hello How Are You
Are So Beautiful When   
I can Meet You Shall
We go To Hotel To Have Date


_______________Could you plz rate me well.Thank You

Add a comment
Know the answer?
Add Answer to:
package Lab11; import java.io.BufferedReader; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; public class Lab10 {    public...
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
  • Complete the code: package hw4; import java.io.File; import java.io.IOException; import java.util.LinkedList; import java.util.Scanner; /* * This...

    Complete the code: package hw4; import java.io.File; import java.io.IOException; import java.util.LinkedList; import java.util.Scanner; /* * This class is used by: * 1. FindSpacing.java * 2. FindSpacingDriver.java * 3. WordGame.java * 4. WordGameDriver.java */ public class WordGameHelperClass { /* * Returns true if an only the string s * is equal to one of the strings in dict. * Assumes dict is in alphabetical order. */ public static boolean inDictionary(String [] dict, String s) { // TODO Implement using binary search...

  • QUESTION The ReadFile class opens and reads a text file. The WriteFile class opens and writes...

    QUESTION The ReadFile class opens and reads a text file. The WriteFile class opens and writes to a file. Compile and run these programs. There are several ways to read/write text files. The examples shown here are just one way of achieving this. Look at the API for the BufferedReader class. The readline() method is a simple way to read the text file line by line. It returns null when the end of the file has been reached. https://docs.oracle.com/javase/8/docs/api/java/io/BufferedReader.html Look...

  • Solver.java package hw7; import java.util.Iterator; import edu.princeton.cs.algs4.Graph; import edu.princeton.cs.algs4.BreadthFirstPaths; public class Solver {    public static...

    Solver.java package hw7; import java.util.Iterator; import edu.princeton.cs.algs4.Graph; import edu.princeton.cs.algs4.BreadthFirstPaths; public class Solver {    public static String solve(char[][] grid) {        // TODO        /*        * 1. Construct a graph using grid        * 2. Use BFS to find shortest path from start to finish        * 3. Return the sequence of moves to get from start to finish        */               // Hardcoded solution to toyTest        return...

  • What is the output of the following question? import java.io.IOException; import java.util. EmptyStackException: public class newclass...

    What is the output of the following question? import java.io.IOException; import java.util. EmptyStackException: public class newclass public static void main(String[] args) try System.out.printf("%d", 1); throw (new Exception()); catch (IOException e) System.out.printf("%d", 2); catch(EmptyStackException e) System.out.printf("%d", 3); catch(Exception e) System.out.printf("%d", 4); } finally System.out.printf("%d", 5); Q3) (15 points) a.Write a JAVA program that reads an arra JAVA program that reads an array from input file and invo Sort and Max ,that sorts the elements of the array and s the elements...

  • complete this in java import java.io.File; import java.io.FileNotFoundException; import java.util.ArrayList; import java.util.Random; import java.util.Scanner; public class...

    complete this in java import java.io.File; import java.io.FileNotFoundException; import java.util.ArrayList; import java.util.Random; import java.util.Scanner; public class WordDetective { /** * Finds the specified set of words in the specified file and returns them * as an ArrayList. This finds the specified set in the file which is on the * line number of the set. The first line and set in the file is 1. * * This returns an ArrayList with the keyword first, then : and then followed...

  • Public class File {    public String base; //for example, "log" in "log.txt"  &nbs...

    public class File {    public String base; //for example, "log" in "log.txt"    public String extension; //for example, "txt" in "log.txt"    public int size; //in bytes    public int permissions; //explanation in toString public String getExtension() {        return extension;    } public void setExtension(String e) {           if(e == null || e.length() == 0)            extension = "txt";        else            extension = e;    } public class FileCollection {    private File[] files;    /**    * DO NOT MODIFY    * Loads collection from input file    * @param input: name...

  • 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...

  • This is the contents of Lab11.java import java.util.Scanner; import java.io.*; public class Lab11 { public static...

    This is the contents of Lab11.java import java.util.Scanner; import java.io.*; public class Lab11 { public static void main(String args[]) throws IOException { Scanner inFile = new Scanner(new File(args[0])); Scanner keyboard = new Scanner(System.in);    TwoDArray array = new TwoDArray(inFile); inFile.close(); int numRows = array.getNumRows(); int numCols = array.getNumCols(); int choice;    do { System.out.println(); System.out.println("\t1. Find the number of rows in the 2D array"); System.out.println("\t2. Find the number of columns in the 2D array"); System.out.println("\t3. Find the sum of elements...

  • Java Project Draw a class diagram for the below class (using UML notation) import java.io.BufferedReader; import...

    Java Project Draw a class diagram for the below class (using UML notation) import java.io.BufferedReader; import java.io.FileWriter; import java.io.IOException; import java.io.InputStreamReader; public class myData { public static void main(String[] args) { String str; BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); System.out.println("Enter text (‘stop’ to quit)."); try (FileWriter fw = new FileWriter("test.txt")) { do { System.out.print(": "); str = br.readLine(); if (str.compareTo("stop") == 0) break; str = str + "\r\n"; // add newline fw.write(str); } while (str.compareTo("stop") != 0); } catch (IOException...

  • Trying to practice this assignment Argument list: the *yahoonews.txt Data file: yahoonews.txt Wr...

    Trying to practice this assignment Argument list: the *yahoonews.txt Data file: yahoonews.txt Write a program named WordCount.java, in this program, implement two static methods as specified below: public static int countWord(Sting word, String str) this method counts the number of occurrence of the word in the String (str) public static int countWord(String word, File file) This method counts the number of occurrence of the word in the file. Ignore case in the word. Possible punctuation and symbals in the file...

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