Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

soccersquirt82

macrumors 6502
Original poster
Mar 11, 2008
296
0
This is the code I made for equating the change due. It's probably all wrong. When I go to run it, all three user inputs (Enter money given, enter amount, enter taxrate) are there. The one my teacher did shows the first one and he would enter an amount and once he pressed enter, showed the second one. How do I do this?

package change;
import java.util.*;
public class Change
{
public static void main (String [] args)
{
Scanner in = new Scanner(System.in);

//user input
System.out.print("Enter money given: ");
System.out.print("Enter amount: ");
System.out.print("Enter taxrate in percent: ");

//variables
double amtGiven = in.nextDouble();
double amt = in.nextDouble();
double taxrate = in.nextDouble();
double tax = in.nextDouble();
double totalamt = in.nextDouble();
double changedue = in.nextDouble();

//equation
tax = (taxrate/100) * amt;
totalamt = tax + amt;
changedue = amtGiven - totalamt;


}
}
 

lee1210

macrumors 68040
Jan 10, 2005
3,182
3
Dallas, TX
I don't think this is "VERY NEW" ... but... You should do your input processing between your prompts, not all after your prompts.

When you are declaring the three variables you are using for calculations only, you don't want to get input from stdin(System.in). They can just be declared double.

Then at the end you will want to display at least changedue, and maybe totalamt. You will want to use printf and %.2f as the specifier to print to 2 decimal points. As for input, you're not doing any validation, and if someone enters a bad value they will get and exception. Probably not an issue right now, just an FYI.

Also, put your code in [ CODE ] / [ /CODE ] tags (no spaces) so we can read it. I have a working version of this code, but it's your homework. Post again if you have more questions.

-Lee
 

soccersquirt82

macrumors 6502
Original poster
Mar 11, 2008
296
0
Thanks! When you say "they can just be declared doubled," what do you mean by that. (I don't understand any terms) "Then at the end you will want to display at least changedue, and maybe totalamt. You will want to use printf and %.2f as the specifier to print to 2 decimal points." I have no idea how to do this. Also thanks for walking me through this and not just telling me the answer.
 

soccersquirt82

macrumors 6502
Original poster
Mar 11, 2008
296
0
I got some help from my teacher, so this is the new code, but all these errors come up. (in cannot be resolved, sc cannot be resolved)

Code:
 / 
package change;
import java.util.*;
public class Change 
{
	public static void main (String [] args)
	{
		
		Scanner in;
		//variables
		double amtGiven = in.nextDouble();
		double amt = in.nextDouble();
		double taxrate = in.nextDouble();
		double tax = in.nextDouble();
		double totalamt = in.nextDouble();
		double changedue = in.nextDouble();
		
	Scanner in = new Scanner(System.in);
	
	//user input
	System.out.print("Enter money given: ");
	amtGiven = sc.nextDouble(); //reads the user's value and assigns to the variable
	System.out.print("Enter amount: ");
	amt = sc.nextDouble();
	System.out.print("Enter taxrate in percent: ");
	taxrate = sc.nextDouble();
	
	//equation
	tax = (taxrate/100) * amt;
	totalamt = tax + amt;
	changedue = amtGiven - totalamt;
	
	System.out.println(changedue);
	
	
	}
} / [ /CODE ]
 

lee1210

macrumors 68040
Jan 10, 2005
3,182
3
Dallas, TX
I got some help from my teacher, so this is the new code, but all these errors come up. (in cannot be resolved, sc cannot be resolved)

Code:
 / 
package change;
import java.util.*;
public class Change 
{
	public static void main (String [] args)
	{
		
		Scanner in;
		//variables
		double amtGiven = in.nextDouble();
		double amt = in.nextDouble();
		double taxrate = in.nextDouble();
		double tax = in.nextDouble();
		double totalamt = in.nextDouble();
		double changedue = in.nextDouble();
		
	Scanner in = new Scanner(System.in);
	
	//user input
	System.out.print("Enter money given: ");
	amtGiven = sc.nextDouble(); //reads the user's value and assigns to the variable
	System.out.print("Enter amount: ");
	amt = sc.nextDouble();
	System.out.print("Enter taxrate in percent: ");
	taxrate = sc.nextDouble();
	
	//equation
	tax = (taxrate/100) * amt;
	totalamt = tax + amt;
	changedue = amtGiven - totalamt;
	
	System.out.println(changedue);
	
	
	}
} / [ /CODE ][/QUOTE]

Alright... so the syntax to declare a variable without initialization is:
type varname;

You have a lot of:
double varname = in.nextDouble();

in.nextDouble() will read a double-precision floating point value from stream you initialized in with, System.in. In fact, you use in.nextDouble() 7 times before you even initialize it, so if this did run you'd get a NullPointerException. Just declare your doubles with no initialization to begin with. It looks like you can replace sc with in, as you have no variable named sc and it looks like you want to be using in where it appears.

In regard to using printf instead of println to print the proper precision, you can use System.out.printf with a format string like "%.2f". You don't have to do this, but the output won't resemble a monetary value otherwise.

Also, code tags look like:
[ CODE]System.out.println("Hello World!");[ /CODE]

Just take the spaces out before the C and the /, as I had to add those so it would display. You can also use the # button in the editor to add these for you.

-Lee
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.