user input program

How To Get User Input Using Console

Get User Input

Get User Input – Creating data by manually changing the variable inside the code is a usual thing to do. But, what if the data you need doesn’t exist yet? and that is until someone inputs it to your program.

Let’s do a small step and try to get input from users using the IDE’s console/terminal. This is a very easy thing to do, you can try to edit the code in any way you want it.

import java.util.Scanner;

public class GetInputData{
public static void main(String args[]){

int num;
int num2;
int num3;

float fnum;

String str;
String str2;
String str3;

Scanner in = new Scanner(System.in);

//Get input String
System.out.println(“Enter a string: “);
str = in.nextLine();
System.out.println(“Input String is: “+str);

System.out.println(“Enter a string: “);
str2 = in.nextLine();
System.out.println(“Input String is: “+str2);

System.out.println(“Input String is: “);
str3= in.nextLine();
System.out.println(“Input String is: “+str3);

//Get input Integer
System.out.println(“Enter an integer: “);
num = in.nextInt();
System.out.println(“Input Integer is: “+num);

System.out.println(“Enter an integer: “);
num2 = in.nextInt();
System.out.println(“Input Integer is: “+num2);

System.out.println(“Enter an integer: “);
num3 = in.nextInt();
System.out.println(“Input Integer is: “+num3);

//Get input float number
System.out.println(“Enter a float number: “);
fnum = in.nextFloat();
System.out.println(“Input Float number is: “+fnum);

}}

If you run this program on your IDE, then you can see this message in your console :

get user input program

try to put any string data into your console and hit enter. The program will surely output that data to the console again, like this :

user input program

But be careful, the user needs to input data that will match its variable. If they input something that does not match its variable, an error will happen :

user input program

The programming language that I specialize in is Java, because I think Java programming language is more universal and of course because I like it regardless of any reason.

Published by

Rafif Santika

The programming language that I specialize in is Java, because I think Java programming language is more universal and of course because I like it regardless of any reason.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.