Java - Assigning User Input to Variable / Change Counter

48,062

Change this line :

Scanner input new Scanner(System.in);

To :

Scanner input = new Scanner(System.in);

And this should be after line below not before:

System.out.println("Enter the amount of money in cents.");

And as you did , the line below will read from input int value and assign it to your variable money :

int money = input.nextInt();
Share:
48,062
Andrii Abramov
Author by

Andrii Abramov

Engineers like to solve problems. If there are no problems handily available, they will create their own problems. (c) The Boy Scouts have a rule: "Always leave the campground cleaner than you found it." If you find a mess on the ground, you clean it up regardless of who might have made the mess. You intentionally improve the environment for the next group of campers. Actually the original form of that rule, written by Robert Stephenson Smyth Baden-Powell, the father of scouting, was "Try and leave this world a little better than you found it." My LinkedIn

Updated on November 24, 2022

Comments

  • Andrii Abramov
    Andrii Abramov over 1 year

    I'm quite new to java, although I have a fairly basic knowledge of C++. For my assignment I am counting change and sorting it into American currency (i.e., if you had 105 cents, it would divide it into one dollar and one dime).
    Logically I understand how to do this, but I'm having some serious trouble understanding the java syntax. I'm having serious trouble to find a way to assign a user-inputted value to a variable of my creation. In C++ you would simply use cin, but Java seems to be a lot more complicated in this regard.

    Here is my code so far:

    package coinCounter;
    import KeyboardPackage.Keyboard;
    import java.util.Scanner;
    
    
    public class  helloworld
    {
    
        public static void main(String[] args) 
        {   
            Scanner input new Scanner(System.in);
            //entire value of money, to be split into dollars, quarters, etc.
            int money = input.nextInt();
            int dollars = 0, quarters = 0, dimes = 0, nickels = 0;
    
            //asks for the amount of money
            System.out.println("Enter the amount of money in cents.");
    
    
            //checking for dollars, and leaving the change
            if(money >= 100)
            {
                dollars = money / 100;
                money = money % 100;
            }
    
            //taking the remainder, and sorting it into dimes, nickels, and pennies
            else if(money > 0)
            {
                quarters = money / 25;
                money = money % 25;
                dimes = money / 10;
                money = money % 10;
                nickels = money / 5;
                money = money % 5;
            }
    
            //result
            System.out.println("Dollars: " + dollars + ", Quarters: " + quarters + ", Dimes: " + dimes + ", Nickels: " + nickels + ", Pennies: " + money);
    
        }
    
    }
    

    I would really appreciate some help with how to assign a user-input to my variable, Money. However, if you see another error in the code, feel free to point it out.

    I know this is really basic stuff, so I appreciate all of your cooperation.