How to Convert String to int in Java

 public class StringtoInt {
  
   public static void main(String[] args) {

       String str = "4"; 
       int in = Integer.parseInt(str);    
       System.out.println(in);

   }
}

Output:
4


 public class StringtoInt {
  
   public static void main(String[] args) {

       String str = "four"; 
       int in = Integer.parseInt(str);    
       System.out.println(in);

   }
}

Output:
Exception in thread “main” java.lang.NumberFormatException: