|
i have a program that has an exception class called StringTooLongException. I also have a class called InputString. I want to prompt the user to input a series of characters and when the user is done they will enter DONE. My exception class is supposed to see if the string is greater than 20 characters. If its greater than 20, it will throw the problem and print a message. Here is what i have....
import java.util.Scanner;
public class InputString
{
public static void main(String[]args) throws Exception
{
final int MAX=20;
String str;
int num;
Scanner scan = new Scanner(System.in);
StringTooLongException problem = new StringTooLongException("Input String is too long."); //******i get an error here saying it can't find the constructor****/
System.out.println("Enter a string of characters" + "When finsihed type DONE");
str = scan.nextLine();
while (!str.equals("DONE"))
{
try
{
num = Integer.parseInt(str);
if(num > MAX)
throw problem;//****error here...saying incompatible types...required "Throwable"*****/
}
catch(StringTooLongException problem)
{
System.out.println("Input String is too long.");
}
}
System.out.println("End of main method.");
}
}
public class StringTooLongException extends Exception
{
StringTooLongException(String message)
{
super (message);
}
}
|
|
|
import java.util.Scanner;
public class InputString
{
public static void main(String[]args) throws StringTooLongException
{
final int MAX=20;
String str = "";
int num;
Scanner scan = new Scanner(System.in);
while (!str.equals("DONE"))
{
System.out.println("Enter a string of characters" + "When finsihed type DONE");
str = scan.nextLine();
num = str.length();
if(num > MAX)
throw new StringTooLongException("length " + num +
" > " + MAX);
}
System.out.println("End of main method.");
}
}
class StringTooLongException extends Exception
{
StringTooLongException()
{
super();
}
StringTooLongException(String message)
{
super (message);
}
}
|
|
|
|
|
|
|
import java.util.Scanner;
public class InputString
{
public static void main(String[]args) throws Exception
{
final int MAX=20;
String str = "";
int num;
Scanner scan = new Scanner(System.in);
while (!str.equals("DONE"))
{
StringTooLongException problem = new StringTooLongException("Input String is too long.");
System.out.println("Enter a string of characters" + "When finsihed type DONE");
str = scan.nextLine();
num = str.length();
try
{
if(num > MAX)
throw problem;
}
catch(StringTooLongException ex)
{
System.err.println("Input String is too long.");
}
}
System.out.println("End of main method.");
}
}
class StringTooLongException extends Exception
{
StringTooLongException(String message)
{
super (message);
}
}
|
|
|
|
|
|
|
|