简体   繁体   中英

Simple java program with scanner error

Ok - so this is a very simple java program. I keep getting the following error. After reading the docs, I have made sure all my scanner statements are coded correctly. What am I doing wrong?

Enter working hours: Exception in thread "main" java.util.NoSuchElementException
    at java.util.Scanner.throwFor(Unknown Source)
    at java.util.Scanner.next(Unknown Source)
    at java.util.Scanner.nextDouble(Unknown Source)
    at WeeklyWages.main(WeeklyWages.java:12)

Here is the code:

import java.util.*;

public class WeeklyWages {

    static Scanner console = new Scanner(System.in);

    public static void main(String[] args) {

        double hrsworked, payrate, wages;

        System.out.print("Enter working hours: ");
        hrsworked = console.nextDouble();

        System.out.println();

        System.out.print("Enter the payrate: ");
        payrate = console.nextDouble();

        System.out.println();

        if (hrsworked > 40)
            wages = 40.0 * payrate + 1.5 * payrate * (hrsworked - 40.0);
        else
            wages = hrsworked * payrate;

        System.out.printf("Hours worked = .2%f %n", wages);
        System.out.println();

    }
}

Ok looks like something wrong with Sublimetext. I just compiled it via javac in dos and it worked. weird.

I think you got your Syntax wrong and does not imported library "java.util.scanner"
Here is the proper code for you

import java.util.Scanner;

public class MainPrintThread {

public static void main(String args[])

{
    Scanner console = new Scanner(System.in);



        double hrsworked, payrate, wages;

        System.out.print("Enter working hours: ");
        hrsworked = console.nextDouble();

        System.out.println();

        System.out.print("Enter the payrate: ");
        payrate = console.nextDouble();

        System.out.println();

        if (hrsworked > 40)
            wages = 40.0 * payrate + 1.5 * payrate * (hrsworked - 40.0);
        else
            wages = hrsworked * payrate;

        System.out.printf("Hours worked = .2%f %n", wages);
        System.out.println();

    }



}

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