Java: String to Int - how it works
In this practical tip we explain to you whether and how you can convert a string into an integer in Java.
Java: String to Int - how it works
In Java, you can easily convert a string to an integer.
- With an integer, the computer stores an integer value. With a string, in turn, a character string is saved, i.e. only the individual digits.
- With the command »String string_1 =" 1234 ";« we have defined a string that only contains numbers. To convert this into an integer, the command »int value_1 = Integer.parseInt (string_1);« is used - so far no problem.
- However, it becomes difficult if the string contains spaces. The string that we use with the command »String string_2 =" 087 105 114 032 098 101 105 032 067 072 073 080 032 098 101 104 101 114 114 115 099 104 101 110 032 100 101 110 032 071 114 097 109 109 097 116 105 107 046 ";« we cannot convert it to an integer. This would require a little more effort.
- You can also find more practical tips in our Java programming guide.