• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

need help about command line arguments

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ..
 
Marshal
Posts: 79036
375
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Would you like to finish this code?

 
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 79036
375
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I thought my solution was fairly simple, curious to see your answer after we get some more from OP
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
please can you guys pm me your proposed solution I am very curious.
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 79036
375
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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



This site is NotACodeMill(←click).
 
Campbell Ritchie
Marshal
Posts: 79036
375
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 79036
375
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paul Witten wrote: . . . You guys are tough. . . .

You haven’t seen me in a bad mood!
 
Campbell Ritchie
Marshal
Posts: 79036
375
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Rancher
Posts: 1043
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

 
"Don't believe every tiny ad you see on the internet. But this one is rock solid." - George Washington
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic