Java Program to Read an Array of Strings using Scanner
Welcome to CodeWithBK — your go-to hub for mastering Java programming.
In this video, we will learn how to write a Java program to read an array of Strings using the Scanner class . This is a common beginner-level task that helps in understanding how to work with arrays and user input in Java.
This program is solved based on the 4 possible cases of input from the user:
Case 1: Input consists of two lines
the first line of input is the number of elements in the array: n
the second line of input contains n space separated strings
each string to be read DOES NOT CONTAIN A WHITESPACE
Case 2: Input consists of one line
the first line of input contains n space separated Strings
each string to be read DOES NOT CONTAIN A WHITESPACE
Case 3: Input consists of n+1 lines
the first line of input is the number of elements in the array: n
the following n lines each contain an input string
since each input string is in a separate line, input string CAN CONTAIN WHITESPACES
Case 4: Input consists of n lines
n lines each contain a string
since each input string is in a separate line, input string CAN CONTAIN WHITESPACES
In this video, you will:
Understand how to use the Scanner class for reading Strings
Learn how to take multiple String inputs from the user
Store inputs in a String array
Handle the possible cases of input
Iterate over the array to display the elements
Strengthen your understanding of arrays, loops, and user input handling in Java
Why learn this program?
Reading and storing multiple Strings from user input is a fundamental skill in Java. It helps you practice loops, indexing, and array handling . Understanding how to take dynamic input from the user is essential for solving real-world coding problems and for Java interview preparation .
If you want to master Java arrays and improve your input handling skills using the Scanner class , this tutorial is a perfect starting point.
Perfect for:
B.Tech / BCA students
AP Computer Science students learning Java
Beginners learning Java basics and input/output handling
Students preparing for school or college exams in computer science
Developers practicing Java array problems
Anyone wanting to build a strong foundation in string and array manipulation
Programming Language: Java
Level: Beginner-friendly
Related Videos:
next() method Scanner class Java : • Java Program: Using next() Method of Scann...
nextInt() method of Scanner class Java : • Java – Using nextInt() in Scanner
nextInt() method with nextLine() of Scanner class Java : • Java Scanner nextInt() vs nextLine() | Ful...
Timestamps:
0:00 Intro
0:24 Java code to read array input of Strings for input case 1
5:14 Java code to read array input of Strings for input case 2
10:39 Java code to read array input of Strings for input case 3
14:51 Java code to read array input of Strings for input case 4
🔔 Don’t forget to like, share, and subscribe to CodeWithBK for more such Java tutorials in simple language!
Stay connected:
Subscribe: / @codewithbk
Email: [email protected]
Discord: / discord
Coding mentor profile: https://www.codementor.io/@balkrishna...
For 1:1 sessions, reach out to me here : https://www.codementor.io/@balkrishna...
My YouTube channel where I teach in Hindi : / @codewithbkhindi
#JavaTutorial #JavaBeginners #CodeWithBK #JavaBeginners #JavaInterviewQuestions #JavaBasics #javatutorialforbeginners #javaprogrammingbasics #javaprogrammingforbeginners