• Post Reply Bookmark Topic Watch Topic
  • New Topic

Finding maximum value from command line arguments.  RSS feed

 
Tom Zanoli
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 255
2
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3174
34
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Campbell Ritchie
Sheriff
Posts: 55351
157
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And you can't re-declare numbers like that.
 
Greg Brannon
Bartender
Posts: 563
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Tom Zanoli
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12529
48
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

 
Tom Zanoli
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 563
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 255
2
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!