简体   繁体   中英

Trouble importing scanner class in Eclipse

import java.util.Scanner;

public class vendingMachine {

    public static void main(String[] args) {
        final int PENNIES_PER_DOLLAR = 100;
        final int PENNIES_PER_QUARTER = 25;
        Scanner in = new Scanner(System.in);
        System.out.print("Please enter the bill ($ 1 is 1 $5 is 5, etc. : ");
        int billValue = in.nextInt();
        System.out.print("Please enter the price of the desired item in pennies. : ");
        int itemPrice = in.nextInt();

        // computation of change due 
        int changeDue = (billValue * PENNIES_PER_DOLLAR) - itemPrice  ;
        int dollarsBack = changeDue / PENNIES_PER_DOLLAR ;
        changeDue = changeDue % PENNIES_PER_DOLLAR; 
        int quartersBack = changeDue / PENNIES_PER_DOLLAR;

        // print statements 

        System.out.printf("Dollars due back %6d" , dollarsBack);
        System.out.println();
        System.out.printf("Quarters due back %6d" , quartersBack);
        System.out.println(); 

    }

}

My error messages are:

Exception in thread "main" java.lang.Error: Unresolved compilation problems: 
The constructor Scanner(InputStream) is undefined
The method nextInt() is undefined for the type Scanner
The method nextInt() is undefined for the type Scanner

at vendingMachine.main(vendingMachine.java:8)

I'm a coding newbie and Eclipse is the first IDE I have worked with, so I would appreciate it if you guys could help walk me through, resolving these errors.

Definitely you would have java file name Scanner somewhere in your project. Try

java.util.Scanner myScanner = new java.util.Scanner(System.in);

instead. Else the compiler tries to instantiate your class which is also called Scanner.

Also, it's a good practice to start the name of class with Upper Case, it should be VendingMachine instead of vendingMachine .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM