You could take the first character from Scanner.next:
char c = reader.next().charAt(0);
To consume exactly one character you could use:
char c = reader.findInLine(".").charAt(0);
To consume strictly one character you could use:
char c = reader.next(".").charAt(0);
Answer from Reimeus on Stack OverflowYou could take the first character from Scanner.next:
char c = reader.next().charAt(0);
To consume exactly one character you could use:
char c = reader.findInLine(".").charAt(0);
To consume strictly one character you could use:
char c = reader.next(".").charAt(0);
Setup scanner:
reader.useDelimiter("");
After this reader.next() will return a single-character string.
java - how to take user inputs in eclipse? - Stack Overflow
How to use Special Chars in Java/Eclipse - Stack Overflow
How to take character input in java - Stack Overflow
eclipse - Input values to a normal JAVA SE program - Stack Overflow
Run --> Run Configurations --> Arguments (it is the 2nd tab on the right) --> Program arguments
Add this line in your program to accept user's input from the console:
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
and then add in.readLine() beside whatever variable you want to accept input from Runtime. Lets say, you want to initialize count variable to the value 1, then, it should be written as
int count = in.readLine();
The value 1 should be inputted in the console after you run the program
The problem is that the characters you are using cannot be represented in the encoding you have the file set to (Cp1252). The way I see it, you essentially have two options:
Option 1. Change the encoding. According to IBM, you should set the encoding to UTF-8. I believe this would solve your problem.
- Set the global text file encoding preference Workbench > Editors to "UTF-8".
- If an encoding other than UTF-8 is required, set the encoding on the individual file rather than using the global preference setting. To do this use the File > Properties > Info menu selection to set the encoding on an individual file.
Option 2. Remove the characters which are not supported by the "Cp1252" character encoding. You can replace the unsupported characters with Unicode escape sequences (\uxxxx). While this would allow you to save your file, it is not necessarily the best solution.
For the characters you specified in your question here are the Unicode escape sequences:
♥ \u2665
♦ \u2666
♣ \u2663
♠ \u2660
In Eclipse:
- Go to Window -> Preferences -> General -> Workspace -> TextFileEncoding
- Set it to UTF-8
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 I remember correctly, you can actually type in Eclipse's Java console and that will be sent through the standard input stream, System.in (probably depending on the application's run configuration).
However the standard way of doing this is to pass a number of strings on the command line, which will appear as the elements of the String[] parameter to your application's main method.
Alternatively you can get input in via flat files, from a database, through sockets etc. though this is, as you might expect, a lot more complex and should certainly not be considered for a "Hello World" type application. Java web services, for example, do take their input via network sockets rather than via the command line as this latter would restrict them somewhat. ;-)
Using eclipse, you need to update the Run Configuration for a given Class. Provide any arguments you want to pass into the main method.