Write a program that reads in lines from the input Each line

Write a program that reads in lines from the input. Each line has two integer values followed by a single character. The objective is to print a rectangle pattern using the input pattern. The first integer represents the number of rows and the second integer represents the number of columns.

Input - The format of an input line is noRows noCols char

The first two are integers (and can be read using nextInt()).

The last one is a single character (like *) and can be read using next() as a String and then using charAt(0) to get at the first character.

Output - For each line of input, a rectangular pattern is printed consisting of several rows. If the no of rows or cols is less than or equal to zero or more than 50, the program prints out an error statement as shown in the sample output.

Sample Input

2 2 ,

2 51 +

10 10 =

0 11 *

Sample Output

,, ,,

error in input

==========

==========

==========

==========

==========

==========

==========

==========

==========

==========

error in input

HINT 1. use in.next().charAt(0) to get the character. 2. don’t forget to discard the rest of the input line using in.nextLine() if you are using in.hasNextLine() to check if there is a next line. 3. You will need TWO loops to print each rectangle.

Solution

import java.io.File;
import java.util.Scanner;

public class PrintRectangle {

   /**
   * @param args
   */
   public static void main(String[] args) {

       Scanner scanner = null;
       try {
           scanner = new Scanner(new File(\"inputdata.txt\"));
           while (scanner.hasNext()) {
               int rows = scanner.nextInt();
               int cols = scanner.nextInt();
               char ch = scanner.next().charAt(0);
               if (!generateRectangle(rows, cols, ch))
                   System.out.println(\"error in input\");
               else
                   System.out.println();
           }

       } catch (Exception e) {
           // TODO: handle exception
       }
   }

   /**
   * method to print the rectangle based on rows , cols and ch
   *
   * @param rows
   * @param cols
   * @param ch
   * @return
   */
   public static boolean generateRectangle(int rows, int cols, char ch) {

       if ((rows <= 0 || cols <= 0) || (rows > 50 || cols > 50)) {
           return false;

       }
       for (int i = 0; i < rows; i++) {
           for (int j = 0; j < cols; j++) {
               System.out.print(ch);
           }
           System.out.println();
       }

       return true;

   }
}

inputdata.txt

2 2 ,
2 51 +
10 10 =
0 11 *

OUTPUT:

,,
,,

error in input
==========
==========
==========
==========
==========
==========
==========
==========
==========
==========

error in input

Write a program that reads in lines from the input. Each line has two integer values followed by a single character. The objective is to print a rectangle patte
Write a program that reads in lines from the input. Each line has two integer values followed by a single character. The objective is to print a rectangle patte
Write a program that reads in lines from the input. Each line has two integer values followed by a single character. The objective is to print a rectangle patte

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site