Q1: Write a class that throws an ExtraneousStringException when a string has mor
ID: 3862127 • Letter: Q
Question
Q1: Write a class that throws an ExtraneousStringException when a string has more than 30 characters in it (see ExtraneousStringException.java. This java file does not need to be modified). Copy paste the contents of ExtraneousStringException.java into a new .java file, uncomment the commented out code, and write your code inside the main method. In the driver class, keep reading strings from the user until the user enters “DONE.” You may read your input from a file, or use the console. public class LastName_HW01Q1 { public static void main(String[] args) throws ExtraneousStringException { // Write your code here. } */ static class ExtraneousStringException extends Exception { public ExtraneousStringException() { super("String too long"); } } }
Explanation / Answer
Solution for the above problem
I have written code in two files
1) ExtraneousStringException.java
2) driver class ie LastName_HW01Q1.java
1)ExtraneousStringException.java
class ExtraneousStringException extends Exception {
public ExtraneousStringException() {
super("String too long");
}
}
2)Driver class
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class LastName_HW01Q1
{
public static void main(String[] args) throws ExtraneousStringException,IOException
{
// Write your code here.
//buffered reader for taking console input
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String text = "";
//for accepting user inputs untill user type DONE
while(true)
{
System.out.println("Please enter a string: ");
text = br.readLine();
if (!text.equals("DONE"))
{
int len=text.length(); //find string length
//checking user given string contains above 30 characters or not
if(len>30){
//if not throw the exception
System.out.println("Length of the given string"+len);
throw new ExtraneousStringException();
}
//It will show user input if only more than 30 characters
System.out.println(text);
}
else
break;// if user type DONE loop will terminates
}
}
}
/////another way is to write code .. both classes in one class as follows
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class LastName_HW01Q1
{
public static void main(String[] args) throws ExtraneousStringException,IOException
{
// Write your code here.
//buffered reader for taking console input
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String text = "";
//for accepting user inputs untill user type DONE
while(true)
{
System.out.println("Please enter a string: ");
text = br.readLine();
if (!text.equals("DONE"))
{
int len=text.length(); //find string length
//checking user given string contains above 30 characters or not
if(len>30){
//if not throw the exception
System.out.println("Length of the given string"+len);
throw new ExtraneousStringException();
}
//It will show user input if only more than 30 characters
System.out.println(text);
}
else
break;// if user type DONE loop will terminates
}
}
static class ExtraneousStringException extends Exception {
public ExtraneousStringException() {
super("String too long");
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.