Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: Problem in Coin-counter with scanner class

  1. #1
    Junior Member
    Join Date
    Mar 2009
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Problem in Coin-counter with scanner class

    Hey all, am currently working on a small project, where the user enters an amount in cents and the program will input the least amount of coins needed (2 dollars, 1 dollars, 50 cents, 20 cents, 10 cents, 5 cents and 1 cents).

    Example Output...

    Please Enter amount in cents: 477

    Least number of coins needed is as follows

    2 coins 2
    1 coins 0
    50 cents coins 1
    20 cents coins 1
    10 cents coins 0
    5 cents coin 1
    1 cent coins 2

    Total coins needed 7.

    So far i have done:
    import java.util.*
    public class CoinCounter {
    	public static void main (String [] args){
    	Scanner sc = new Scanner(System.in);
     
    	int amount;
    	int oneDollar
    	int twoDollar;
    	int fiftyCents;
    	int twentyCents;
    	int tenCents;
    	int fiveCents;
    	int oneCents;
    	int totalCoins = 0;	
     
    	System.out.println("Enter amount in cents: ")
    	amount=sc.nextInt();
     
    			System.out.println((amount / 200) + "  $2 coins");
    			amount = amount % 200;
    			System.out.println((amount / 100) + "  $1 coins");
    			amount = amount % 100;
    			System.out.println((amount / 50) + "  $0.50 coins");
    			amount = amount % 50;
    			System.out.println((amount / 20) + "  $0.20 coins");
    			amount = amount % 20;
    			System.out.println((amount / 10) + "  $0.10 coins");
    			amount = amount % 10;
    			System.out.println((amount / 5) + "  $0.05 coins");
    			amount = amount % 5;  
    			System.out.println(amount + "  $0.01 coins");
    	}
     }

    Im not sure if that is correct, but im now stuck on what i should do next?
    Last edited by coccoster; March 24th, 2009 at 01:22 AM.


  2. #2
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 286 Times in 225 Posts
    Blog Entries
    4

    Default Re: CoinCounter with scanner class.

    Hello coccoster and welcome to the Java programming forums

    There are some missing semicolons ( ; ) in your code above.

    Have you been able to compile it?

    Your code seems to be working fine once the semicolons are in place.
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

    Looking for a Java job? Visit - Java Programming Careers

  3. #3
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 286 Times in 225 Posts
    Blog Entries
    4

    Default Re: CoinCounter with scanner class.

    Hey coccoster,

    I have just added the functionality to allow you to display the number of coins needed in your code.

    import java.util.*;
     
    public class CoinCounter {
     
        public static void main (String [] args){
     
            Scanner sc = new Scanner(System.in);
     
        int amount;
        int oneDollar;
        int twoDollar;
        int fiftyCents;
        int twentyCents;
        int tenCents;
        int fiveCents;
        int oneCents;
        int totalCoins = 0;    
        int myInt;
     
        System.out.println("Enter amount in cents: ");
        amount=sc.nextInt();
     
     
                System.out.println((amount / 200) + " – $2 coins");
                myInt = (amount / 200);        
                amount = amount % 200;            
     
                if(amount > 0){
                    for(int a = 0; a < myInt; a++ ){
                    totalCoins++;
                    }
                }
     
                System.out.println((amount / 100) + " – $1 coins");
                myInt = (amount / 100);
                amount = amount % 100;
     
                if(amount > 0){
                    for(int a = 0; a < myInt; a++ ){
                    totalCoins++;
                    }
                }
     
                System.out.println((amount / 50) + " – $0.50 coins");
                myInt = (amount / 50);
                amount = amount % 50;
     
                if(amount > 0){
                    for(int a = 0; a < myInt; a++ ){
                    totalCoins++;
                    }
                }
     
                System.out.println((amount / 20) + " – $0.20 coins");
                myInt = (amount / 20);
                amount = amount % 20;
     
                if(amount > 0){
                    for(int a = 0; a < myInt; a++ ){
                    totalCoins++;
                    }
                }
     
                System.out.println((amount / 10) + " – $0.10 coins");
                myInt = (amount / 10);
                amount = amount % 10;
     
                if(amount > 0){
                    for(int a = 0; a < myInt; a++ ){
                    totalCoins++;
                    }
                }
     
                System.out.println((amount / 5) + " – $0.05 coins");
                myInt = (amount / 5);
                amount = amount % 5;  
     
                if(amount > 0){
                    for(int a = 0; a < myInt; a++ ){
                    totalCoins++;
                    }
                }
     
                System.out.println(amount + " – $0.01 coins");
                myInt = (amount);
     
                if(amount > 0){
                    for(int a = 0; a < myInt; a++ ){
                    totalCoins++;
                    }
                }
     
                System.out.println("Total Coins: " + totalCoins);
     
        }
     }

    Enter amount in cents: 
    477
    2 – $2 coins
    0 – $1 coins
    1 – $0.50 coins
    1 – $0.20 coins
    0 – $0.10 coins
    1 – $0.05 coins
    2 – $0.01 coins
    Total Coins: 7

    Hope this all works as expected
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

    Looking for a Java job? Visit - Java Programming Careers

  4. #4
    Junior Member
    Join Date
    Mar 2009
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: CoinCounter with scanner class.

    Quote Originally Posted by JavaPF View Post
    Hey coccoster,

    I have just added the functionality to allow you to display the number of coins needed in your code.

    import java.util.*;
     
    public class CoinCounter {
     
        public static void main (String [] args){
     
            Scanner sc = new Scanner(System.in);
     
        int amount;
        int oneDollar;
        int twoDollar;
        int fiftyCents;
        int twentyCents;
        int tenCents;
        int fiveCents;
        int oneCents;
        int totalCoins = 0;    
        int myInt;
     
        System.out.println("Enter amount in cents: ");
        amount=sc.nextInt();
     
     
                System.out.println((amount / 200) + "  $2 coins");
                myInt = (amount / 200);        
                amount = amount % 200;            
     
                if(amount > 0){
                    for(int a = 0; a < myInt; a++ ){
                    totalCoins++;
                    }
                }
     
                System.out.println((amount / 100) + "  $1 coins");
                myInt = (amount / 100);
                amount = amount % 100;
     
                if(amount > 0){
                    for(int a = 0; a < myInt; a++ ){
                    totalCoins++;
                    }
                }
     
                System.out.println((amount / 50) + "  $0.50 coins");
                myInt = (amount / 50);
                amount = amount % 50;
     
                if(amount > 0){
                    for(int a = 0; a < myInt; a++ ){
                    totalCoins++;
                    }
                }
     
                System.out.println((amount / 20) + "  $0.20 coins");
                myInt = (amount / 20);
                amount = amount % 20;
     
                if(amount > 0){
                    for(int a = 0; a < myInt; a++ ){
                    totalCoins++;
                    }
                }
     
                System.out.println((amount / 10) + "  $0.10 coins");
                myInt = (amount / 10);
                amount = amount % 10;
     
                if(amount > 0){
                    for(int a = 0; a < myInt; a++ ){
                    totalCoins++;
                    }
                }
     
                System.out.println((amount / 5) + "  $0.05 coins");
                myInt = (amount / 5);
                amount = amount % 5;  
     
                if(amount > 0){
                    for(int a = 0; a < myInt; a++ ){
                    totalCoins++;
                    }
                }
     
                System.out.println(amount + "  $0.01 coins");
                myInt = (amount);
     
                if(amount > 0){
                    for(int a = 0; a < myInt; a++ ){
                    totalCoins++;
                    }
                }
     
                System.out.println("Total Coins: " + totalCoins);
     
        }
     }

    Enter amount in cents: 
    477
    2  $2 coins
    0  $1 coins
    1  $0.50 coins
    1  $0.20 coins
    0  $0.10 coins
    1  $0.05 coins
    2  $0.01 coins
    Total Coins: 7

    Hope this all works as expected
    Thanks mate your a champion.. Would you mind explaining to me why you chose myInt as the variable name?
    and could you also explain to me the for loops you have used?

  5. #5
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 286 Times in 225 Posts
    Blog Entries
    4

    Default Re: CoinCounter with scanner class.

    Quote Originally Posted by coccoster
    Thanks mate your a champion.. Would you mind explaining to me why you chose myInt as the variable name?
    and could you also explain to me the for loops you have used?
    Glad I could help!

    There was no real reason why I named that integer variable 'myInt'. You can call a variable pretty much what ever you want. I normally try to make the name descriptive so I can easily see what its used for but in this case I didn't put too much thought into it.

    This bit of code is used for counting each coin used:

                if(amount > 0){
                    for(int a = 0; a < myInt; a++ ){
                    totalCoins++;
                    }
                }
    It first checks to see if the 'amount' is greater than 0. If it is, that means a coin is being used so we need to count it. If not then it skips this part.

    myInt = (amount / 100);

    gives us the number of coins used. For example, 2.

    The for loop executes from 0 to the amount of coins used (myInt) so if there are 2 coins, then totalCoins++ is executed twice, adding 2 to the totalCoins value.
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

    Looking for a Java job? Visit - Java Programming Careers

  6. #6
    Junior Member
    Join Date
    Mar 2009
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: CoinCounter with scanner class.

    Quote Originally Posted by JavaPF View Post
    Glad I could help!

    There was no real reason why I named that integer variable 'myInt'. You can call a variable pretty much what ever you want. I normally try to make the name descriptive so I can easily see what its used for but in this case I didn't put too much thought into it.

    This bit of code is used for counting each coin used:

                if(amount > 0){
                    for(int a = 0; a < myInt; a++ ){
                    totalCoins++;
                    }
                }
    It first checks to see if the 'amount' is greater than 0. If it is, that means a coin is being used so we need to count it. If not then it skips this part.

    myInt = (amount / 100);

    gives us the number of coins used. For example, 2.

    The for loop executes from 0 to the amount of coins used (myInt) so if there are 2 coins, then totalCoins++ is executed twice, adding 2 to the totalCoins value.
    Thanks you helped alot mate.

  7. #7
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 286 Times in 225 Posts
    Blog Entries
    4

    Default Re: CoinCounter with scanner class.

    Always glad to help! Good luck with the rest of your project.
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

    Looking for a Java job? Visit - Java Programming Careers

Similar Threads

  1. Reading user input from the console with the Scanner class
    By JavaPF in forum Java SE API Tutorials
    Replies: 3
    Last Post: September 7th, 2011, 03:09 AM
  2. Use of scanner to return white space between the tokens
    By Alysosh in forum File I/O & Other I/O Streams
    Replies: 3
    Last Post: May 20th, 2009, 09:22 AM
  3. Reading a file line by line using the Scanner class
    By JavaPF in forum File Input/Output Tutorials
    Replies: 0
    Last Post: April 17th, 2009, 07:34 AM
  4. Replies: 1
    Last Post: May 13th, 2008, 08:08 AM

Tags for this Thread