Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

addition of numbers

 
Ken Anderson
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8521
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!!!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic