Question: Need help to fix java code to get the right output Code: import java.util.Scanner; public class TextBox { public static void main(String[] args) { Scanner

Need help to fix java code to get the right output

Code:

import java.util.Scanner;

public class TextBox {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

int numLines = scanner.nextInt();

String[] lines = new String[numLines];

int maxLength = 0;

for (int i = 0; i < numLines; i++) {

lines[i] = scanner.nextLine();

maxLength = Math.max(maxLength, lines[i].length());

}

for (int i = 0; i < maxLength + 4; i++) {

System.out.print("+");

}

System.out.println();

for (int i = 0; i < numLines; i++) {

System.out.print("+ " + lines[i]);

for (int j = 0; j < maxLength - lines[i].length(); j++) {

System.out.print(" ");

}

System.out.println(" +");

}

for (int i = 0; i < maxLength + 4; i++) {

System.out.print("+");

}

}

}

Currently when the user inputs:

4

Two before narrow not relied how except moment myself.

Dejection assurance mrs led certainly.

So gate at no only none open.

Betrayed at properly it of graceful on.

The program outputs:

++++++++++++++++++++++++++++++++++++++++++++++

+ +

+ Two before narrow not relied how except moment myself. +

+ Dejection assurance mrs led certainly. +

+ So gate at no only none open. +

++++++++++++++++++++++++++++++++++++++++++++++

The desired program output:

++++++++++++++++++++++++++++++++++++++++++++++

+ Two before narrow not relied how except moment myself. +

+ Dejection assurance mrs led certainly. +

+ So gate at no only none open. +

+ Betrayed at properly it of graceful on. +

+++++++++++++++++++++++++++++++++++++++++++++++

If the user inputs a blank line and another line with words after:

2

Two before narrow not relied how except moment myself.

The program outputs:

++++ + + + + ++++

The desired output:

++++++++++++++++++++++++++++++++++++++++++++++ +Two before narrow not relied how except moment myself. + + + + ++++++++++++++++++++++++++++++++++++++++++++++

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!