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 Overflow
🌐
Upgrad
upgrad.com › home › blog › software development › how to take multiple string input in java using scanner: a step-by-step guide
How to Take Multiple String Input in Java Using Scanner
September 10, 2025 - Java provides several ways to achieve this, most notably using the Scanner class, which simplifies reading strings from the console or other input sources. Ready to deepen your understanding of critical tech skills and advance your career? Choose from our top-rated courses designed to elevate your technical skills and boost your career—enroll now and take the first step toward your success!
Discussions

How to use scanner for multiple inputs
You can use a Scanner as many times as you want. More on reddit.com
🌐 r/learnjava
10
3
December 5, 2020
java - Assigning multiple inputs to multiple variables using Scanner - Stack Overflow
What I am trying to do is have multiple inputs that all have different variables. Each variable will be part of different equations. I am looking for a way to do this, and I think I have an idea. I... More on stackoverflow.com
🌐 stackoverflow.com
java.util.scanner - How to iterate multiple String input in Java using the Scanner class - Stack Overflow
I need to get multiple line Input from a User in Java. Currently I'm using the Scanner class to get the input, but the loop I'm using to check each line does not break. Current Code package getxml; More on stackoverflow.com
🌐 stackoverflow.com
July 1, 2021
How does the Scanner class work? Two different inputs using the same Scanner Object prints statement twice?
Your problem results from the combination of .nextInt() and .nextLine() (and that is precisely the reason the MOOC stipulates using Integer.parseInt(scanner.nextLine()) over the dedicated methods). The .nextInt() method leaves the line break in the keyboard buffer and the following .nextLine() consumes it leading to an empty String. Either use the MOOC method with .parseXX(scanner.nextLine()), or add a dummy scanner.nextLine() before any real nextline input that follows a .nextInt(). More info: The Scanner class and its caveats from the r/javahelp wiki. More on reddit.com
🌐 r/learnjava
7
20
November 5, 2019
People also ask

