Results 1 to 5 of 5
  1. #1
    Mortus is offline Member
    Join Date
    Mar 2012
    Posts
    1
    Rep Power
    0

    Default Finding Max and Min values from string args

    Hey all,

    This is my first time posting here and my java knowledge is very limited, it doesn't go to far beyond hello world so apologies for any ignorance that ensues here. I have searched the forums here and google for my question but had little luck so I thought I would post.

    The title pretty much says it all I am writing a small code that will be finding the mean, length, max and min values from the arguments given and printing them.

    Thus far I have managed to code the length and mean but I am a little stuck on finding max and min values, any help would be much appreciated.

    Below is my code

    Java Code:
       public static void main(String[] args) {
    		int total = 0;
    		for (int i = 0; i < args.length; i++) {
    			total += Integer.parseInt(args[i])
    			{
    			}
    		}
    		System.out.println(args.length);
    		System.out.println(total/(double)args.length);
    	}
    Thanks in advance!

  2. #2
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    760
    Rep Power
    8

    Default Re: Finding Max and Min values from string args

    I have to following code to get the minimum and maximum value from an array of integer:

    Java Code:
    import java.util.Arrays;
    import java.util.Collections;
    
    public class ArrayMinMax {
        public static void main(String[] args) 
        {
            Integer[] numbers = { 8, 2, 6, 7, 0, 1, 4, 9, 5, 3 };
    
            int min = (int) Collections.min(Arrays.asList(numbers));
            int max = (int) Collections.max(Arrays.asList(numbers));
    
            System.out.println("Min number: " + min);
            System.out.println("Max number: " + max);
        }
    }
    Source: Learn Java by Examples - How do I know the minimum and maximum number in array?

  3. #3
    noobplus's Avatar
    noobplus is offline Senior Member
    Join Date
    Feb 2012
    Location
    chennai,south of India(south east of asia)
    Posts
    232
    Rep Power
    3

    Default Re: Finding Max and Min values from string args

    Quote Originally Posted by Mortus View Post
    Java Code:
    total += Integer.parseInt(args[i])
    			{
    			}
    'total' cannot have a body because it is a variable

    regards
    dhilip

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,236
    Rep Power
    19

    Default Re: Finding Max and Min values from string args

    Quote Originally Posted by noobplus View Post
    'total' cannot have a body because it is a variable
    Meaning what exactly?

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  5. #5
    noobplus's Avatar
    noobplus is offline Senior Member
    Join Date
    Feb 2012
    Location
    chennai,south of India(south east of asia)
    Posts
    232
    Rep Power
    3

    Default Re: Finding Max and Min values from string args

    Quote Originally Posted by noobplus View Post
    'total' cannot have a body because it is a variable

    regards
    dhilip
    Quote Originally Posted by DarrylBurke View Post
    Meaning what exactly?

    db
    I posted a coding following that post for the thread starter Mortus, but the codings were removed by some moderators,
    :Edited -- sorry, I just found it. It was moved to here by some moderators
    I had removed the body of 'total' in that codings
    I was not about to mention that error anyway, but I mentioned that error before posting my code

    regards
    dhilip
    Last edited by noobplus; 03-22-2012 at 03:10 PM.

Similar Threads

  1. Why can't we write main without String args[]
    By shailender in forum New To Java
    Replies: 6
    Last Post: 06-17-2013, 09:28 AM
  2. use of String[] args
    By mallikanala in forum New To Java
    Replies: 1
    Last Post: 06-21-2011, 08:09 AM
  3. Passing values to main(String args[]) ?
    By al_Marshy_1981 in forum New To Java
    Replies: 6
    Last Post: 02-21-2010, 10:16 PM
  4. String, quotes and args
    By mac in forum New To Java
    Replies: 8
    Last Post: 02-04-2010, 04:24 PM
  5. What does String args[] stand for?
    By Addez in forum New To Java
    Replies: 7
    Last Post: 08-19-2009, 10:24 AM

Posting Permissions

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