
Java Program To accept different types of inputs in aline at atime from the keyboard (like name,age,salary,gender) just like one can do using scanf() in c(using StringTokenizer)

import java.util.*;
class AcceptData
{
public static void main(String[] args)
{
Scanner input=new Scanner(System.in);
System.out.print("Enter Name: ");
String name=input.next();
System.out.print("Enter Age: ");
int age=input.nextInt();
System.out.print("Enter Salary: ");
int sal=input.nextInt();
System.out.print("Enter Gender: ");
String gender=input.next();
String str = "Name="+name+";Age=" +age+";Salary="+sal+";Gender="+gender+";";
StringTokenizer st = new StringTokenizer(str, "=;");
while(st.hasMoreTokens()) {
String key = st.nextToken();
String val = st.nextToken();
System.out.println(key + ":\t" + val);
}
}
}
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.