• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

addition of numbers

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 );
}
}

 
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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().
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!!!
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic