0
 import java.util.Scanner;

public class MyClass {
    public static void main(String args[]) {
      {  try (Scanner sc = new Scanner(System.in)) { 

  while (true) {
    System.out.println("");
    System.out.println("Enter the numbers to peform operations");
    System.out.println("Enter the first number");
    int x = sc.nextInt();

/This is where my code stops^. I just need to see the error thats here because I can't see it.

Federico klez Culloca
  • 26,308
  • 17
  • 56
  • 95
  • What do you mean it stops? What's after that? Could it just be that it's looping and waiting for input again? – Federico klez Culloca Jul 26 '22 at 10:51
  • Now this isn't the end of the code but I just had to cut most of it off to be able to post. – Mark Ashibuogwu Jul 26 '22 at 10:52
  • Did you enter a number into your console and confirm it by pressing enter? – maloomeister Jul 26 '22 at 10:53
  • 1
    Do not try with resources with a scanner on System.in it closes System.in – Jens Jul 26 '22 at 10:53
  • Please add a [mcve] – Jens Jul 26 '22 at 10:54
  • @FedericoklezCulloca Yeah I get an error after it prints "Enter the first number". It says "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 MyClass.main(MyClass.java:10)" – Mark Ashibuogwu Jul 26 '22 at 10:54
  • I didn't get to do that because it gave me an error @maloomeister – Mark Ashibuogwu Jul 26 '22 at 10:55
  • @MarkAshibuogwu Like Jens says, we need to see a minimal reproducible example, because that code **by itself** (and assuming you just close the loop and the `try` after reading) shouldn't throw that exception) – Federico klez Culloca Jul 26 '22 at 10:56
  • Duplicate of https://stackoverflow.com/questions/13042008/java-util-nosuchelementexception-scanner-reading-user-input – Jens Jul 26 '22 at 10:56
  • What comes after it is this: System.out.println("Enter the first number"); int x = sc.nextInt(); System.out.print("Enter the second number"); int y = sc.nextInt(); System.out.println("Choose the operation you want to peform"); System.out.println("Choose 1 for ADDITION"); System.out.println("Choose 2 for SUBTRACTION"); System.out.println("Choose 3 for MULTIPLICATION"); System.out.println("Choose 4 for DIVISION"); System.out.println("Choose 5 for MODULUS"); System.out.println("Choose 6 for EXIT"); int n = sc.nextInt (); switch (n) { After that I just have the switch cases. – Mark Ashibuogwu Jul 26 '22 at 11:02
  • Really sorry if I'm a bit messy right now. I'm new to Stack Overflow – Mark Ashibuogwu Jul 26 '22 at 11:02
  • Oh you mean the "try" loop I put in. Ok I think I know what to do now thanks. @Jens – Mark Ashibuogwu Jul 26 '22 at 11:06
  • @MarkAshibuogwu for future reference, that code should have gone in the question (using the [edit] link) and not in the comments. – Federico klez Culloca Jul 26 '22 at 11:11
  • there is no `try`-loop – Jens Jul 26 '22 at 11:12
  • @Jens there's only `do`-loop or `do not`-loop? – Federico klez Culloca Jul 26 '22 at 11:18
  • @FedericoklezCulloca sorry can not get you – Jens Jul 26 '22 at 11:24
  • @Jens it was a Star Wars joke (see https://www.youtube.com/watch?v=BQ4yd2W50No) – Federico klez Culloca Jul 26 '22 at 11:43

0 Answers0