Results 1 to 10 of 10
  1. #1
    May.ver.rick is offline Member
    Join Date
    Apr 2010
    Posts
    5
    Rep Power
    0

    Default Help with my code to find MinOfAll numbers

    I am new to Programming world and so to Java,

    I tried my hands on writing the following code to find minimum of all the numbers entered via command prompt,
    but I am getting the error

    Exception in thread "main" java.lang.ArrayIndexOutOfBound Exception :5 at MinOfAll.main

    Kindly tell me where am I going wrong?

    Java Code:
    public class MinOfAll{  
      
    public static void main(String[] args){  
      
    int result = Integer.parseInt(args[0]);  
    for (int i =0 ; i< args.length; i++)  
    {  
        int current=Integer.parseInt(args[i+1]);  
     if (current < result){  
         result = current;  
     }  
             
    }  
    System.out.println("Min of all is : " + result);  
      
      
    }  
    }  
      
    [color=red]I/P at console : java MinOfAll 2 3 4 5 6[/color]

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    Why did you do that?

    Java Code:
    int current=Integer.parseInt(args[i+1]);
    should be,

    Java Code:
    int current=Integer.parseInt(args[i]);
    Because arrays in Java are zero based indexing.

  3. #3
    May.ver.rick is offline Member
    Join Date
    Apr 2010
    Posts
    5
    Rep Power
    0

    Default

    I did that to start the comparison with next element in the array.

    Even tough I edit it to,

    Java Code:
    int current=Integer.parseInt(args[i]);
    the output error still persists.

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

  5. #5
    May.ver.rick is offline Member
    Join Date
    Apr 2010
    Posts
    5
    Rep Power
    0

    Default

    This is what I do on console

    java MinOfAll 2 3 4 5 6

  6. #6
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    So that's the error. Can you show your correct code. I don't think that you fix the correct place.

  7. #7
    May.ver.rick is offline Member
    Join Date
    Apr 2010
    Posts
    5
    Rep Power
    0

    Default

    Java Code:
    public class MinOfAll{
    
    public static void main(String[] args){
    
    int result = Integer.parseInt(args[0]);
    for (int i =0 ; i< args.length; i++)
    {
    	int current=Integer.parseInt(args[i]);
     if (current <= result){
    	 result = current;
     }
    	   
    }
    System.out.println("Min of all is : " + result);
    
    
    }
    }

  8. #8
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    Right, so it' working. There cannot be any error at all. You say that you get the same error? It cannot happen.

  9. #9
    May.ver.rick is offline Member
    Join Date
    Apr 2010
    Posts
    5
    Rep Power
    0

    Default Thanks Erangs, its working

    My bad I had kept the same code at 2 places ad was running the unpdated one.

    Thanks again!!

  10. #10
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

Similar Threads

  1. Replies: 1
    Last Post: 03-04-2010, 12:19 PM
  2. Replies: 7
    Last Post: 08-28-2009, 09:23 AM
  3. printing two smallest numbers from a series of numbers
    By trofyscarz in forum New To Java
    Replies: 2
    Last Post: 10-15-2008, 12:46 AM
  4. how to find if the code contains deprecated API?
    By Pooja Deshpande in forum Advanced Java
    Replies: 3
    Last Post: 05-29-2008, 10:12 AM
  5. Replies: 1
    Last Post: 04-26-2007, 04:52 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
  •