| Author |
Finding maximum value from command line arguments.
|
Tom Zanoli
Greenhorn
Joined: Aug 09, 2011
Posts: 5
|
|
Hi,
I have been struggling with this for several days and would appreciate any insight. Trying to call a method from main that reads string of arguments and determines maximum value. I cannot figure out why the below doesnt work. It appears that when I do the numbers conversion, it doesnt convert it to an array. Thanks,
Tom
|
 |
Raymond Tong
Ranch Hand
Joined: Aug 15, 2010
Posts: 156
|
|
Your code is not compilable.
numbers seems to be int[] but not defined anywhere.
And if you just want to find the max value, why would you need to store all the converted integers?
|
 |
Mohamed Sanaulla
Bartender
Joined: Sep 08, 2007
Posts: 2926
|
|
And ItDoesntWorkIsUseless
You are using number as an array at few places and as an int at some place. Moreover you haven't declared the int[] number.
|
Mohamed Sanaulla | My Blog
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32654
|
|
|
And you can't re-declare numbers like that.
|
 |
Greg Brannon
Bartender
Joined: Oct 24, 2010
Posts: 530
|
|
Here's what you could do:
Use the String array args as the argument to your findMax() method - Done
Then in the findMax() method:
1. Assign the first integer value of the array to a new int, maxValue
2. Compare the other integer values of the array to maxValue, and
a. if a greater value is found, the greater value becomes the new maxValue
3. Repeat step 2 until all array members have been checked.
4. Report maxValue as the largest integer.
|
Learning Java using Eclipse on OpenSUSE 11.2
Linux user#: 501795
|
 |
Tom Zanoli
Greenhorn
Joined: Aug 09, 2011
Posts: 5
|
|
|
Thanks everyone. The main issue I cant get my head around is how do you convert the string array to integer array. I cannot assign a string array value to a new int. Thanks.
|
 |
fred rosenberger
lowercase baba
Bartender
Joined: Oct 02, 2003
Posts: 9946
|
|
i don't think you can convert a String array to an Integer (or int) array. You can, however, convert a String to an Int. you can iterate through a String array, converting them one at a time to Integers, then insert those into your Integer array.
|
Never ascribe to malice that which can be adequately explained by stupidity.
|
 |
Tom Zanoli
Greenhorn
Joined: Aug 09, 2011
Posts: 5
|
|
|
OK that makes sense. Thanks a lot. I still don't quite get how to iterate through since it is not an array.
|
 |
Greg Brannon
Bartender
Joined: Oct 24, 2010
Posts: 530
|
|
|
I'm don't know that you need an int[] array. If I suggested you do, I didn't mean to. You only need to consider the integer values of your String[] array one at a time to find the largest one.
|
 |
Raymond Tong
Ranch Hand
Joined: Aug 15, 2010
Posts: 156
|
|
Tom Zanoli wrote:OK that makes sense. Thanks a lot. I still don't quite get how to iterate through since it is not an array.
What is the it you are referring given you have String array and int array?
|
 |
 |
|
|
subject: Finding maximum value from command line arguments.
|
|
|