| Author |
need help about command line arguments
|
elvin saflor
Greenhorn
Joined: Nov 14, 2012
Posts: 4
|
|
anybody help me make program plss
when i input in command line 11 5 10 15 23
the output is
the highest number is: 23
the lowest number is: 5
use conditional operators or relational operators
thank you in advance ..
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32712
|
|
Welcome to the Ranch
We expect you to have something you can show us, so we can help you improve it. I presume you already know about command line arguments.
|
 |
Ivan Franko
Ranch Hand
Joined: May 30, 2011
Posts: 44
|
|
Would you like to finish this code?
|
 |
Wesleigh Pieters
Ranch Hand
Joined: Sep 04, 2012
Posts: 81
|
|
I will help put you onto the right track then come back with some code if you need help and we take it further ok deal?
you need to create an array of ints populated by parsing the main methods arguments into it.
then as a handy trick go look at the java.util.Arrays class and you should find a handy static methods in there that will do something to your array. from there it will be easy enough to call the relevant position of where the highest and lowest int should now be.
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32712
|
|
I would use a different technique, which I think is simpler, which neither of you has mentioned. I would not use a method of the Arrays class, nor of the Collections class. I am not convinced you even need an int[].
But please don’t post any more code. That post is a little too complete for my likings.
|
 |
Wesleigh Pieters
Ranch Hand
Joined: Sep 04, 2012
Posts: 81
|
|
|
I thought my solution was fairly simple, curious to see your answer after we get some more from OP
|
 |
Jeff Verdegan
Bartender
Joined: Jan 03, 2004
Posts: 5895
|
|
If all we want is lowest and highest, there's no need for an array or list (other than the String[] args for the command line args where he's getting the numbers from, but it could just as easily be done by reading from System.in with no array or list at all).
And even if we are using a List, we should never need to catch NoSuchElementException. That's an unchecked exception, and indicates a bug in our code.
|
 |
Wesleigh Pieters
Ranch Hand
Joined: Sep 04, 2012
Posts: 81
|
|
|
please can you guys pm me your proposed solution I am very curious.
|
 |
fred rosenberger
lowercase baba
Bartender
Joined: Oct 02, 2003
Posts: 9955
|
|
Wesleigh Pieters wrote:please can you guys pm me your proposed solution I am very curious.
Forget about java...I am going to read to you a list of numbers over the phone. When I am done, you need to tell me the largest value I read and the smallest.
You have two small pieces of paper - each is only big enough for you to write down one number on it. You have a pencil, a pencil sharpener, an eraser, you can tell me to pause when I read them, and you do not have any idea how many numbers I am going to read.
How would you do it?
|
Never ascribe to malice that which can be adequately explained by stupidity.
|
 |
Wesleigh Pieters
Ranch Hand
Joined: Sep 04, 2012
Posts: 81
|
|
|
Ivan already pm'd me and I get it, should have thought of it, very simple but efficient
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32712
|
|
|
I wrote out a solution yesterday which does not use the int[], and takes nine lines inside a method. Two of those lines consist entirely of { or }.
|
 |
elvin saflor
Greenhorn
Joined: Nov 14, 2012
Posts: 4
|
|
anybody solve my question ? just simple code plss
thank you )
|
 |
Ivan Franko
Ranch Hand
Joined: May 30, 2011
Posts: 44
|
|
I can do it. but I am afraid, that after this Campbell Ritchie kill me..
|
 |
Wesleigh Pieters
Ranch Hand
Joined: Sep 04, 2012
Posts: 81
|
|
|
nope, I gave you a verbal solution, partial code was also give by another and Campbell and Ivan said there was an even simpler solution, you haven't even tried.
|
 |
Jeff Verdegan
Bartender
Joined: Jan 03, 2004
Posts: 5895
|
|
elvin saflor wrote:anybody solve my question ? just simple code plss
This site is NotACodeMill(←click).
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32712
|
|
You show us yours and we might show you ours.
I probably shall post my version of the solution, once we know you have handed in your version.
|
 |
Paul Witten
Ranch Hand
Joined: Oct 10, 2012
Posts: 86
|
|
Campbell Ritchie wrote:.... once we know you have handed in your version.
You guys are tough. A nice little student can't even steal intellectual labor from you.
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32712
|
|
Paul Witten wrote: . . . You guys are tough. . . .
You haven’t seen me in a bad mood!
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32712
|
|
|
This version has no input validation, and does everything in the main method, which is usually not a good idea. It might be better to pass args to the constructor of a class which extracts the values from the array.
|
 |
 |
|
|
subject: need help about command line arguments
|
|
|