VERY NEW: What is wrong?

Discussion in 'Mac Programming' started by soccersquirt82, Aug 18, 2008.

  1. soccersquirt82 macrumors 6502

    Joined:
    Mar 11, 2008
    #1
    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;


    }
    }
     
  2. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #2
    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
     
  3. soccersquirt82 thread starter macrumors 6502

    Joined:
    Mar 11, 2008
    #3
    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.
     
  4. soccersquirt82 thread starter macrumors 6502

    Joined:
    Mar 11, 2008
    #4
    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 ]
     
  5. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #5
     

Share This Page