Question: Modify the program such that the strings which were read from the user input get written to an output file called userStrings.txt after the user
Modify the program such that the strings which were read from the user input get written to an output file called userStrings.txt after the user enters DONE.
import java.util.Scanner; public class StringDriver { public static void main(String[] args) { Scanner in = new Scanner(System.in); String s; while (true) { try { System.out.print("Enter a string (DONE to quit): "); s = in.nextLine(); if (s.length() > 5) throw new StringTooLongException("String is longer than 5 characters."); if (s.equals("DONE")) break; } catch (StringTooLongException e) { // move try catch block into inside the while loop System.out.println(e.getMessage()); } } } }
public class StringTooLongException extends Exception { //----------------------------------------------------------------- // Sets up the exception object with a particular message. //----------------------------------------------------------------- public StringTooLongException(String message) { super(message); } } Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
