aspose file tools*
The moose likes Beginning Java and the fly likes addition of numbers Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "addition of numbers" Watch "addition of numbers" New topic
Author

addition of numbers

Ken Anderson
Greenhorn

Joined: Nov 24, 2000
Posts: 6
Subject:
addition
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

Joined: Sep 29, 2000
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().


"JavaRanch, where the deer and the Certified play" - David O'Meara
Anonymous
Ranch Hand

Joined: Nov 22, 2008
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

Joined: Nov 22, 2008
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

Joined: Feb 12, 2001
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


SCJP
Ken Anderson
Greenhorn

Joined: Nov 24, 2000
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!!!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: addition of numbers