Page 1 of 2 12 LastLast
Results 1 to 20 of 31
Like Tree4Likes

Thread: is this the right way?

  1. #1
    lanmonster is offline Member
    Join Date
    Dec 2012
    Posts
    22
    Rep Power
    0

    Default Simple math program

    This is a program I wrote to aide in doing a cash drop at my work. I need it to ask me for a variable and then manipulate it according to the monetary value. I am fairly certain I did it right. At the end I want it to:
    • add all of the variables together \,
    • display how much of each I have \,
    • Display the total \,
    • subtract 200 from the total and give me that value \,
    • tell me how many of each bill to take out

    This is the code I have thus far:
    Java Code:
    public class Subway{
        public static void main(String[]  args) {
    import javax.swing.JOptionPane;
         //Gather Variables
            int hundred = 0;
            String userInput = JOption.showInputDialog("How many 100's are there?");
            hundred = hundred * 100;
             
            int fifty = 0;
            String userInput = JOption.showInputDialog("How many 50's are there?");
            fifty = fifty * 50;
             
            int twenty = 0;
            String userInput = JOption.showInputDialog("How many 20's are there?");
            twenty = twenty * 20;
         
            int ten = 0;
            String userInput = JOption.showInputDialog("How many 10's are there?");
            ten = ten * 10;
         
            int five = 0;
            String userInput = JOption.showInputDialog("How many 5's are there?");
            five = five * 5;
         
            int one = 0;
            String userInput = JOption.showInputDialog("How many 1's are there?");
             
            int quarter = 0;
            String userInput = JOption.showInputDialog("How many quarters are there?");
            quarter = quarter / 4;
         
            int dime = 0;
            String userInput = JOption.showInputDialog("How many dimes are there?");
            dime = dime / 10;
         
            int nickel = 0;
            String userInput = JOption.showInputDialog("How many nickels are there?");
            nickel = nickel / 20;
         
            int penny = 0;
            String userInput = JOption.showInputDialog("How many pennies are there?");
            penny = penny / 100;
       
        //Display each amount
        System.out.println("$ in hundreds: " + hundred);
        System.out.println("$ in fifties: " + fifty);
        System.out.println("$ in twenties: " + twenty);
        System.out.println("$ in tens: " + ten);
        System.out.println("$ in fives: " + five);
        System.out.println("$ in ones: " + one);
        System.out.println("$ in quarters: " + quarter);
        System.out.println("$ in dimes: " + dime);
        System.out.println("$ in nickels: " + nickel);
        System.out.println("$ in pennies: " + penny);
    
        //Add Variables
        System.out.println("Total is " + (hundred + fifty + twenty + ten + five + one + quarter + dime + nickel + penny));
    
        //Subtract 200
        System.out.println("Take out " + (hundred + fifty + twenty + ten + five + one + quarter + dime + nickel + penny - 200));
        }
    }
    Any help is much appreciated!
    P.S. this is my first big java project!
    *EDIT*
    I decided to go with my gut and this is what I came up with. It gives me an error on compile though. the error is
    Java Code:
    C:\Java>javac Subway.java
    Subway.java:2: error: illegal start of type
    import javax.swing.JOptionPane;
    ^
    Subway.java:2: error: ';' expected
    import javax.swing.JOptionPane;
          ^
    Subway.java:2: error: illegal start of type
    import javax.swing.JOptionPane;
                ^
    Subway.java:2: error: ';' expected
    import javax.swing.JOptionPane;
                      ^
    Subway.java:2: error: <identifier> expected
    import javax.swing.JOptionPane;
                                  ^
    Subway.java:4: error: illegal start of expression
    import javax.swing.JOptionPane;
    ^
    Subway.java:4: error: not a statement
    import javax.swing.JOptionPane;
                      ^
    7 errors
    Questions:
    1. How do I get user input and store it as a variable?
    2. How do I fix the errors I have on compiling?
    3. Can anyone simplify what Darkzombies suggested? I'm a n00b and I don't understand what he said.
    Last edited by lanmonster; 12-03-2012 at 06:40 AM.

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,458
    Rep Power
    20

    Default Re: is this the right way?

    Please go through the Forum Rules -- particularly the third paragraph.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

  4. #4
    lanmonster is offline Member
    Join Date
    Dec 2012
    Posts
    22
    Rep Power
    0

    Default Re: is this the right way?

    Sorry, I'm a n00b. Does the code I ask for variables and manipulate them how I want?

  5. #5
    Darkzombies is offline Senior Member
    Join Date
    Nov 2012
    Posts
    105
    Rep Power
    0

    Default Re: is this the right way?

    You don't necessarily have to create variables for each calculation, you can simply create an array, and use each instance of the array in your calculations. I.E. Numbers[0] = Numbers[0] * 100 instead of hundred = hundred * 100 and so forth, it will still be understandable because of the prints. Then at the end, you can use a for loop and add all the instances of the array together.

  6. #6
    lanmonster is offline Member
    Join Date
    Dec 2012
    Posts
    22
    Rep Power
    0

    Default Re: is this the right way?

    OP updated. more code and questions at the bottom. Thanks for your responses!

  7. #7
    Darkzombies is offline Senior Member
    Join Date
    Nov 2012
    Posts
    105
    Rep Power
    0

    Default Re: is this the right way?

    You have to import before the class, also, I'll get you an example of what I meant.

    Java Code:
    import java.util.*;
    import javax.*;
    import java.awt.*;
    
    public class arrayTest{
    	public static void main(String[] args){
    		int[] numbers;
    		
            String userInput = JOption.showInputDialog("How many 50's are there?");
            numbers[0] *= 50;
              
            String userInput = JOption.showInputDialog("How many 20's are there?");
            numbers[1] *= 20;
          
            String userInput = JOption.showInputDialog("How many 10's are there?");
            numbers[2] *= 10;
          
            String userInput = JOption.showInputDialog("How many 5's are there?");
            numbers[3] *= 5;
          
            String userInput = JOption.showInputDialog("How many 1's are there?");
            numbers[4] *= 1;
            
            String userInput = JOption.showInputDialog("How many quarters are there?");
            numbers[5] /= 4;
          
            String userInput = JOption.showInputDialog("How many dimes are there?");
            numbers[6] /= 10;
          
            String userInput = JOption.showInputDialog("How many nickels are there?");
            numbers[7] /= 20;
          
            String userInput = JOption.showInputDialog("How many pennies are there?");
            numbers[8] /= 100;
    	}
    }
    Sort of like that, also, I believe it should be JOptionPane, not JOption. Finally, you are declaring userInput every time you ask a question, do not declare it more than once. Don't use "String" in front, just do userInput = blahblahblah
    Last edited by Darkzombies; 12-03-2012 at 05:07 AM.

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: is this the right way?

    Quote Originally Posted by Darkzombies View Post
    You have to import before the class, also, I'll get you an example of what I meant.

    Java Code:
    import java.util.*;
    import javax.*;
    import java.awt.*;
    
    public class arrayTest{
    	public static void main(String[] args){
    		int[] numbers;
    		
            String userInput = JOption.showInputDialog("How many 50's are there?");
            numbers[0] *= 50;
              
            String userInput = JOption.showInputDialog("How many 20's are there?");
            numbers[1] *= 20;
          
            String userInput = JOption.showInputDialog("How many 10's are there?");
            numbers[2] *= 10;
          
            String userInput = JOption.showInputDialog("How many 5's are there?");
            numbers[3] *= 5;
          
            String userInput = JOption.showInputDialog("How many 1's are there?");
            numbers[4] *= 1;
            
            String userInput = JOption.showInputDialog("How many quarters are there?");
            numbers[5] /= 4;
          
            String userInput = JOption.showInputDialog("How many dimes are there?");
            numbers[6] /= 10;
          
            String userInput = JOption.showInputDialog("How many nickels are there?");
            numbers[7] /= 20;
          
            String userInput = JOption.showInputDialog("How many pennies are there?");
            numbers[8] /= 100;
    	}
    }
    Sort of like that, also, I believe it should be JOptionPane, not JOption. Finally, you are declaring userInput every time you ask a question, do not declare it more than once. Don't use "String" in front, just do userInput = blahblahblah
    Does this example compile?? You appear to be using a non-existing class, JOption, and appear to be re-declaring the same variable multiple times, both of which should cause this code to not make it past compilation.

  9. #9
    Darkzombies is offline Senior Member
    Join Date
    Nov 2012
    Posts
    105
    Rep Power
    0

    Default Re: is this the right way?

    Well, I just used her code as an example, and explained both those things to him... Either way it wouldn't compile anyways because I have to initialize the Array, which I forgot to do. >.<

  10. #10
    lanmonster is offline Member
    Join Date
    Dec 2012
    Posts
    22
    Rep Power
    0

    Default Re: is this the right way?

    Java Code:
    import javax.swing.JOptionPane;
    public class Subway{
        public static void main(String[]  args) {
         //Gather Variables
    	int hundred = 0;
            String userInput = JOptionPane.showInputDialog("How many 100's are there?");
            hundred = hundred * 100;
            
    	int fifty = 0;
            userInput = JOptionPane.showInputDialog("How many 50's are there?");
            fifty = fifty * 50;
              
    	int twenty = 0; 
            userInput = JOptionPane.showInputDialog("How many 20's are there?");
            twenty = twenty * 20;
          
    	int ten = 0;
            userInput = JOptionPane.showInputDialog("How many 10's are there?");
            ten = ten * 10;
          
    	int five = 0;
            userInput = JOptionPane.showInputDialog("How many 5's are there?");
            five = five * 5;
          
    	int one = 0;
            userInput = JOptionPane.showInputDialog("How many 1's are there?");        
    
    	int quarter = 0;  
            userInput = JOptionPane.showInputDialog("How many quarters are there?");
            quarter = quarter / 4;
        
    	int dime = 0;
            userInput = JOptionPane.showInputDialog("How many dimes are there?");
            dime = dime / 10;
          
    	int nickel = 0;
            userInput = JOptionPane.showInputDialog("How many nickels are there?");
            nickel = nickel / 20;
          
    	int penny = 0;
            userInput = JOptionPane.showInputDialog("How many pennies are there?");
            penny = penny / 100;
        
        //Display each amount
        System.out.println("$ in hundreds: " + hundred);
        System.out.println("$ in fifties: " + fifty);
        System.out.println("$ in twenties: " + twenty);
        System.out.println("$ in tens: " + ten);
        System.out.println("$ in fives: " + five);
        System.out.println("$ in ones: " + one);
        System.out.println("$ in quarters: " + quarter);
        System.out.println("$ in dimes: " + dime);
        System.out.println("$ in nickels: " + nickel);
        System.out.println("$ in pennies: " + penny);
     
        //Add Variables
        System.out.println("Total is " + (hundred + fifty + twenty + ten + five + one + quarter + dime + nickel + penny));
     
        //Subtract 200
        System.out.println("Take out " + (hundred + fifty + twenty + ten + five + one + quarter + dime + nickel + penny - 200));
        }
    }
    compiles but when I ran it, no matter what I input, it says the value is zero. How do I take the userInput and manipulate it? I tried replacing hundred = hundred * 100 with hundred = userInput * 100 but it wont compile...

  11. #11
    Darkzombies is offline Senior Member
    Join Date
    Nov 2012
    Posts
    105
    Rep Power
    0

    Default Re: is this the right way?

    I know you're probably going to facepalm, but if hundred = 0, and you times 0 by 100, you still get 0...

  12. #12
    lanmonster is offline Member
    Join Date
    Dec 2012
    Posts
    22
    Rep Power
    0

    Default Re: is this the right way?

    I know, but I thought the String userInput = JOptionPane.showInputDialog("How many 100's are there?"); junk changed hundred to whatever I input. How do I make it change?

  13. #13
    lanmonster is offline Member
    Join Date
    Dec 2012
    Posts
    22
    Rep Power
    0

    Default Re: is this the right way?

    How do I change String userInput = JOptionPane.showInputDialog("How many 100's are there?"); to an int variable?

  14. #14
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,225
    Rep Power
    20

    Default Re: is this the right way?

    Please do not ask for code as refusal often offends.

    ** This space for rent **

  15. #15
    lanmonster is offline Member
    Join Date
    Dec 2012
    Posts
    22
    Rep Power
    0

    Default Re: is this the right way?

    Java Code:
            
            int hundred = 0;
            String userInput = JOptionPane.showInputDialog("How many 100's are there?");
            Integer.parseInt(String userInput);
            hundred = userInput * 100;
    When I compile, it says:
    Java Code:
    Subway.java:7: ')' expected
            Integer.parseInt(String userInput);
                                   ^
    Subway.java:7: illegal start of expression
            Integer.parseInt(String userInput);
                                             ^
    What is wrong and how do I fix it?

  16. #16
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: is this the right way?

    Java Code:
    Integer.parseInt(String userInput);
    That's not how you call a method. When you define a method, you have the parameter type, here String, in the parameter, but when you actually call the method, the type is not passed in. It's just called with Integer.parseInt(userInput). This is no different from the JOptionPane call above this line. You don't do it like:

    Java Code:
    String userInputWrong = JOptionPane.showInputDialog(String "How many 100's are there?"); // *** incorrect ***
    String userInputRight   = JOptionPane.showInputDialog("How many 100's are there?"); // *** correct ***
    Right?

    Next you will have to do something with the int returned by this method such as put it into a variable. Currently you're just discarding it, ignoring it. So put it on the right side of an assignment statement and have an int variable on the left side ready to receive the int returned, just like the String variable in the JOptionPane line above it.
    Last edited by Fubarable; 12-05-2012 at 07:44 AM.
    lanmonster likes this.

  17. #17
    lanmonster is offline Member
    Join Date
    Dec 2012
    Posts
    22
    Rep Power
    0

    Default Re: is this the right way?

    Thanks! You guys are great! I did some googling and found that BigDecimal is the best to use for currency but how do I use it and how do I operate mathematically on it?

  18. #18
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,225
    Rep Power
    20

    Default Re: is this the right way?

    Quote Originally Posted by lanmonster View Post
    Thanks! You guys are great! I did some googling and found that BigDecimal is the best to use for currency but how do I use it and how do I operate mathematically on it?
    The key thing to remember is that a BigDecimal object is immutable.
    That is it cannot change.
    So all the methods on it return a new BigDecimal that is the result of the method.
    Java Code:
          BigDecimal bd1 = new BigDecimal(123);
          BigDecimal bd2 = new BigDecimal(456);
          BigDecimal bd3 = bd1.add(bd2);
    The call to add() on bd1 does not add the value of bd2 to bd1.
    'bd1' is still 123.
    But it returns a new BigDecimal that is the result, 579...which I assign to bd3.
    I could have assigned it back to bd1 instead, if I didn't actually need to keep a copy of bd1 around.
    Fubarable likes this.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  19. #19
    lanmonster is offline Member
    Join Date
    Dec 2012
    Posts
    22
    Rep Power
    0

    Default Re: is this the right way?

    Java Code:
    userInput = JOptionPane.showInputDialog("How many quarters are there?");
            int quarter = Integer.parseInt(userInput);
            int f = 4;
            BigDecimal bd = new BigDecimal(quarter);
            BigDecimal bd25 = bd.divide(f, 2, BigDecimal.ROUND_HALF_UP);
    is what I have. I get this error
    Java Code:
    Subway.java:33: cannot find symbol
    symbol  : method divide(int,int,int)
    location: class java.math.BigDecimal
            BigDecimal bd25 = bd.divide(f, 2, BigDecimal.ROUND_HALF_UP);
                                         ^
    1 error
    I really hope I am not bugging you guys...

  20. #20
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,458
    Rep Power
    20

    Default Re: is this the right way?

    You need to look at the API for BigDecimal and see what parameters the divide(...) method accepts.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •