0

I'm java beginner, and I wanted to make To-Do list. The problem is I made scanner function with a lot of useless code and the main problem is that, when I click '1' to finish the To-Do list everything works fine, but when I click '0' to continue doing list i get 3 errors in eclipse:

I really want to get rid of that and still practice.

import java.util.Scanner;

public class ToDo {

  public static void main(String[] args) {

      Scanner input1 = new Scanner(System.in);
      System.out.println("Put in task: ");
      String task1 = input1.nextLine();
      System.out.println("Is that all?  ");
      double answer1 = input1.nextDouble();
      input1.close();
      if(answer1 == 1) {
          System.out.println("Your tasks are:\n" + task1);
          System.exit(0);
      } else if(answer1 == 0) {
          System.out.println("\n");
      }
      Scanner input2 = new Scanner(System.in);
      System.out.println("Put in task: ");
      String task2 = input2.nextLine();
      System.out.println("Is that all?  ");
      double answer2 = input2.nextDouble();
      input2.close();
      if(answer2 != 0) {
          System.out.println("Your tasks are:\n" + task1 + task2);
          System.exit(0);
      } else if(answer2 != 1) {
          System.out.println("\n");
      }
  }
}
Arun Sudhakaran
  • 2,167
  • 4
  • 27
  • 52
mp5-a4_k
  • 11
  • 1
  • 1
    Your `No line found` error occurs because you're constructing an additional scanner. Rather than create an `input1` and `input2` scanner you can reuse the first scanner –  Oct 10 '19 at 23:38

1 Answers1

2

This is because when you do input1.close();, it is also closing the System.in input stream.

Ideally you should use the same scanner object, but you can also make your code work by removing input1.close();.

More info can be found here and here.

Kartik
  • 7,677
  • 4
  • 28
  • 50