Thursday 8 April 2010

How to Convert "String" to "Int."

import java.util.Scanner;
public class stringToNum{
public static void main(String[] args) throws Exception{
String number;
int num = 0;
Scanner input = new Scanner(System.in);
System.out.println("Enter any Number");
try{
number = input.next();
num = Integer.parseInt(number);
System.out.println(num);
}catch(Exception e){
System.out.println("You have to enter a number");
}
}
}

No comments:

Post a Comment