1

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.
Osama Nisar
  • 13
  • 1
  • 7
  • What does `task ':javastart:practice.main()'.` mean? We need to understand how you are running your program. The problem is due to you program not getting the input it expects on its standard input. That is most likely because it is getting the wrong input, or no input at all. But we need to see how it is launched to figure out what that would be. – Stephen C Jul 06 '22 at 13:32
  • I am running this program in android studio using "java class". – Osama Nisar Jul 08 '22 at 08:34
  • My code is correct. I tried to find to find solution on many sites but I can't find solution. – Osama Nisar Jul 08 '22 at 08:36
  • The problem is not in the code, and this nothing to do with "importing". It is about the input you are reading with the scanner. The input *that it sees* is wrong. And it is *probably* because you are running the code the wrong way. Hence my previous comment. But we can't help you unless and until you **clearly** explain how you are running the code and if / how you are providing the input that it needs. – Stephen C Jul 09 '22 at 06:38

1 Answers1

0

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().

varun
  • 11
  • 2