Home > Blockchain >  Java Language user input error with importing file
Java Language user input error with importing file

Time:07-06

I am facing this problem, I have no errors in my code. Here is the code .Please remember I am writing java in android studio because I am working on app development.

practice.java

package com.example.javastart;
import java.util.Scanner;

public class practice {
public static void main(String[] args) {
    System.out.println("Taking Input");
    Scanner sc = new Scanner(System.in);
    System.out.println("Enter Number for Table ");
    int a = sc.nextInt();


    for (int i = 1; i <= 10; i  ) {
        int total = a * i;
        System.out.println(a   " * "  i   " = "  total);

    }
}

}

Here's some error

Task :javastart:practice.main() FAILED
Taking Input
Enter Number for Table 
Exception in thread "main" java.util.NoSuchElementException
at java.base/java.util.Scanner.throwFor(Scanner.java:937)
at java.base/java.util.Scanner.next(Scanner.java:1594)
at java.base/java.util.Scanner.nextInt(Scanner.java:2258)
at java.base/java.util.Scanner.nextInt(Scanner.java:2212)
at com.example.javastart.practice.main(practice.java:9)

 Execution failed for task ':javastart:practice.main()'.

* Try:
> Run with --stacktrace option to get the stack trace.
> Run with --info or --debug option to get more log output.
> Run with --scan to get full insights.

CodePudding user response:

NoSuchElementException will be thrown if no more tokens are available. This is caused by invoking nextInt() without checking if there's any integer available. Try using hasNextInt() to check if any tokens are available before calling nextInt().

CodePudding user response:

step1: check if imported the right package ; step2: refresh the project, and rerun again

  • Related