The Artima Developer Community
Sponsored Link

Legacy Java Answers Forum
April 2001

Advertisement

Advertisement

This page contains an archived post to the Java Answers Forum made prior to February 25, 2002. If you wish to participate in discussions, please visit the new Artima Forums.

Message:

Converting string to numbers

Posted by absm on April 10, 2001 at 11:30 AM

Could someone please test with the following command line
arguments 4.5 and 87.2
Please show how you did it if it worked ?
When these are used as command line arguments the output
is as follows:

a+b = 91.7
a-b = -82.7
a*b = 392.4
a/b = 0.0516055
a%b = 4.5

public class ValueOfDemo {
public static void main(String[] args) {

//this program requires two arguments on the command line
if (args.length == 2) {

//convert strings to numbers
float a = Float.valueOf(args[0]).floatValue();
float b = Float.valueOf(args[1]).floatValue();

//do some arithmetic
System.out.println("a + b = " + (a + b) );
System.out.println("a - b = " + (a - b) );
System.out.println("a * b = " + (a * b) );
System.out.println("a / b = " + (a / b) );
System.out.println("a % b = " + (a % b) );
} else {
System.out.println("This program requires two command-line arguments.");
}
}
}




Replies:

Sponsored Links



Google
  Web Artima.com   
Copyright © 1996-2009 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use - Advertise with Us