Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

please show output of this code as below output= please enter a String; java cor

ID: 3853162 • Letter: P

Question

please show output of this code

as below

output= please enter a String;

java

correct any errors in the code below and show OUTPUT AS MENTIONED ABOVE

import java.io.*;
//Main class
public class Driver
{
public static void main(String[] args)
throws IOException
{
//declaring String variable
String str;
BufferedReader br=new BufferedReader
(new InputStreamReader(System.in));
System.out.println("Please enter a String:");
str=br.readLine();
while(!str.equals("DONE"))
{
int i=1;
try
{
while(i>0)
{
System.out.println("Please enter"+" a String:");
str=br.readLine();
if(str.equals("DONE"))
{
break;
}
else if(str.length()>20)
{
throw

new StringTooLongException();
}
else
i++;
}
}
catch(StringTooLongException st)
{
System.out.println
("String has too many"+"characters:"+st);
System.out.println("Please enter"+" a String:");
str=br.readLine();
}
}
}
}

Explanation / Answer

Hi

I have fixed the issue and highlighted the code changes below.

Driver.java

import java.io.*;

//Main class

public class Driver {

public static void main(String[] args) throws IOException {

// declaring String variable

String str;

BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

System.out.println("Please enter a String:");

str = br.readLine();

while (!str.equals("DONE")) {

int i = 1;

try {

while (i > 0) {

System.out.println("Please enter" + " a String:");

str = br.readLine();

if (str.equals("DONE")) {

break;

} else if (str.length() > 20) {

throw new StringTooLongException();

} else

i++;

}

} catch (StringTooLongException st) {

System.out.println("String has too many" + "characters:" + st);

System.out.println("Please enter" + " a String:");

str = br.readLine();

}

}

}

}

StringTooLongException.java

public class StringTooLongException extends Exception{

String errorMsg ;

public StringTooLongException() {

}

public StringTooLongException(String s){

this.errorMsg = s;

}

public String toString(){

return (errorMsg ) ;

}

}

Output:

Please enter a String:
java
Please enter a String:
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
String has too manycharacters:null
Please enter a String:
hai
Please enter a String:
DONE