Results 1 to 2 of 2
  1. #1
    McChill is offline Member
    Join Date
    Feb 2009
    Posts
    47
    Rep Power
    0

    Default ParseInt question

    static int parseInt(String s)
    Parses the string argument as a signed decimal integer.

    Java Code:
    
    public class Divider {
    	
    	public static void main(String args[])
    	{
    		try{
    			System.out.println("Before divison");
    			int i = Integer.parseInt(args[0]);
    			int j = Integer.parseInt(args[1]);
    			System.out.println(i/j);
    			System.out.println("After divison");			
    		}
    		catch(ArithmeticException e) {
    			System.out.println("Arithmetic Exception");
    		}
    		catch(ArrayIndexOutOfBoundsException e) {
    			System.out.println("Array Index Out Of Bounds Exception.");
    		}
    		catch(NumberFormatException e) {
    			System.out.println("Number Format Exception");
    		}
    		finally {
    			System.out.println("Finally block");
    		}
    	}
    
    }
    If I put in, for example, "1" and "hey" the program gives me a number format exception. I thought parseint returned the string AS an int? If so, why does it give me an exception?

  2. #2
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    10

    Default

    The "1" is okay, the "hey" isn't. It will parse the String as an int if the String is a number. And what a number is depends partially on the radix, but, when you don't designate one, the radix is 10 (i.e. normal decimal numbers).

Similar Threads

  1. [SOLVED] Integer.parseInt() problem
    By eggmanpete in forum New To Java
    Replies: 18
    Last Post: 02-15-2009, 05:25 PM
  2. parseInt() vs. intValue()
    By JavaPilot in forum New To Java
    Replies: 5
    Last Post: 02-04-2009, 09:39 AM
  3. Integer.parseInt?
    By Exhonour in forum New To Java
    Replies: 4
    Last Post: 01-20-2009, 03:31 AM
  4. Problem with Integer.parseInt()
    By Hevonen in forum New To Java
    Replies: 2
    Last Post: 12-14-2008, 04:41 AM
  5. [SOLVED] Command Line Arguments and ParseInt
    By Sophiie in forum New To Java
    Replies: 4
    Last Post: 11-16-2008, 10:45 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •