设计和实现一个程序,程序中定义一个异常类 String TooLongException ,当一个字符串有太多的字符时,就拋出这个异常类的对象。在 main 方法中,读取用户输入的一行行字符串,直到用户输入“ DONE ”结束。如果输入的一个字符串,有过多字符(例如20个),就抛出异常。允许抛出异常终止程序。要求,定义一个方法,方法的参数是一个字符串,如果字符串超过20个字符,那么就拋出 String TooLongException 给调用者。
分成两个类:
自定义运行异常类
public class StringTooLongException extends RuntimeException{
public StringTooLongException() {
super();
}
public StringTooLongException(String message) {
super(message);
}
}
二、 测试类和测试方法
public class Test {
public static void main(String[] args) {
String[] attr = new String[100];
System.out.println("请输入字符串DONE结束");
int i = 0;
Scanner scanner = new Scanner(System.in);
attr[i] = scanner.next();
while(!"DONE".equals(attr[i])){
judge(attr[i]);
i++;
attr[i] = scanner.next();
}
}
/**
* 判断是否抛出异常
* @param str
*/
public static void judge(String str){
if(str.length() >= 20){
throw new StringTooLongException("字符数大于20");
}else{
System.out.println("字符串长度符合要求!");
}
}
}
测试结果:
获取到 根据length()判断好啦
这是你们的作业吧,我给你点提示:
自定义异常可以继承RuntimeException类,比如public class StringTooLongException extends RuntimeException
主动抛出异常用throw关键字,比如throw new StringTooLongException()
读取用户一行行的输出,在while循环里调用Scanner对象的readLine方法
直到用户输入DONE结束,可以将readLine的返回值与DONE做equals判断,如果为true,就停止readLine
有过多字符(例如20个),就抛出异常,可以判断readLine的返回值的长度是否大于20
这道题很简单,如果你能看懂我的提示,你就能做这道题,如果看不懂,建议看一些java入门的视频,把基础掌握好
class Scratch {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
while (true){
String str = scanner.nextLine();
if("DONE".equals(str)){
return;
}
stringLength(str);
}
}
private static void stringLength(String str) {
if (null != str && str.length() > 20) {
throw new StringTooLongException("字符串超过20个字符");
}
}
}
class StringTooLongException extends RuntimeException {
public StringTooLongException() {
}
public StringTooLongException(String message) {
super(message);
}
}
一个简单的实现:
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
// TODO Auto-generated method stub
String str[] = new String[100] ;
int i=0;
Scanner sc = new Scanner(System.in);
str[i] = sc.next();
while(!"DONE".equals(str[i])) {
try {
if(str[i].length()>20) {
throw new TooLongException();
}
}catch(TooLongException ex) {
ex.printStackTrace();
return ;
}
i++;
str[i] = sc.next();
}
for(int k=0;k<i;k++) {
System.out.println(str[k]);
}
}
}
class TooLongException extends Exception{
public TooLongException() {
// TODO Auto-generated constructor stub
super("字符串超过20个字符异常,程序退出!");
}
}
这个一定能看懂的~~~
import java.util.Scanner;
public class Test {
public static void main(String[] args) throws TooLongException {
String myString;
do {
Scanner s = new Scanner(System.in);
System.out.print("\n请输入一个字符串: ");
myString = s.next();
System.out.print("输入值为: "+myString + "\n");
} while (getString(myString) < 21 && !"DONE".equals(myString));
if (getString(myString) > 21) {
throw new TooLongException("太长了");
}
}
public static int getString(String str) {
return str.length();
}
}
class TooLongException extends Exception{
private final String message;
public TooLongException(String message) {
this.message = message;
}
@Override
public String getMessage() {
return message;
}
}