you can use a Scanner to read from input :
Scanner scanner = new Scanner(System.in);
char c = scanner.next().charAt(0); //charAt() method returns the character at the specified index in a string. The index of the first character is 0, the second character is 1, and so on.
Answer from unfamous on Stack OverflowHow to take character input in java - Stack Overflow
java - Read multiple characters in char input and convert to double - Stack Overflow
java - Reading multiple char inputs on one line - Stack Overflow
arrays - Getting multiple inputs from user with char and integers Java - Stack Overflow
What’s the best way to store multiple string inputs in Java for later use?
How do you handle exceptions or errors while taking multiple string inputs using Scanner?
How can I optimize Scanner usage when taking multiple string inputs in large-scale applications?
you can use a Scanner to read from input :
Scanner scanner = new Scanner(System.in);
char c = scanner.next().charAt(0); //charAt() method returns the character at the specified index in a string. The index of the first character is 0, the second character is 1, and so on.
import java.util.Scanner;
class SwiCas {
public static void main(String as[]) {
Scanner s= new Scanner(System.in);
char a=s.next().charAt(0);//this line shows how to take character input in java
switch(a) {
case 'a':
System.out.println("Vowel....");
break;
case 'e':
System.out.println("Vowel....");
break;
case 'i':
System.out.println("Vowel....");
break;
case 'o':
System.out.println("Vowel....");
break;
case 'u':
System.out.println("Vowel....");
break;
case 'A':
System.out.println("Vowel....");
break;
case 'E':
System.out.println("Vowel....");
break;
case 'I':
System.out.println("Vowel....");
break;
case 'O':
System.out.println("Vowel....");
break;
case 'U':
System.out.println("Vowel....");
break;
default:
System.out.println("Consonants....");
}
}
}
If you separate your input using whitespace and then press enter like this:
2 3
You can use nextInt() normally:
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
a = in.nextInt();
b = in.nextInt();
}
a will contain 2 and b will contain 3.
If you need to, you can place both nextInt() calls inside a loop, so you can read multiple lines:
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int numLines = in.nextInt();
int a, b;
for (int i = 0; i < numLines; i++) {
a = in.nextInt();
b = in.nextInt();
System.out.println(a + ", " + b);
}
}
To take multiple inputs from the user from the same input line, you can ask the user to seperate the input with a comma or a white space.
In case of a white space, Anderson Vieira's answer gives the correct idea. You can use 2 successive nextInt() statements as in
System.out.println("Enter 2 nos.");
int a = sc.nextInt();
int b = sc.nextInt();
Also, you can specifically ask the user to seperate two nos. with a comma, take the input as a String and then seperate the two nos. using Integer.parseInt() command.
System.out.println("Enter 2 nos. seperated with a comma(,) or a semicolon(;)");
String S = sc.nextLine();
String nos[] = S.split("[, ;]");
int a = Integer.parseInt(nos[0]);
int b = Integer.parseInt(nos[1]);
You can ofcourse use any other character to seperate the nos.