java异常输出两次思考

next与nextLine的区别

next()方法:读取到有效字符后才可以结束输入,对输入有效字符之前遇到的空格键,Tab键或者Enter键等结束符,next()方法会自动将其去掉,只有在输入有效字符之后,next()方法才将其后输入的空格键、Tab键或者Enter键等视为分隔符或者结束符。next方法不能得到带空格的字符串。

nextLine()方法:结束符只是Enter键,nextLine()方法返回的是Enter键之前的所有字符,它是可以得到带空格的字符串的。

java 复制代码
public static void nextInput(){ 	
    Scanner scan = new Scanner(System.in);
    System.out.println("请输入第一个字符串");
    String str1 = scan.nextLine();
    System.out.println("请输入第二个字符串");
    String str2 = scan.next();
    System.out.println("输入的字符串是:"+str1 + str2);
    scan.close();
          }

运行结果:

请输入第一个字符串

home work

请输入第二个字符串 over 输入的字符串是:home work over 第一种先用next,再用nextline,这里home work输入完又加了一个换行符,这个换行符被scan.nextLine()读取,所有str2输入不了,被赋值为 work

java 复制代码
public static void firstInput() {
        Scanner scan = new Scanner(System.in);
        System.out.println("请输入第一个字符串");
        String str1 = scan.next();
        System.out.println("第一次str1读入的是:"+str1);
        System.out.println("请输入第二个字符串");
        String str2 = scan.nextLine();
        System.out.println("第二次str2读入的是:"+ str2);
        System.out.println("输入的字符串是:"+ str1 + str2);
        scan.close();
}

运行结果:

请输入第一个字符串

home work

请输入第二个字符串

第二次str2读入的是: work

输入的字符串是:home work
nextLine()自动读取了next()读取完结束符之后剩余的部分,所有没办法给str2从键盘输入值,这里解决办法就是在每一个next()语句后加一句nextLine()语句,nextDouble()、nextFloat()、nextInt()等也存在这个问题。解决办法也是语句之后加一个nextLine()语句。

java 复制代码
//不加nextLine()语句
public static void nextInputnextInt() {
        //没加nextLine()语句,输入示例如下:
        Scanner scan = new Scanner(System.in);
        System.out.println("请输入第一个数字");
        int s1 = scan.nextInt();
        String str2 = scan.nextLine();
        System.out.println("第二次str2读入的是:"+str2);
        System.out.println("第一次str1与第二次str2读入的是:"+ s1 + str2);
        scan.close();
}

请输入第一个数字

-111 -67

-111

第二次str2读入的是: -67

第一次str1与第二次str2读入的是:-111 -67

java 复制代码
////加nextLine()语句
public static void nextTest(){
    // 加上scan.nextLine();解决后可以正常输入
    Scanner scan = new Scanner(System.in);
    System.out.println("请输入第一个数字");
    String s1 = scan.next();
    scan.nextLine();
    System.out.println(s1);
    System.out.println("请输入一个字符串");
    String str2 = scan.nextLine();
    System.out.println("第二次str2读入的是:"+str2);
    System.out.println("第一次s1与第二次str2读入的是:"+ s1 + str2);
    scan.close();
}

请输入第一个数字

666 -99

666

请输入一个字符串

老铁

第二次str2读入的是:老铁

第一次s1与第二次str2读入的是:666老铁

自己遇到的问题

代码如下:

java 复制代码
package chapter8.homework3_2;

import java.util.InputMismatchException;
import java.util.Scanner;

class IllegalInputException extends RuntimeException{
	public IllegalInputException(String message) {
		super(message);
	}
	
}
public class CalRadio {
	double radio;
	double bill;
	public void calRadio(int electricity) {
		if(electricity < 0) {
			throw new IllegalInputException("度数必须大于0");
		}
		else if(electricity <= 240) {
			bill = electricity * 0.5;
		}
		else if(electricity <= 400) {
			bill = electricity * 0.53;
		}
		else {
			bill = electricity * 0.78;
		}
		String str = String.format("电费为:%.0f,电费与用电量比值为:%.2f", bill,bill/electricity);
		System.out.println(str);
	}
	public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);
		System.out.println("输入本月用电度数");
//		写在这里不能输入下个用电量,因为一直读取的都是之前的用电量
		int electricity = scan.nextInt();
		while(true) {
			try {
				CalRadio radio = new CalRadio();
				System.out.println("电量是"+electricity);
				radio.calRadio(electricity);
				break;
			}catch(InputMismatchException e) {
				System.out.println("输入用电数不是整数类型,请重新输入");
				scan.nextLine();
			}catch(IllegalInputException ee) {
				System.out.println(ee);
				scan.nextLine();
			}
		}
		scan.close();
	}

}
makefile 复制代码
输入本月用电度数
-9
电量是-9
chapter8.homework3_2.IllegalInputException: 度数必须大于0
电量是-9
chapter8.homework3_2.IllegalInputException: 度数必须大于0

这里用电度数输入为-9时,会报两次异常。个人通过了解scan.nextLine()的用法得知,是因为当输入-9时,会带一个回车,而scan.nextInt()只会仅读取整数部分,并会在读取到整数之后停止,留下输入流中整数后面的任何字符(包括空格、制表符或换行符)。这时捕抓到异常,scan.nextLine()读取开始的位置是:scan.nextInt()读取完成的下一个位置,这里是换行符,读取完,再次进入while循环,然后electricity变量的值没有变,所以再报一次异常,此时scan.nextLine()已经读取不到什么内容,等待用户输入,自然停止下来。如果按下回车符,会再次出现异常。

相关推荐
【D'accumulation】几秒前
令牌主动失效机制范例(利用redis)注释分析
java·spring boot·redis·后端
2401_8543910810 分钟前
高效开发:SpringBoot网上租赁系统实现细节
java·spring boot·后端
Cikiss19 分钟前
微服务实战——SpringCache 整合 Redis
java·redis·后端·微服务
Cikiss20 分钟前
微服务实战——平台属性
java·数据库·后端·微服务
OEC小胖胖34 分钟前
Spring Boot + MyBatis 项目中常用注解详解(万字长篇解读)
java·spring boot·后端·spring·mybatis·web
2401_857617621 小时前
SpringBoot校园资料平台:开发与部署指南
java·spring boot·后端
计算机学姐1 小时前
基于SpringBoot+Vue的在线投票系统
java·vue.js·spring boot·后端·学习·intellij-idea·mybatis
Yvemil72 小时前
MQ 架构设计原理与消息中间件详解(二)
开发语言·后端·ruby
2401_854391082 小时前
Spring Boot大学生就业招聘系统的开发与部署
java·spring boot·后端
虽千万人 吾往矣3 小时前
golang gorm
开发语言·数据库·后端·tcp/ip·golang