![java scanner java scanner](https://i.ytimg.com/vi/rxuibVkUDjY/maxresdefault.jpg)
Let us demonstrate it through a Java program:- How to Take Multiple String Input in Java using Scanner? import The nextLine() method is declared as follows:- public String nextLine() While reading string value generally we prefer to read one line therefore we will focus on the nextLine() method. In scanner class, we have the next() method which reads one word at a time, and nextLine() method which reads one line at a time.
![java scanner java scanner](https://pbs.twimg.com/media/EQQqDvYWsAcxXde.png)
There are two different options:- read one word or read one line. To store multiple String inputs we need a String array. Now in this post, we will discuss how to take multiple String input in Java using Scanner.
#JAVA SCANNER CODE#
To learn more about coding in Java, read our How to Code in Java guide.In the previous Java program, we had seen how to take string input in Java using scanner class. In addition, we discussed the different data types offered by the Scanner class that we can use to collect user input. In this tutorial, using a few examples, we explored how to use the Java Scanner class to collect user input. The Scanner class is capable of collecting a variety of data types from users, including short values, Strings, booleans, and others. You can use Java’s Scanner class to collect input from a user. We use the nextBoolean() method to collect this value from the user. However, this time we collect an additional value from the user: whether the product they inserted into the program is on display. Our program works in the same way as our example above. As mentioned, we had to use different code to collect these types of data.īoolean on_display = input.nextBoolean() In our above example, we collected two types of data from the user: a string and an integer. When we collected the product name, we used input.next(), and when we collected the product quantity, we used input.nextInt(). Notice that we used different code to collect numbers and strings. We print out the value of the quantity variable to the console.We print out Quantity: to the console and prompt the user to submit the quantity of the product in stock using input.nextInt().We print out to the console the product name the user submitted.We print out “Product name: ” to the console and ask the user to submit a product name using input.next().We initialize the Scanner class using Scanner input = new Scanner(System.in) The input Java variable stores our initialized scanner.We declare a class called Main that stores the code for our program.We import the Scanner library into our code so that we can receive user input.» MORE: How to Convert a Java String to Char Array How our Scanner Java Program Works This allows us to verify that our program is working. It then returned to the console the value the user entered. When we run our code and insert a few example values, the program returns the following response:Īs you can see, our program collected the user’s input. In the code below, we define this number with the code: int quantity, where int stands for integer. The next input is the quantity of the item. In the code below, we define this string with the code: String product_name. This will be a string because the item names are text-based and use a variety of characters.
![java scanner java scanner](https://linuxhint.com/wp-content/uploads/2022/03/How-to-get-User-Input-in-Java-using-scanner-class.jpg)
The first input we accept the name of the item. The following is the code for each of the above methods: