Win a copy of Kotlin in Action this week in the Kotlin forum!
programming forums Java Java JSRs Mobile Certification Databases Caching Books Engineering Languages Frameworks Products This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Sheriffs:
Saloon Keepers:
Bartenders:

Ken Anderson
Greenhorn
Posts: 6
Subject:
Date:
Sat, 17 Feb 2001 00:09:37 -0500
From:
Ken Anderson <kand@clover.net>
To:
beginning@javaranch.com
I am writing some java for practice and have run into a problem. I am entering several prices of merchandise as if it were checkout register. I was then going to total the merchandise and then figure out the sales tax and finally add the two for the total sale. However I can't get past the first section. No matter if I use int or doubles or floats, I get totals that do not make sense. I know I am missing something easy but I have looked now for several days and can't figure it out, so I think it is now time to look for help!! I have tried different combinations of numbers and they are all goofy.
for example:
I entered 11.00 14.00 22.50 13.00 and got
The purchase price is 11.00
The purchase price is 14.00
The purchase price is 22.50
The purchase price is 13.00
The total price before tax is 44.00
class Hi
{
public static void main( String[] args )
{
double totPrice = 0.0 ;
double price = Double.parseDouble( args[0] ) ;
for ( int i = 0; i < args.length; i++ )
{
totPrice = totPrice + price ;
System.out.println( "The purchase price is " + args[i] ) ;
}
System.out.println( " __________" ) ;
System.out.println( "The total before tax is "
+ totPrice );
}
}

Cindy Glass
"The Hood"
Sheriff
Posts: 8521
Your variable price is always pointing ar args[0], so that got added 4 times.
You need to move the setting of the variable inside the for loop and make it args[i] like you did for the println().

Anonymous
Ranch Hand
Posts: 18944
Here is a code,which might help u fix the prob.....
import java.io.*;
class Args{
public static void main(String args[]){
double total_price=0;
double element;
int i;
for(i=0;i<args.length;i++){>
element=Double.parseDouble(args[i]);
total_price=total_price+element;
}
System.out.println("total Price with out tax="+total_price);
}
}
regards,
Sujay

Anonymous
Ranch Hand
Posts: 18944
import java.io.*;
class Args{
public static void main(String args[]){
double total_price=0;
double element;
int i;
for(i=0;i<args.length;i++){>
element=Double.parseDouble(args[i]);
total_price=total_price+element;
}
System.out.println("total Price with out tax="+total_price);
}
}
regards,
Jay

huiying li
Ranch Hand
Posts: 68
You are very close. Just move the variable assignment price inside the for loop for each price argument passed from the command line.
Hope this helps.
<br /> C:\JavaRanch>java Hi 11.0 14.0 22.5 13.0
The purchase price is 11.0
The purchase price is 14.0
The purchase price is 22.5
The purchase price is 13.0
__________
The total before tax is 60.5

Ken Anderson
Greenhorn
Posts: 6
Thank you Cindy for the reasonning behide my mistake and thank you Huiying for showing it to me. It works so well when it is done right!!!

 Consider Paul's rocket mass heater.