Topic: Java Programming. (LEVEL: easy) Below are two separate codes which, when
ID: 3681979 • Letter: T
Question
Topic: Java Programming. (LEVEL: easy)
Below are two separate codes which, when saved under one folder, run a program.
The current program throws an exception if you enter a string with more than 20 characters, and then terminates the program.
Task: Modify the code so that it catches and handles the exception if it is thrown. Handle the exception by printing an appropriate message, then continue processing more strings. Comment on the modified piece of code.
//********************************************************************
// ReadStrings.java Author: Lewis/Loftus
//
// Solution to Programming Project 11.1
//********************************************************************
import java.util.Scanner;
public class ReadStrings
{
//-----------------------------------------------------------------
// Creates an exception object and throws it as appropriate.
//-----------------------------------------------------------------
public static void main (String[] args) throws StringTooLongException
{
final int MAX = 20;
String input = "";
Scanner scan = new Scanner (System.in);
StringTooLongException lengthException =
new StringTooLongException ("String has too many characters");
System.out.println("Enter strings, enter DONE when finished:");
do
{
input = scan.nextLine();
if (!input.equals("DONE"))
if (input.length() > MAX)
throw lengthException;
else
System.out.println ("You entered: " + input);
}
while (!input.equals("DONE"));
System.out.println("Program successfully completed");
}
}
//********************************************************************
// StringTooLongException.java Author: Lewis/Loftus
//
// Solution to Programming Projects 11.1 and 11.2
//********************************************************************
public class StringTooLongException extends Exception
{
//-----------------------------------------------------------------
// Sets up the exception object with a particular message.
//-----------------------------------------------------------------
public StringTooLongException (String message)
{
super (message);
}
}
Explanation / Answer
Hi, I have added required code to handle exception.
There is no change in StringTooLongException.java.
import java.util.Scanner;
public class ReadStrings {
// -----------------------------------------------------------------
// Creates an exception object and throws it as appropriate.
// -----------------------------------------------------------------
public static void main(String[] args) throws StringTooLongException {
final int MAX = 20;
String input = "";
Scanner scan = new Scanner(System.in);
StringTooLongException lengthException = new StringTooLongException(
"String has too many characters");
System.out.println("Enter strings, enter DONE when finished:");
do {
// putting error prone code into try block
try {
input = scan.nextLine();
if (!input.equals("DONE"))
if (input.length() > MAX)
throw lengthException;
else
System.out.println("You entered: " + input);
} // this is catch block that handles thrown exception
catch (StringTooLongException e) {
System.out.println(e.getMessage()); // printing message
}
} while (!input.equals("DONE"));
System.out.println("Program successfully completed");
}
}
/*
Sample run:
Enter strings, enter DONE when finished:
sDWWFEFEFEF4RT4GGRHFDSGEFWAFWDFWFEWF3QFWQF
String has too many characters
FWEFWEFEWGRTGRGWEFWAFGEWFWFWQG
String has too many characters
WRQRQ2Q
You entered: WRQRQ2Q
2 R2 R2 3
You entered: 2 R2 R2 3
R2 R2 3ER
You entered: R2 R2 3ER
FGEGRTETGEFEGRTERFRE4RGEWFRET4T43
String has too many characters
done
You entered: done
DONE
Program successfully completed
*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.