John Squirrels
ระดับ
San Francisco

NumberFormatException ใน Java

เผยแพร่ในกลุ่ม

NumberFormatException ใน Java คืออะไร

“NumberFormatException ถูกส่งไปที่การแปลงสตริงที่ไม่ใช่ตัวเลขเป็นตัวเลข”
พูดง่ายๆ ถ้าสตริงไม่ได้เก็บเฉพาะข้อมูลที่เป็นตัวเลขและคุณพยายามแปลงเป็นตัวเลข คุณจะพบข้อยกเว้นนี้ สิ่งนี้สามารถตีความได้อีกทางหนึ่ง ข้อยกเว้นถูกใช้เป็นตัวบ่งชี้หากไม่สามารถแปลงสตริงเป็นตัวเลขได้ จำนวนสามารถเป็นจำนวนเต็มทศนิยมหรือทศนิยมตามความต้องการของคุณ ตัวอย่างเช่น หากสตริงอินพุตประกอบด้วยตัวอักษรทั้งหมด อักขระที่เป็นตัวอักษรและตัวเลข หรืออักขระพิเศษ และคุณพยายามแปลงจากสตริงเป็นจำนวนเต็มดังนั้น NumberFormatException จะถูกส่งออกไป

ตัวอย่าง

ลองดูตัวอย่างง่ายๆเพื่อทำความเข้าใจสิ่งนี้
import java.util.Date;

public class NumberFormatExceptionTestDriver {

	public static void main(String[] args) {

		try {
			// a valid Integer number in the input String
			String inputString = "2550";
			System.out.println("Integer.pareseInt(" + inputString + ") = " + Integer.parseInt(inputString));
		} catch (NumberFormatException e) {
			// java.lang.NumberFormatException will be thrown if the
			// input string can not be converted to a valid integer
			System.out.println("\nNumberFormatException thrown! " + e.getMessage());
		}

		try {
			// a floating number in the input String
			// use Float.parseFloat(inputString) to avoid this exception
			String inputString = "255.0";
			System.out.println("Integer.pareseInt(" + inputString + ") = " + Integer.parseInt(inputString));
		} catch (NumberFormatException e) {
			System.out.println("\nNumberFormatException thrown! " + e.getMessage());
		}

		try {
			Date day = new Date();
			// date containing alpha-numeric data in the input string
			String inputString = day.toString();
			System.out.println("Integer.pareseInt(" + inputString + ") = " + Integer.parseInt(inputString));
		} catch (NumberFormatException e) {
			System.out.println("\nNumberFormatException thrown! " + e.getMessage());
		}

		try {
			// numbers with spaces in the input string
			String inputString = "1 2 3";
			System.out.println("Integer.pareseInt(" + inputString + ") = " + Integer.parseInt(inputString));
		} catch (NumberFormatException e) {
			System.out.println("\nNumberFormatException thrown! " + e.getMessage());
		}

		try {
			// all letters in the input string
			String inputString = "Lubaina Khan";
			System.out.println("Integer.pareseInt(" + inputString + ") = " + Integer.parseInt(inputString));

		} catch (NumberFormatException e) {
			System.out.println("\nNumberFormatException thrown! " + e.getMessage());
		}
	}
}

เอาต์พุต

Integer.pareseInt(2550) = 2550 NumberFormatException โยนทิ้ง! สำหรับสตริงอินพุต: "255.0" NumberFormatException ถูกโยนทิ้ง! สำหรับสตริงอินพุต: "วันพุธที่ 11 สิงหาคม 08:18:21 PKT 2021" NumberFormatException โยนทิ้ง! สำหรับสตริงอินพุต: "1 2 3" NumberFormatException ถูกโยนทิ้ง! สำหรับสตริงอินพุต: "Lubaina Khan"

บทสรุป

เราหวังว่าคุณจะเข้าใจว่าเหตุใด NumberFormatException จึงเกิดขึ้นใน Java หากคุณเข้าใจสาเหตุที่แท้จริงแล้ว คุณสามารถระบุสาเหตุและแก้ไขได้เสมอ หากคุณยังไม่ชัดเจนเกี่ยวกับแนวคิดนี้ ลองทำผิดให้มากขึ้น แล้วคุณจะได้เรียนรู้ถึงสาเหตุและวิธีการของปัญหานี้ จนกว่าจะเติบโตและเรียนรู้ต่อไป
ความคิดเห็น
  • เป็นที่นิยม
  • ใหม่
  • เก่า
คุณต้องลงชื่อเข้าใช้เพื่อแสดงความคิดเห็น
หน้านี้ยังไม่มีความคิดเห็นใด ๆ