There is no direct way to take array input in Java usingScanner or any other utility, but it's pretty easy to achieve the same by using standardScanner methods and asking some questions to the user. For example, if you want to take a one-dimensional array of String as input then you can first ask the user about thelength of the array and then you can use afor loop to retrieve that many elements from the user and store them in an array. You can use thenext() to take a String input from the user. Similarly, if you need to take an integer array or double array, you can use the nextInt() ornextDouble() method of the Scanner class.
Some programmers may still be stuck and ask, how about taking a two-dimensional array as input? Well isn't that pretty easy by applying some maths?
If you look, a two-dimensional array is like a matrix, You need both numbers of rows and columns to create and populate thetwo-dimensional array in Java. Just ask the user and then read subsequent input to store in the 2-dimensional array as row by row.
Some programmers may still be stuck and ask, how about taking a two-dimensional array as input? Well isn't that pretty easy by applying some maths?
If you look, a two-dimensional array is like a matrix, You need both numbers of rows and columns to create and populate thetwo-dimensional array in Java. Just ask the user and then read subsequent input to store in the 2-dimensional array as row by row.
Don't worry, in this article, I'll show you a couple of examples abouthow to take array input in Java using Scanner.
And, If you are new to the Java world then I also recommend you go through these free Java Programming courses to learn Java in a better and more structured way. This is one of the best and up-to-date courses to learn Java online.
Depending on which type of array you are taking as input e.g.String orint or any other array, you need to use thenext() ornextInt() method toread a value from the command prompt.
And, If you are new to the Java world then I also recommend you go through these free Java Programming courses to learn Java in a better and more structured way. This is one of the best and up-to-date courses to learn Java online.
Java Program to take array input from User
Here is our sample Java program to demonstratehow to take an array as input from the user. As I told you, there is no direct way but you can use a for loop toread user input and save them into the array. By using this technique you can also take a two-dimensional array as input.Depending on which type of array you are taking as input e.g.String orint or any other array, you need to use thenext() ornextInt() method toread a value from the command prompt.
You can then save this value into array by assigning to respective index e.g.input[i] = sc.next().
You can see that we have successfully taken array input from the user, both String and integer array, and bothone and atwo-dimensional array.
Don't forget to close the Scanner once you have done to prevent resource leaks in Java, you can also see theseJava programming coursesto learn more about why you should close readers and stream once you are done using them.
2) I have used theArrays.toString() andArrays.deepToString() to display actual elements of the array into the console because the array in Java doesn't override thetoString() method and directly printing them will not be very meaningful as discussedhere.
3) If you want to learn more about such fundamentals of Java Programming language, I strongly suggest reading theCore Java Volume 1 - Fundamentals by Cay S. Horstmann. One of the best and most readable book on core Java at the moment. It also covers Java SE 8.
Finally, here are some important points about the Scanner class which you should remember while using it in the Java program:
That's all abouthow to take array input in Java using Scanner class. It's a simple technique to save input from the user into an array and can be used to save input in both one and multi-dimensional arrays. Since Scanner allows you to read anint,String,double,float,byte, andlongyou can create those types of array. There is no method to read characters but you can read the character as a String to create a char array.
Program to take an array as input from user in Java
importjava.util.Arrays;importjava.util.Scanner;/* * Java Program to take array input from the user using Scanner. */publicclassArrayInputDemo {publicstaticvoidmain(String[] args) {// taking String array input from user Scanner sc =new Scanner(System.in); System.out.println("Please enter length of String array");int length = sc.nextInt();// create a String array to save user input String[] input =new String[length];// loop over array to save user input System.out.println("Please enter array elements");for (int i =0; i < length; i++) { String userInput = sc.next(); input[i] = userInput; } System.out.println("The String array input from user is : "); System.out.println(Arrays.toString(input));// saving user input inside a 2D array in Java System.out.println("Please enter number of rows and columns of 2D array");int rows = sc.nextInt();int columns = sc.nextInt();int[][] data =newint[rows][columns]; System.out.println("Please enter array elements row by row");for (int i =0; i < rows; i++) {for (int j =0; j < columns; j++) {int value = sc.nextInt(); data[i][j] = value; } } System.out.println("The 2d int array input from user is : "); System.out.println(Arrays.deepToString(data)); sc.close(); }}Now, let's see the output of this program to understand how to take array as input from command line in Java:OutputPlease enter length of String array3Please enter array elementsJavaC++RubyThe String array input from user is : [Java, C++, Ruby]Please enter number of rows and columns of2D array23Please enter array elements row by row123456The2dint array input from user is : [[1,2,3], [4,5,6]]
You can see that we have successfully taken array input from the user, both String and integer array, and bothone and atwo-dimensional array.
Don't forget to close the Scanner once you have done to prevent resource leaks in Java, you can also see theseJava programming coursesto learn more about why you should close readers and stream once you are done using them.
Important points
1) Usenext() to read String instead ofnextLine()which is used to read the entire line. This is useful when you read a file line by line in Java as shownhere.2) I have used theArrays.toString() andArrays.deepToString() to display actual elements of the array into the console because the array in Java doesn't override thetoString() method and directly printing them will not be very meaningful as discussedhere.
3) If you want to learn more about such fundamentals of Java Programming language, I strongly suggest reading theCore Java Volume 1 - Fundamentals by Cay S. Horstmann. One of the best and most readable book on core Java at the moment. It also covers Java SE 8.
Finally, here are some important points about the Scanner class which you should remember while using it in the Java program:
That's all abouthow to take array input in Java using Scanner class. It's a simple technique to save input from the user into an array and can be used to save input in both one and multi-dimensional arrays. Since Scanner allows you to read anint,String,double,float,byte, andlongyou can create those types of array. There is no method to read characters but you can read the character as a String to create a char array.
4 comments :
excellent explaination
Thank you for another excellent explanation
Very clear
How can we input array in c.
Post a Comment