• 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

args.length problem

 
Ranch Hand
Posts: 178
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I have the following class.
public class Hypotenuse
{
public double calcHypotenuse(double sideA, double sideB)
{
return (Math.sqrt(Math.pow (sideA, 2) + Math.pow (sideB, 2)));
}
public static void main (String args[])
{
Hypotenuse hypot;
double sideA, sideB, sideC;
if (args.length < 2) { //Don't understand why it will print out the error message as I pass args[0] and args[1] only.
System.err.println(
"Usage: java Hypotenuse <side a> <side b>");
System.exit(1);
}
sideA = Double.valueOf(args[0]).doubleValue(); //Will this assignment create a double value for sideA?
sideB = Double.valueOf(args[1]).doubleValue();
hypot = new Hypotenuse();
sideC = hypot.calcHypotenuse(sideA, sideB);
System.out.println(
"Given a triangle with two sides: " + sideA +
" and " + sideB + ", the longest side is: " + sideC);
//Why don't this result print out?
}
}

Thanks for help.
Andrew
 
Chicken Farmer ()
Posts: 1932
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Andrew Parker:

if (args.length < 2) { //Don't understand why it will print out the error message as I pass args[0] and args[1] only.
System.err.println(
"Usage: java Hypotenuse <side a> <side b>");
System.exit(1);
}


Not sure what you are asking here. What you have is that if someone runs java Hypotenuse, and either puts no arguments or 1 argument, that error prints out (this seems to be the desired effect). 2 or more arguments causes the program to continue as normal.


sideA = Double.valueOf(args[0]).doubleValue(); //Will this assignment create a double value for sideA?
sideB = Double.valueOf(args[1]).doubleValue();



Better way to do this would be to call Double.parseDouble() on the strings instead.
Jason
 
Sheriff
Posts: 9109
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

If I type
java Hypotenuse 3 4
It prints
Given a triangle with two sides: 3.0 and 4.0, the longest side is: 5.0

I guess I don't understand your question.
 
Andrew Parker
Ranch Hand
Posts: 178
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am sorry. I forgot to pass the numeric parameters.
Thanks for help.
Andrew
 
The only cure for that is hours of television radiation. And this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic