Results 1 to 2 of 2
  1. #1
    blsimpson is offline Member
    Join Date
    Jun 2011
    Posts
    1
    Rep Power
    0

    Default New to Java, need some help.

    Hello all,

    As stated I am new to Java, and currently taking an online course, and struggling to get through it..

    We are learning about how to process command line arguments,and how to write a method and call it by sending an argument. The assignment is asking me to write two methods outside of main() that will be called later..

    I was able to write the first part of the first method, and display it as it should look, but I cant seem to get it to find the max number entered and display it. The code that I have so far far is below.

    Since I am in a class, I am not looking for just the answer, but guidence on how to get there.

    Here is my code:

    Java Code:
    class Max
    {
        public static void main(String[] args)
        {
            if (args.length == 0)
    		{
    			System.out.println("You must enter at least 1 integer. Please try again.");
    			System.exit(0);
    		}
    		else findMax(args);
    	}
    
    	static int findMax(String[] values)
    	{
    		int maxVal = Integer.parseInt(values[0]);
    		
    		// Write a 'for' loop for remaining elements of 'values' array.
    		int value = 0;
    		int max = 0;
    		System.out.print("The input values are: ");
    		for (int i = 0; i < values.length; i++)
    		{
    			int temp = Integer.parseInt(values[i]);
    			System.out.print(temp + ", ");
    			value = temp;
    		if (max > value)
    		{
    			maxVal = value;
    			System.out.println("Max = " + maxVal);
    		}
    		else
    			{
    			value = max;
    			System.out.println("Max = " + maxVal);
    			}
    		}
    		// If an element is more than 'maxVal', set maxVal = element.
    	return maxVal;
    	}
    	
    	static void displayResult(String[] values, int max)
    	{
    		String tmp = values[0];
    		// Write a 'for' loop for remaining elements of 'values' array.
    		// concatenate a comma and the element's value each time
     
    		// Display 'tmp'
    	}
    }
    Currently, when I run the code, this is the output, which I know is wrong..

    Java Code:
    c:\Java\Lesson5\assign51>java Max
    You must enter at least 1 integer. Please try again.
    
    c:\Java\Lesson5\assign51>java Max 5 4 6 8 5
    The input values are: 5, Max = 5
    4, Max = 5
    6, Max = 5
    8, Max = 5
    5, Max = 5
    
    c:\Java\Lesson5\assign51>

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    You have 2 variables: max and maxVal which may be leading to confusion.
    You are using value to store the current value so why near the bottom do you assign max to value?
    One of your if statements you have max > value as the condition. Shouldn't it be if value is greater than the maximum value....?

    You need to step away from the computer and think about how to solve this on paper first. That is get the algorithm clear in your head before typing the code.

Posting Permissions

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