Take a char input from the Scanner
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);
Reading an int and a char input using a scanner?
I would suggest you read a whole line, then parse what you need.
Note: This solution will only work for single character / digit combos. Use a regular expression if you want something more complex.
public static void main (String args[]){
char column;
int row;
Scanner scan = new Scanner(System.in);
Othello game = new Othello();
//game.startGame();
while (true) {
game.displayBoard();
do {
System.out.print("Enter the column, then row, for example (A0): ");
String line = scan.nextLine();
column = line.charAt(0);
row = Integer.parseInt("" + line.charAt(1));
} while (!( game.checkColumn(column) && game.checkRow(row) ); //loop until input is valid
game.takeTurn(column, row); // Remove game as a parameter here. It is not needed
// if (gameOver) break;
}
}
I am trying to take to char inputs from a single string using Scanner with .charAt()
You can use the nextLine method of Scanner class
String userInput = input.nextLine();
char noOfDie = userInput.charAt(0);
char dieType = userInput.charAt(2);
The method returns a string of a complete line entered by the user.
Works by giving your input and pressing enter.
How to take character input in java
use the System class
char yourChar = System.in.read()
Scanner method to get a char
To get a char
from a Scanner
, you can use the findInLine
method.
Scanner sc = new Scanner("abc");
char ch = sc.findInLine(".").charAt(0);
System.out.println(ch); // prints "a"
System.out.println(sc.next()); // prints "bc"
If you need a bunch of char
from a Scanner
, then it may be more convenient to (perhaps temporarily) change the delimiter to the empty string. This will make next()
returns a length-1 string every time.
Scanner sc = new Scanner("abc");
sc.useDelimiter("");
while (sc.hasNext()) {
System.out.println(sc.next());
} // prints "a", "b", "c"
Is there a way to accept a single character as an input?
You can use System.in.read()
instead of Scanner
char input = (char) System.in.read();
You can also use Scanner
, doing something like:
Scanner scanner = new Scanner(System.in);
char input = scanner.next().charAt(0);
For using String
instead of char, you can also to convert to String
:
Scanner scanner = new Scanner(System.in);
String input = String.valueOf(input.next().charAt(0));
This is less fancy than other ways, but for a newbie, it'll be easier to understand. On the other hand, I think the problem proposed doesn't need amazing performance.
Reading a character input in the console using the Scanner Class in Java
This method Finds and returns the next complete token from this scanner. A complete token is preceded and followed by input that matches the delimiter pattern.
char ch = sc.next(".").charAt(0);
In this code it waiting to match this from this input "."
How the scannner input for character ( Operators ) work?
The problem is not because of s.nextLine()
; rather, it is because of s.nextInt()
. Check Scanner is skipping nextLine() after using next() or nextFoo()? to learn more about it.
Use
int a = Integer.parseInt(s.nextLine());
instead of
int a = s.nextInt();
I would recommend you create a utility function e.g. getInteger
as created in this answer to deal with exceptional scenarios as well.
Related Topics
How to Remove Objects from an Array in Java
Nullpointerexception When Creating an Array of Objects
How to Programmatically Compile and Instantiate a Java Class
Java 8 Lambda Function That Throws Exception
Java Process With Input/Output Stream
How to Add to List≪? Extends Number≫ Data Structures
What Is the Concept of Erasure in Generics in Java
What Is the Memory Consumption of an Object in Java
Questions About Java'S String Pool
Adding External Library to Artifact Jar in Intellij Idea
How to Get a Platform-Dependent New Line Character
Dependency Injection With Jersey 2.0
How Does a Java Hashmap Handle Different Objects With the Same Hash Code
How to Run a Java Program from the Command Line on Windows