0
import java.io.*;
import java.util.*;

public class Main{
    public static void main(String [] args) throws InputMismatchException{
    double width;
    int period;
    double Ppp;
    Scanner in0  = new Scanner(System.in);
    Scanner in1  = new Scanner(System.in);
    Scanner in2  = new Scanner(System.in);
    System.out.println("Give width\n");
    while(in0.hasNextDouble()){
        width = in0.nextDouble();
    }
    in0.close();
    System.out.println("\n");
    System.out.println("Give period");
    while(in1.hasNextInt()){
        period = in1.nextInt();
    }
    in1.close();
    System.out.println("\n");
    System.out.println("Insert width peak to peak");
    while(in2.hasNextDouble()){
        Ppp = in2.nextDouble();
    }
    in2.close();
}

I run this code block I insert the first input but it displays null for each input and then it crash May someone run it and tell if he has the same problem I use BlueJ compiler

4
  • Why do you have multiple scanner instances ? Commented Sep 22, 2015 at 12:32
  • How do you know it displays "null"? Do you enter your doubles using a comma or a point? Commented Sep 22, 2015 at 12:49
  • That program cannot possibly display "null". For a start, it doesn't even attempt to display the inputs. Please describe your problem clearly and accurately if you want us to help you. Commented Sep 22, 2015 at 13:08
  • Please read this: java.util.NoSuchElementException - Scanner reading user input Commented Sep 22, 2015 at 13:37

2 Answers 2

2

The cause of the problem is this

Scanner in0  = new Scanner(System.in);
Scanner in1  = new Scanner(System.in);
Scanner in2  = new Scanner(System.in);

and this

in0.close();
...
in1.close();
...
in2.close();

When you create the Scanner, you work on System.in, then you close it. This cause that next Scanner operate on closed stream.

The solution is to create a single Scanner for InputStream.

Scanner scanner = new Scanner(System.in);

System.out.println("Give width\n");
double width = scanner.nextDouble();

System.out.println("Give period");
int period = scanner.nextInt();

System.out.println("\nInsert width peak to peak:");
double p2p = scanner.nextDouble();

This is only example that do not validate the user input.

Sign up to request clarification or add additional context in comments.

Comments

0
public static void main(String [] args) throws InputMismatchException{
    double width;
    int period;
    double Ppp;
    Scanner in0  = new Scanner(System.in);

    System.out.println("Give width\n");
    // This will read the line, and parse the result as a double, this way you can insert a number and press enter
    width = Double.parseDouble(in0.nextLine());

    System.out.println("Give period");
    period = Integer.parseInt(in0.nextLine());

    System.out.println("\n");
    System.out.println("Insert width peak to peak:");
    ppp = Double.parseDouble(in0.nextLine());

    in0.close();
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.