This week's book giveaway is in the Design forum.
We're giving away four copies of Design for the Mind and have Victor S. Yocco on-line!
See this thread for details.
Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

need help about command line arguments

 
elvin saflor
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Would you like to finish this code?

 
Wesleigh Pieters
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I thought my solution was fairly simple, curious to see your answer after we get some more from OP
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
please can you guys pm me your proposed solution I am very curious.
 
fred rosenberger
lowercase baba
Bartender
Pie
Posts: 12097
30
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Wesleigh Pieters
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ivan already pm'd me and I get it, should have thought of it, very simple but efficient
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
anybody solve my question ? just simple code plss

thank you )
 
Ivan Franko
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I can do it. but I am afraid, that after this Campbell Ritchie kill me..
 
Wesleigh Pieters
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
elvin saflor wrote:anybody solve my question ? just simple code plss


This site is NotACodeMill(←click).
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Witten wrote: . . . You guys are tough. . . .
You haven’t seen me in a bad mood!
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 972
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic