• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

Finding maximum value from command line arguments.

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 258
2
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Bartender
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 74031
332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And you can't re-declare numbers like that.
 
Bartender
Posts: 563
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
lowercase baba
Posts: 13003
66
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 258
2
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic