Lỗi NumberFormatException trong java

Khi nhâp điểm 4.3 thì chương trình ra lỗi:Exception in thread “main” java.lang.NumberFormatException: For input string: “4.3”
Mọi người chỉ lỗi và cách giải quyết giúp em vs!!

try{
	while(check == false){
	        System.out.println("Nhap vao diem: ");
		float ndiem =Integer.parseInt(input.nextLine());
		if(ndiem<0 || ndiem>10){
				throw new NgoaiLe2();
		}
		check = true;
	}
}catch(NgoaiLe2 b){
	System.out.println(b);
}		
1 Like

hàm parseInt nằm trong lớp Integer sẽ chuyển đối số String truyền vào thành kiểu int, mà nó chỉ làm việc với int type thôi. Muốn truyền vào kiểu nào thì bạn phải dùng đúng lớp của kiểu đó:

double d = Double.parseDouble(String s);
float f = Float.parseFloat(String s); 


Your case:

float ndiem = Float.parseFloat(input.nextLine());
3 Likes
83% thành viên diễn đàn không hỏi bài tập, còn bạn thì sao?