Java – requires multiple input from the user
•
Java
I'm trying to write a program that keeps asking users for an integer until they enter a non integer value, and the program stops
This is what I have done so far:
import java.util.Scanner; import java.util.ArrayList; public class InputStats { private static Scanner a; public static void main(String[] args) { Scanner input = new Scanner(system.in); ArrayList InputArray = new ArrayList(); Boolean Running = true; System.out.println("Please type an integer. To stop enter a non integer value"); while (Running) { a = Scanner.nextLine(); if (!a.hasNextInt()) { Running = false; } else { InputArray.add(input.nextLine()); } System.out.println(InputArray); } } }
However, with this code, I received an error:
error: non-static method nextLine() cannot be referenced from a static context (for the line a = Scanner.nextLine();)
and
error: incompatible types (for the line a = Scanner.nextLine();)
What could be the problem?
Solution
String str = input.nextLine();
String str = input.nextLine();
You don't need a scanner at all Simply replace all references to the input with references to it
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码