JAVA STRING TO INT

     

If you have worked in Java Swing, it has components such as JTextField & JTextArea which we use khổng lồ get our input đầu vào from the GUI. It takes our input as a string.

If we want to make a simple calculator using Swing, we need khổng lồ figure out how lớn convert a string lớn an integer. This leads us to the question – how can we convert a string lớn an integer?

In Java, we can use Integer.valueOf() & Integer.parseInt() to lớn convert a string to an integer.

1. Use Integer.parseInt() to lớn Convert a String to lớn an Integer

This method returns the string as a primitive type int. If the string does not contain a valid integer then it will throw a NumberFormatException.

So, every time we convert a string to an int, we need lớn take care of this exception by placing the code inside the try-catch block.

Let"s consider an example of converting a string khổng lồ an int using Integer.parseInt():

String str = "25"; try int number = Integer.parseInt(str); System.out.println(number); // output = 25 catch (NumberFormatException ex) ex.printStackTrace(); Let"s try lớn break this code by inputting an invalid integer:

String str = "25T"; try int number = Integer.parseInt(str); System.out.println(number); catch (NumberFormatException ex) ex.printStackTrace(); As you can see in the above code, we have tried khổng lồ convert 25T to lớn an integer. This is not a valid input. Therefore, it must throw a NumberFormatException.

Here"s the output of the above code:

java.lang.NumberFormatException: For đầu vào string: "25T"at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)at java.lang.Integer.parseInt(Integer.java:580)at java.lang.Integer.parseInt(Integer.java:615)at OOP.StringTest.main(StringTest.java:51)Next, we will consider how khổng lồ convert a string to lớn an integer using the Integer.valueOf() method.

2.


Bạn đang xem: Java string to int


Xem thêm: Chất Nào Sau Đây Còn Có Tên Gọi Là Đường Nho Là Loại Đường Nào Sau Đây


Xem thêm: Tải Bộ Đề Kiểm Tra Văn Bản Lớp 8 Học Kì 1 Lớp 8 Môn Ngữ Văn (Có Ma Trận, Đáp Án)


Use Integer.valueOf() to Convert a String to lớn an Integer

This method returns the string as an integer object. If you look at the Java documentation, Integer.valueOf() returns an integer object which is equivalent khổng lồ a new Integer(Integer.parseInt(s)).

We will place our code inside the try-catch block when using this method. Let us consider an example using the Integer.valueOf() method:

String str = "25"; try Integer number = Integer.valueOf(str); System.out.println(number); // đầu ra = 25 catch (NumberFormatException ex) ex.printStackTrace(); Now, let"s try khổng lồ break the above code by inputting an invalid integer number:

String str = "25TA"; try Integer number = Integer.valueOf(str); System.out.println(number); catch (NumberFormatException ex) ex.printStackTrace(); Similar khổng lồ the previous example, the above code will throw an exception.

Here"s the output of the above code:

java.lang.NumberFormatException: For input string: "25TA"at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)at java.lang.Integer.parseInt(Integer.java:580)at java.lang.Integer.valueOf(Integer.java:766)at OOP.StringTest.main(StringTest.java:42)We can also create a method to check if the passed-in string is numeric or not before using the above mentioned methods.

I have created a simple method for checking whether the passed-in string is numeric or not.

public class StringTest public static void main(String<> args) String str = "25"; String str1 = "25.06"; System.out.println(isNumeric(str)); System.out.println(isNumeric(str1)); private static boolean isNumeric(String str) return str != null && str.matches("<0-9.>+"); The output đầu ra is:

truetrueThe isNumeric() method takes a string as an argument. First it checks if it is null or not. After that we use the matches() method to kiểm tra if it contains digits 0 to lớn 9 và a period character.

This is a simple way to check numeric values. You can write or search Google for more advanced regular expressions to lớn capture numerics depending on your use case.

It is a best practice to check if the passed-in string is numeric or not before trying lớn convert it to integer.

Thank you for reading.

Post image by