If you know exactly the number of input then use an Array, if not use a ArrayList
With Arrays
String []inpupts = new String[b];
for (int i=0; i< b; i++) {
System.out.println("Enter a string: ");
inputs[i] = in.nextLine();
}
With ArrayList
List<String> inpupts = new ArrayList<String>();
for (int i=0; i< b; i++) {
System.out.println("Enter a string: ");
inputs.add(in.nextLine());
}
Answer from Thusitha Thilina Dayaratne on Stack OverflowIf you know exactly the number of input then use an Array, if not use a ArrayList
With Arrays
String []inpupts = new String[b];
for (int i=0; i< b; i++) {
System.out.println("Enter a string: ");
inputs[i] = in.nextLine();
}
With ArrayList
List<String> inpupts = new ArrayList<String>();
for (int i=0; i< b; i++) {
System.out.println("Enter a string: ");
inputs.add(in.nextLine());
}
From your code (<= b) I am assuming you just started learning Java. Therefore, I edited your solution and am proposing the following, if this is okay?
public static void main(String[] args) {
int b;
Scanner in = new Scanner(System.in);
System.out.println("Enter verifying number: ");
b = in.nextInt();
//necessary to do due to Enter key pressed by user
in.nextLine();
String s[] = new String[b];
for (int i=0; i<b; i++) {
System.out.println("Enter a string: ");
s[i] = in.nextLine();
// You can check at the same time if this is what you entered
System.out.println("I have received this sring: "+s[i]+"\n");
}
How to use scanner for multiple inputs
java - Assigning multiple inputs to multiple variables using Scanner - Stack Overflow
java.util.scanner - How to iterate multiple String input in Java using the Scanner class - Stack Overflow
How does the Scanner class work? Two different inputs using the same Scanner Object prints statement twice?
What’s the best way to store multiple string inputs in Java for later use?
How do you handle exceptions or errors while taking multiple string inputs using Scanner?
How can I efficiently store multiple string inputs read by Scanner for further processing?
Videos
Hi all
Newbie here and totally stuck. I’m on an exercise for a course where I need to create a salary calculator
I need to have a user input several separate details on that will be used for calculations in separate methods
When reading course material apparently you should/can only use the scanner once? I.e user to input their wage
But I’ll need to ask them for more information. If the scanner should really only be used once am I missing another way of getting user input? I’ve started reading on ‘get & set’ but I’m not clear on how get would work here
Sorry for the poorly worded question, I’m only a few weeks in and adamant to try these exercises but im stumped here
First you will have to decide what is going to be considered The End Of User Input and then act upon that specific condition. In the little example below, if the User enters nothing then that will be considered the End Of User Input.
All the code numbers entered by the User are stored within a List Interface object. Rules have also been applied whereas all code numbers supplied must be numerical and eight digits in length. The String#matches() method is used for this along with a small Regular Expression (regex). Also, there can be no duplicate code numbers supplied, each code number must be unique.
When the User has finished entering the desired Code Numbers then those numbers are sorted in ascending order and displayed within the console window:
System.out.println("Enter the required code numbers (enter nothing when done): ");
Scanner in = new Scanner(System.in);
List<String> codes = new ArrayList<>();
// Outer loop to keep asking for Code Numbers
while (true) {
boolean isValid = false; // Flag to ensure a valid entry
String codeLine = "";
// inner loop to back up valitity before storing supplied code Number.
while (!isValid) {
System.out.print("Code Line: --> ");
codeLine = in.nextLine().trim();
// Break out of this inner loop if nothing is supplied
if (codeLine.isEmpty()) { break; }
// Is the supplied number all digits and are there 8 of them?
if (!codeLine.matches("^\\d{8}$")) {
// No...
System.err.println("Invalid Code Number! Try Again...");
}
// Has the supplied number already been previously stored?
// In other words, is it unique?
else if (codes.contains(codeLine)) {
// Already got it! Not Unique!
System.err.println("Code Number: " + codeLine + " has already been supplied!");
}
// Passed validity! Set isValid to true so to exit this inner loop.
else { isValid = true; }
}
// Break out of the outer loop is nothing was supplied.
if (codeLine.isEmpty()) { break; }
// Validity has been met so Store the the supplied code number.
codes.add(codeLine);
}
in.close(); // Close the input Stream
System.out.println("Scanner closed");
// Sort the Stored Code Numbers in ascending order.
Collections.sort(codes);
// Display the Stored Code Numbers...
System.out.println("Code Numbers Entered:");
System.out.println(codes);
The issue is that your code does not know if the user has stopped entering the codes.
You can do something like this :
public static void main(String[] args) {
System.out.println("Enter the Codes: ");
Scanner in = new Scanner(System.in);
String nextLine = "";
do{
nextLine = in.nextLine();
System.out.println("nextLine:" + nextLine);
} while(!nextLine.equals("exit"));
in.close();
System.out.println("Scanner closed");
}
And you console will look like:
