This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes need help about command line arguments Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "need help about command line arguments" Watch "need help about command line arguments" New topic
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: 37939
    
  22
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: 37939
    
  22
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: 6109
    
    6

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: 11153
    
  16

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?


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
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: 37939
    
  22
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: 6109
    
    6

elvin saflor wrote:anybody solve my question ? just simple code plss


This site is NotACodeMill(←click).
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37939
    
  22
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: 37939
    
  22
Paul Witten wrote: . . . You guys are tough. . . .
You haven’t seen me in a bad mood!
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37939
    
  22
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.
Omar Jouda
Greenhorn

Joined: Jul 11, 2012
Posts: 19
Campbell Ritchie wrote: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.


This is a very smart way to approach it indeed. It gets a bit confusing for with loops because I kind of have to visualize it clearly in my head to understand.
You mentioned that you can pass the args to a constructor of a class. Methods can't call constructors the way they call other methods right? So, do you mean invoke a constructor of a class you create using new Classname(args); ?
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Omar Jouda wrote:You mentioned that you can pass the args to a constructor of a class. Methods can't call constructors the way they call other methods right? So, do you mean invoke a constructor of a class you create using new Classname(args); ?


Yes, assuming Classname has such a constructor.
Ivan Jozsef Balazs
Rancher

Joined: May 22, 2012
Posts: 867
    
    5

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.


It is better to break up a program into parts of appropriate size and hence it is usually not good to do everything in the main method (excepted maybe for small, possibly contrived examples and "├ętudes", maybe including the one we are coping with here.)

In a real-world program you would for example write a class that would take in the constructor a String array and would provide a method to return the converted values as a List<Integer> or ant int[]. Thus this conversion step would be factored out of the main method.

 
 
subject: need help about command line arguments
 
Similar Threads
Listening for connections
+ overloded operator
Assignment Java-2 (EvenOrOdd)
how to put numbers beside a textpane?
convert perl to java