Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Practical Database Programming With Visual Basic.NET

Authors: Ying Bai

1st Edition

0521712351, 978-0521712354

More Books

Students also viewed these Databases questions