All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

How To Take Integer Input From User In Java

Last Updated : Jul 1, 2023

How To Take Integer Input From User In Java

In this article we will show you the solution of how to take integer input from user in java, taking user input is a typical requirement in many Java programmes.

When a Java programme asks the user to submit an integer value using the keyboard or another input device, it signifies that the programme reads the user-provided value and applies it to its logic.

Now let's talk about the idea of taking integer input from the user in Java.

Step By Step Guide On How To Take Integer Input From User In Java :-

We can receive numeric input from a user in Java by using a scanner class, which is a component of the java.util package.

The Scanner class offers a number of ways to scan various user inputs, such as the integer, float, double, & texts.

import java.util.Scanner;
public class ToDArray{
    public static void main(String args[]){
       System.out.print("Enter the 2D array size : ");
       Scanner sc=new Scanner(System.in);
       int rows=sc.nextInt();
       int columns=sc.nextInt();
       System.out.println("Enter array elements : ");
       int twoD[][]=new int[rows][columns];
        for(int i=0; i<rows;i++)
         {
            for(int j=0; j<columns;j++)
            {
                twoD[i][j]=sc.nextInt();
            }
         }
        System.out.print("\nData you entered : \n");
        for(int []x:twoD){
            for(int y:x){
            System.out.print(y+" ");
            }
            System.out.println();
        }
    }
}
  1. Here, you can see how we created a straightforward Java programme that prints the user's inputted data back to the console after allowing them to enter the size and items of a two-dimensional array employing the Scanner class.
  2. We import the Java.util package's Scanner class in the first line of code.
  3. The console is just one of the places that this class is used to read user input.
  4. The following line defines TwoDArray as a public class, which contains the main method, and that's where the programme starts.
  5. The System.out.print command is used to print a message such as "Enter 2D array size:" to the console in the main method's first line of code.
  6. The user is then prompted to provide the 2D array's size.
  7. The following line then generates a new Scanner object called sc.
  8. This object reads data from the console's simulated System.in stream.
  9. The following two numeric inputs are then received from the console by the third and fourth lines of code, which store them in the columns and rows of variables, respectively, using the Scanner object's nextInt method.
  10. These variables indicate the dimensions of the user's upcoming 2D array.
  11. Then, using the System.out.println statement, the following line of code prints the phrase "Enter array elements:" to the console.
  12. The user is then prompted to enter the 2D array's elements.
  13. A new 2D array with the desired number of columns and rows is declared in the sixth line of code and given the name twoD.
  14. The 2D array's elements are iterated over in the following few lines using a nested for loop, and the nextInt method of the Scanner object is used to read user input.
  15. The value given by the user is used to set the twoD[i][j] element.
  16. The following few lines of code use the System.out.print statement to print a message stating "Data you entered:" to the console when the user has entered all the components.
  17. The following loop uses the System.out.print statement to print the values in a tabular manner for each row of the 2D array.
  18. Finally, the final line of code uses the System.out.println statement to print a new line to the console and advance the cursor to the following line.

Conclusion :-

As a result, we have successfully mastered the Java idea of taking integer input from the user.

We also learned how to read input from the user and save it in two-dimensional arrays in a Java programme using the Scanner class.

It requests input from the user, reads it with the help of the Scanner object, stores it in a 2D array, & then outputs the user-entertainment data back to the console in a tabular style.

This programme is a simple illustration of how to use Java's Scanner class to read and manipulate 2D arrays.

I hope this article on how to take integer input from user in java helps you and the steps and method mentioned above are easy to follow and implement.