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 3 of 3

Thread: Help with finding a mistake/issue in this code

  1. #1
    Junior Member
    Join Date
    May 2021
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Help with finding a mistake/issue in this code

    Hello guys! Need your help with something. I'm tasked to find an issue/mistake in this program that is supposed to take any amount entered in the console and exchange that amount of money for coins of different values. The program seems to work fine and accomplishes the task. I have been staring at it for 2 days now and have come up with nothing. Any help would be much appreciated! Best regards and thank you!

    public static void main(String[] args) {
     
        Scanner keyboardInput = new Scanner(System.in);
     
        System.out.println("Please enter the amount of coins you want to exchange: ");
     
        int coinsLeft = keyboardInput.nextInt();
        System.out.println("Your coin amount is: " + coinsLeft);
     
        System.out.println("\n\nYou'll get: ");
     
        int twoEuros = coinsLeft / 200;
        coinsLeft = coinsLeft % 200;
        if (twoEuros > 0){
            System.out.println(twoEuros + " <=== 2 euro coin(s)");}
        int oneEuro = coinsLeft / 100;
        coinsLeft = coinsLeft % 100;
        if (oneEuro > 0){
            System.out.println(oneEuro + " <=== 1 euro coin(s)");}
        int fiftyCents = coinsLeft / 50;
        coinsLeft = coinsLeft % 50;
        if (fiftyCents > 0){
            System.out.println(fiftyCents + " <=== 50 cent coin(s)");}
        int twentyCents = coinsLeft / 20;
        coinsLeft = coinsLeft % 20;
        if (twentyCents > 0){
            System.out.println(twentyCents + " <=== 200 cent coin(s)");}
        int tenCents = coinsLeft / 10;
        coinsLeft = coinsLeft % 10;
        if (tenCents > 0){
            System.out.println(tenCents + " <=== 100 cent coin(s)");}
        int fiveCents = coinsLeft / 5;
        coinsLeft = coinsLeft % 5;
        if (fiveCents > 0){
            System.out.println(fiveCents + " <=== 5 cent coin(s)");}
        int twoCents = coinsLeft / 2;
        coinsLeft = coinsLeft % 2;
        if (twoCents > 0){
            System.out.println(twoCents + " <=== 2 cent coin(s)");}
        int oneCent = coinsLeft;
        if (coinsLeft > 0){
            System.out.println(oneCent + " <=== 1 cent coin(s)");}
    }

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Help with finding a mistake/issue in this code

    Why do you think there is any problem with the code?
    What happens when you run the code with different input values?

    For easier testing, add a loop around the code that reads the number and displays the results.
    Then change the Scanner class's constructor(outside of the loop) to preload a number of input values:
    Scanner keyboardInput = new Scanner("1 234 678 543 333 23 -99 0 100000");
    If you don't understand my answer, don't ignore it, ask a question.

  3. The Following User Says Thank You to Norm For This Useful Post:

    SisyphusTheCoder (May 15th, 2021)

  4. #3
    Junior Member
    Join Date
    May 2021
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Thumbs up Re: Help with finding a mistake/issue in this code

    Quote Originally Posted by SisyphusTheCoder View Post
    Hello guys! Need your help with something. I'm tasked to find an issue/mistake in this program that is supposed to take any amount entered in the console and exchange that amount of money for coins of different values. The program seems to work fine and accomplishes the task. I have been staring at it for 2 days now and have come up with nothing. Any help would be much appreciated! Best regards and thank you!

    public static void main(String[] args) {
     
        Scanner keyboardInput = new Scanner(System.in);
     
        System.out.println("Please enter the amount of coins you want to exchange: ");
     
        int coinsLeft = keyboardInput.nextInt();
        System.out.println("Your coin amount is: " + coinsLeft);
     
        System.out.println("\n\nYou'll get: ");
     
        int twoEuros = coinsLeft / 200;
        coinsLeft = coinsLeft % 200;
        if (twoEuros > 0){
            System.out.println(twoEuros + " <=== 2 euro coin(s)");}
        int oneEuro = coinsLeft / 100;
        coinsLeft = coinsLeft % 100;
        if (oneEuro > 0){
            System.out.println(oneEuro + " <=== 1 euro coin(s)");}
        int fiftyCents = coinsLeft / 50;
        coinsLeft = coinsLeft % 50;
        if (fiftyCents > 0){
            System.out.println(fiftyCents + " <=== 50 cent coin(s)");}
        int twentyCents = coinsLeft / 20;
        coinsLeft = coinsLeft % 20;
        if (twentyCents > 0){
            System.out.println(twentyCents + " <=== 200 cent coin(s)");}
        int tenCents = coinsLeft / 10;
        coinsLeft = coinsLeft % 10;
        if (tenCents > 0){
            System.out.println(tenCents + " <=== 100 cent coin(s)");}
        int fiveCents = coinsLeft / 5;
        coinsLeft = coinsLeft % 5;
        if (fiveCents > 0){
            System.out.println(fiveCents + " <=== 5 cent coin(s)");}
        int twoCents = coinsLeft / 2;
        coinsLeft = coinsLeft % 2;
        if (twoCents > 0){
            System.out.println(twoCents + " <=== 2 cent coin(s)");}
        int oneCent = coinsLeft;
        if (coinsLeft > 0){
            System.out.println(oneCent + " <=== 1 cent coin(s)");}
    }
    System.out.println(twentyCents + " <=== 200 cent coin(s)");}
    int tenCents = coinsLeft / 10;
    coinsLeft = coinsLeft % 10;
    if (tenCents > 0){
    System.out.println(tenCents + " <=== 100 cent coin(s)");}
    int fiveCents = coinsLeft / 5;
    coinsLeft = coinsLeft % 5;
    if (fiveCents > 0){

    Seems like I overthought this one. Turns out that there were 2 simple typos in the code which didn't affect the function of the code itself and just the print outs. I guess a lesson learned - keep it simple and don't overthink it.

    Quote Originally Posted by Norm View Post
    Why do you think there is any problem with the code?
    What happens when you run the code with different input values?

    For easier testing, add a loop around the code that reads the number and displays the results.
    Then change the Scanner class's constructor(outside of the loop) to preload a number of input values:
    Scanner keyboardInput = new Scanner("1 234 678 543 333 23 -99 0 100000");
    I tried out what you said and learned quite a bit in the process about loops. Thanks for putting in the effort and time to reply to post. Have a good one.

Similar Threads

  1. [SOLVED] Need help finding why this code doesn't work!
    By MattsMan999 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 19th, 2021, 09:26 AM
  2. Replies: 0
    Last Post: October 25th, 2013, 03:28 PM
  3. [SOLVED] Can't find mistake in my code
    By Daler in forum What's Wrong With My Code?
    Replies: 4
    Last Post: November 19th, 2012, 12:19 AM
  4. need help finding code in javadoc
    By lf2killer in forum Java Theory & Questions
    Replies: 3
    Last Post: October 13th, 2012, 08:52 AM
  5. Need Help - Factoring & Prime Finding Code
    By prodigytoast in forum Algorithms & Recursion
    Replies: 5
    Last Post: November 5th, 2009, 07:38 AM

Tags for this Thread