To take input from a user in Java, you can use the Scanner
class which is a part of the Java standard library. Here's an example:
import java.util.Scanner; public class Example { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print("Enter your name: "); String name = scanner.nextLine(); System.out.print("Enter your age: "); int age = scanner.nextInt(); System.out.println("Your name is " + name + " and you are " + age + " years old."); } }
In this example, we create a new Scanner
object and use it to read input from the user. We first ask the user to enter their name and store the input in a String
variable called name
. We then ask the user to enter their age and store the input in an int
variable called age
.
Finally, we print out a message that includes the values of name
and age
that were entered by the user.
Note that the Scanner
class has many other methods for reading different types of input (e.g. nextBoolean()
, nextDouble()
, etc.) that you can use depending on the type of input you need to read.
Australia
UK
UAE
Singapore
Canada
New
Zealand
Malaysia
USA
India
South
Africa
Ireland
Saudi
Arab
Qatar
Kuwait
Hongkong
Copyright 2016-2023 www.programmingshark.com - All Rights Reserved.
Disclaimer : Any type of help and guidance service given by us is just for reference purpose. We never ask any of our clients to submit our solution guide as it is, anywhere.