Convert String to int in Java
π Mastering Java String to int Conversion (The Fun Way!)β
Ever found yourself staring at a String
and wishing it could magically transform into an int
? Well, Javaβs got your back! Let's dive into the world of number conversions and make it a fun ride. π’
1οΈβ£ Using Integer.parseInt()
(The Classic Way)β
π Syntaxβ
Integer.parseInt()
is like your reliable friend who always gets the job done. It takes a String
, chews it up (figuratively, of course), and spits out an int
.
It comes in three flavors:
public static int parseInt(String s) throws NumberFormatException { ... }
public static int parseInt(String s, int radix) throws NumberFormatException { ... }
public static int parseInt(String s, int beginIndex, int endIndex, int radix) throws NumberFormatException { ... }
β¨ Example Magicβ
Letβs turn some strings into integers like a Java wizard. π§ββοΈ
Assertions.assertEquals(1001, Integer.parseInt("1001"));
Assertions.assertEquals(513, Integer.parseInt("1001", 8));
Assertions.assertEquals(1001, Integer.parseInt("amount=1001", 7, 11, 10));
Cool, right? But waitβthereβs a catch! π¨
β Beware of NumberFormatException
β
Java is great, but it won't let you fool around with invalid numbers. If you try to pass nonsense, it'll throw a tantrum! π‘
Assertions.assertThrows(NumberFormatException.class, () -> {
Integer.parseInt(null);
});
Assertions.assertThrows(NumberFormatException.class, () -> {
Integer.parseInt("abc");
});
2οΈβ£ Using Integer.valueOf()
(The Cousin of parseInt()
)β
Integer.valueOf()
is basically parseInt()
, but it returns an Integer
object instead of a primitive int
. So, itβs fancier. π©
public static Integer valueOf(String s) throws NumberFormatException {...}
public static Integer valueOf(String s, int radix) throws NumberFormatException {...}
π₯ Example Fireworksβ
Assertions.assertEquals(1001, Integer.valueOf("1001"));
Assertions.assertEquals(513, Integer.valueOf("1001", 8));
Oops, don't mess up the input! π¨
Assertions.assertThrows(NumberFormatException.class, () -> {
Integer.valueOf("abc");
});
3οΈβ£ Using Integer.decode()
(For the Fancy Number Nerds π€)β
If you're dealing with different number formats like decimal, octal, or hexadecimal, Integer.decode()
is here to save the day! π¦ΈββοΈ
- Decimal: Just use numbers, optionally prefixed with
+
or-
(e.g.,+100
,-2022
) - Octal: Start with a
0
(e.g.,0404
,+0100
) - Hexadecimal: Start with
0x
or0X
(e.g.,0x334
,+0x100
)
public static Integer decode(String s) throws NumberFormatException
π― Example Conversionsβ
Assertions.assertEquals(100, Integer.decode("+100"));
Assertions.assertEquals(64, Integer.decode("+0100"));
Assertions.assertEquals(256, Integer.decode("+0x100"));
Boom! Now you can handle numbers like a pro. πͺ
π Conclusionβ
In this fun Java adventure, we:
β
Learned to convert a String
to int
using parseInt()
and valueOf()
.
β
Discovered decode()
for handling decimal, octal, and hexadecimal formats.
β
Realized Java doesnβt like nonsense inputs (hello, NumberFormatException
! π).
Now, go forth and convert your strings like a Java champion! π
Happy Coding! ππ