What’s the best way to store multiple string inputs in Java for later use?
When handling multiple string inputs, especially if the count isn’t fixed, use dynamic data structures like ArrayList. After reading strings using scanner.nextLine() or splitting a line into tokens, add each entry to the list. This allows you to easily perform operations like searching, filtering, or mapping over the input later in the program.
🌐
upgrad.com
upgrad.com › home › blog › software development › how to take multiple string input in java using scanner: a step-by-step guide
How to Take Multiple String Input in Java Using Scanner
How do you handle exceptions or errors while taking multiple string inputs using Scanner?
Scanner input operations can throw exceptions such as NoSuchElementException or IllegalStateException if input is prematurely closed or unavailable. Although strings rarely cause format issues like numbers do, you should still handle unexpected inputs or stream closure gracefully. Using try-catch blocks around input reading logic ensures your program can inform users of errors and request re-entry instead of crashing. This practice is critical in interactive applications like user registration or command-line tools, where reliability improves user trust and reduces debugging time.
🌐
upgrad.com
upgrad.com › home › blog › software development › how to take multiple string input in java using scanner: a step-by-step guide
How to Take Multiple String Input in Java Using Scanner
How can I efficiently store multiple string inputs read by Scanner for further processing?
After capturing multiple strings, especially when the number is dynamic, using collections such as ArrayList or LinkedList provides flexibility for storage and manipulation. For example, after splitting a line into tokens or reading tokens one by one in a loop, you can add each string to an ArrayList. This is beneficial in applications like search engines, where input keywords need to be stored, filtered, or searched. Collections allow you to apply operations like sorting, filtering, or mapping strings, improving your program’s scalability and performance.
🌐
upgrad.com
upgrad.com › home › blog › software development › how to take multiple string input in java using scanner: a step-by-step guide
How to Take Multiple String Input in Java Using Scanner
🌐
SourceBae
sourcebae.com › home › how to take multiple string input in java using scanner?
How to Take Multiple String Input in Java Using Scanner? - SourceBae
August 21, 2025 - Learn how to take multiple string input in Java using the Scanner class. Follow this simple guide to handle user input efficiently in Java.
🌐
Javatpoint
javatpoint.com › how-to-take-multiple-string-input-in-java-using-scanner
How to Take Multiple String Input in Java Using Scanner - Javatpoint
How to Take Multiple String Input in Java Using Scanner with java tutorial, features, history, variables, object, class, operators, for-loop, oops concept, array, string, map, math, methods, examples etc.
🌐
Know Program
knowprogram.com › home › program to take multiple string input in java using scanner
Program to Take Multiple String Input in Java using Scanner
September 20, 2021 - Then we created a String array of size 5 to store five-string values. Using loop and nextLine() method this program read 5 string value and stored into the string array. After that, using loop string elements are fetched and displayed to the screen.
🌐
Brainly
brainly.com › computers and technology › high school › how do you take multiple string inputs in java using the scanner class?
[FREE] How do you take multiple string inputs in Java using the Scanner class? - brainly.com
June 13, 2023 - To take multiple string inputs in Java, you need to import the Scanner class, create an instance, prompt for the number of strings, and use a loop to read and store each input. Make sure to consume the newline character after reading the integer ...
Find elsewhere
🌐
Talkerscode
talkerscode.com › howto › how-to-take-multiple-string-input-in-java-using-scanner.php
How To Take Multiple String Input In Java Using Scanner
To begin, construct a Scanner object to read console input. Users should then be prompted to enter the desired number of strings. To read in each line using the nextLine() function of the Scanner class and save it in the array, create a string ...
🌐
Tpoint Tech
tpointtech.com › how-to-take-multiple-string-input-in-java-using-scanner
How to Take Multiple String Input in Java Using Scanner - Tpoint Tech
March 17, 2025 - In Java, Scanner is a class that provides methods for input of different primitive types. It is defined in java.util package.
🌐
Baeldung
baeldung.com › home › java › java io › read multiple inputs on the same line in java
Read Multiple Inputs on the Same Line in Java | Baeldung
December 14, 2023 - Scanner scanner = new Scanner(System.in); scanner.useDelimiter(";"); System.out.print("Enter two numbers separated by a semicolon: "); int num1 = scanner.nextInt(); int num2 = scanner.nextInt(); System.out.println("You entered " + num1 + " and " + num2); In this example, we use a semicolon as a delimiter instead of a space. We also call the setDelimiter() method to set the delimiter to a semicolon. In addition to using a space or a custom delimiter, we can also use regular expressions as delimiters when reading multiple inputs on the same line. Regular expressions are patterns that can match strings flexibly and powerfully.
🌐
Quora
quora.com › How-do-I-read-multiple-inputs-in-Java-using-Scanner-class
How to read multiple inputs in Java using Scanner class - Quora
Answer (1 of 5): import java.util.Scanner; class Scan { public static void main (String…arhs) { Scanner sc = new Scanner(System.in) System.out.println(“enter name “); String name= sc.nextLine(); System.out.println(name); System.out.println(“enter age “); int age= sc.nextInt(); System.ou...
🌐
Sololearn
sololearn.com › en › Discuss › 1945966 › how-can-we-take-multiple-inputs-in-java-can-we-do-this-scanner-abc-new-scannersystemin
How can we take multiple inputs in java ? Can we do this "Scanner a,b,c = new Scanner(System.in); | Sololearn: Learn to code for FREE!
August 27, 2019 - //Create the Scanner object Scanner s = new Scanner(System.in); //Declare some string variables String a,b,c; //when the input screen pops up and you enter charactors on 3 diffrenet lines each line is assigned to a seprate variable a=s.nextLine(); ...
🌐
Sololearn
sololearn.com › en › Discuss › 1787956 › how-to-scan-multiple-string-in-a-single-line
How to scan multiple string in a single line? | Sololearn: Learn to code for FREE!
For example there are 2 strings. First one is cat secone one is move. Then the input of the code should be cat move. ... Version 4: Scanner scan = new Scanner(System.in); //.next() reads a String until a white space //it reads not the full line String a = scan.next(); String b = scan.next(); System.out.println(a + " " + b); input: cat move output: cat move
🌐
GeeksforGeeks
geeksforgeeks.org › java › java-user-input-scanner-class
Java User Input - Scanner Class - GeeksforGeeks
In this example, we read different types of input such as strings, integers, and floating-point values. ... import java.util.Scanner; public class Geeks { public static void main(String[] args) { Scanner scn = new Scanner(System.in); // Reading a single line string System.out.print("Enter a sentence: "); String sentence = scn.nextLine(); System.out.println("Entered Sentence: " + sentence); // Reading an integer System.out.print("Enter an integer: "); int x = Integer.parseInt(scn.nextLine()); System.out.println("Entered Integer: " + x); // Reading a float value System.out.print("Enter a float value: "); float f = Float.parseFloat(scn.nextLine()); System.out.println("Entered Float Value: " + f); scn.close(); } }
Published   January 16, 2026
🌐
YouTube
youtube.com › tamanna murad
How to take multiple string input in java using Scanner ? | in Hindi - YouTube
How to take multiple string input in java using Scanner.scanner classjava scanner input tutorialjava user inputjava tutorialmultiple string input #java#quest...
Published   August 27, 2024
Views   3
🌐
Reddit
reddit.com › r/learnjava › how to use scanner for multiple inputs
r/learnjava on Reddit: How to use scanner for multiple inputs
December 5, 2020 -

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

🌐
TutorialsPoint
tutorialspoint.com › article › how-to-input-multiple-values-from-user-in-one-line-in-java
How to input multiple values from user in one line in Java?
July 13, 2020 - import java.util.Scanner; public class Demo { public static void main(String[] args) { System.out.print("Enter two floating point values : "); Scanner my_scan = new Scanner(System.in); double double_val = my_scan.nextFloat(); int int_val = my_scan.nextInt(); System.out.println("The floating point value is : " + double_val + " and the integer value is : " + int_val); } }
Top answer
1 of 3
1

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);
2 of 3
1

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:

🌐
Medium
medium.com › @AlexanderObregon › reading-multiple-lines-of-input-with-java-scanner-4901bb99cbbd
Reading Multiple Lines of Input with Java Scanner | Medium
July 24, 2025 - public static List<String> collectBufferedInputUntil(BufferedReader reader, Predicate<String> stopCondition) throws IOException { List<String> result = new ArrayList<>(); String line; while ((line = reader.readLine()) != null) { if (stopCondition.test(line)) { break; } result.add(line); } return result; } The idea stays the same. You decide how to stop, and the loop takes care of the rest. Scanner is easy to work with when input is small and well-structured.