The Artima Developer Community
Sponsored Link

Java Answers Forum
command-line argument

4 replies on 1 page. Most recent reply: Jul 14, 2013 10:42 AM by Hema Kalsi

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 4 replies on 1 page
Denise B

Posts: 5
Nickname: elusive57
Registered: Feb, 2008

command-line argument Posted: Feb 20, 2008 6:47 AM
Reply to this message Reply
Advertisement
New to Java and trying to understand what's happening. Task is to write an application that uses an enhanced for statement to sum the double values passed by the command-line arguments. I know the parseDouble converts the string to a double. Can't seem to make the program sum up the arguments. I'm sure my coding is flawed, but don't know where. Would appreciate any help. Thanks!

import java.util.Scanner;
 
public class Doubletest
{
    public static void main(String args[])
    {
       String s1;
	    String s2;
	  	 double total = 0.0;
			
	 Scanner input = new Scanner(System.in);
	
	 System.out.println("Enter first number");
	 s1 = input.next();//i'm thinking this will go in index 0
	
	 System.out.println("Enter second number");
	 s2 = input.next();//and this goes in index 1
		 	 	 	  	
	 if (args.length != 3)
	     System.out.println("Clueless");
	 else {
		 for (String s : args){//not sure where string s comes from
		 double d = Double.parseDouble(args[0]);//convert string to double
		 total += d;}
	  	 }
        System.out.printf("The sum should appear here: %.2f", total);//total shows as 0.00 no matter the input
	}//end main
}//end class DoubleTest


George B

Posts: 22
Nickname: hmgeorge
Registered: Feb, 2008

Re: command-line argument Posted: Feb 20, 2008 1:02 PM
Reply to this message Reply
The args you seek should be entered on the command line, so you don't need to prompt the user for them. The user should just put whatever numbers they want to sum on the command line, e.g.:

java Doubletest 1.1 2.22 3

Then they'll get passed into your program in the args array. Here's a trimmed-down version of your program that works:
public class Doubletest
{
    public static void main(String args[])
    {
        double total = 0.0;
            
        for (String s : args)
        {
            double d = Double.parseDouble(s);
            total += d;
        }
        System.out.printf("The sum should appear here: %.2f\n", total);
    }
}

When I run it as above I get the following:

> java Doubletest 1.1 2.22 3
The sum should appear here: 6.32

There were a few other mistakes in the original:
- do the parseDouble on 's', not args[0] (the for loop causes s to get the value of each of the args in turn)
- don't bother with the 'clueless' check -- you'll just sum up all the args regardless how many there are
- you were missing a newline ("\n") in your printf statement
- your indentation needs work :)

Denise B

Posts: 5
Nickname: elusive57
Registered: Feb, 2008

Re: command-line argument Posted: Feb 20, 2008 2:14 PM
Reply to this message Reply
Thank you so much for offering an explanation...i couldn't picture the user entering data without me prompting, but now i see this is geared toward users smart enough to use the command line...i appreciate all of your other useful suggestions and tips as well.

Thanks!

Michelle Theodora

Posts: 1
Nickname: michtheo
Registered: Mar, 2013

Re: command-line argument Posted: Mar 25, 2013 12:50 PM
Reply to this message Reply
i used your code.. but it didn't work or maybe i miss something @.@

public static void main(String args[])
{
String s1;
String s2;
Scanner input = new Scanner(System.in);

System.out.println("Enter first number");
s1 = input.next();//i'm thinking this will go in index 0

System.out.println("Enter second number");
s2 = input.next();//and this goes in index 1
double total = 0.0;

for (String s: args)
{
double d= Double.parseDouble(s);
total += d;
}
System.out.printf("The sum should appear here: %.2f\n", total);
}

at the end total is still 0.00. how i change this??

Hema Kalsi

Posts: 1
Nickname: hkalsi
Registered: Jul, 2013

Re: command-line argument Posted: Jul 14, 2013 10:42 AM
Reply to this message Reply
The code using command line is already posted by George B.

Here is the corrected code if you would like to use java.util.Scanner:

public static void main(String args[])
{
String s1;
String s2;
Scanner input = new Scanner(System.in);

System.out.println("Enter first number");
s1 = input.next();//i'm thinking this will go in index 0

System.out.println("Enter second number");
s2 = input.next();//and this goes in index 1
double total = 0.0;

total += Double.parseDouble(s1) + Double.parseDouble(s2);
System.out.printf("The sum should appear here: %.2f\n", total);
}

Flat View: This topic has 4 replies on 1 page
Topic: promotion forum question Previous Topic   Next Topic Topic: Graal VM and its setup


Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2014 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use - Advertise with Us