Cara menggunakan INPUT.NEXTCHAR pada JavaScript

How to add Java Scanner char support

The Java Scanner class provides methods that take String input and convert that String into any Java primitive type you might need, except for one: the char.

The Java Scanner class provides the following self-explanatory methods:

  • nextInt()
  • nextByte()
  • nextBoolean()
  • nextFloat()
  • nextDouble()
  • nextLong()
  • nextShort()

But the one method it doesn’t have is nextChar().

Implement a Java Scanner nextChar method

Naturally, all Java developers, after learning the Java Scanner class has no nextChar method, want to implement one of their own. It’s not that hard.

By default, the Scanner class reads a line of input from a user and tokenizes the contents based on whitespaces found in the input.

useDelimiter(Pattern pattern)

Sets this scanner’s delimiting pattern to the specified pattern.

This allows a developer to loop through input one text String at a time.

But you don’t have to accept the Scanner’s defaults. The Scanner class’ useDelimiter method allows you to change the delimiter from a blank space to any character you want.

Simple Java user input made approaches

There are many ways to take input from the user and format the output in Java.

  • Top 3 Java user input strategies
  • How to import the Java Scanner class
  • A simple Java Scanner String input example
  • Use the Java Scanner for user input
  • How to format output with Java printf

Scanner input one char at a time

The trick is to change the delimiter to nothing.

Just set the delimiter to two double-quotes with nothing inside: "". The empty quotes make the Scanner chunk out the line one char at a time.

At this point, the Scanner still returns a String, although the String contains only one character. To complete the use case, you must convert this one-character String into a single Java char with the charAt(0) method.

Java Scanner char input code example

The code to get Scanner input one char at a time looks like this:

import java.util.Scanner;
public class NextCharScanner{
  
  // implement a nextChar Scanner method
  public static void main(String[] args) {
    System.out.println("Input char data to the Java Scanner: "); 
    Scanner charScanner = new Scanner(System.in); 
    charScanner.useDelimiter("");
    while (charScanner.hasNext()) {
      char name = charScanner.next().charAt(0);
      if (name == '\n') {
        return;
      }
    }
  }
}

Notice that the code has a check for the newline character, as this will be treated as an input character if not filtered out.

How to read the next char with Java’s Scanner

To summarize the process, follow these steps to read user input with the Java Scanner one char at a time:

  1. Read a line of text with the Scanner as you normally would do.
  2. Change the Scanner’s delimiter to an empty set of double quotes, "".
  3. Convert each single-character to a char with the charAt(0) method.
  4. Filter out any special characters such as EOL of EOF.

And that’s how easy it is to create a char input Scanner method in Java.

This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters

package pertemuan2;
// mengimpor Scanner ke program
import java.util.Scanner;
public class DataKaryawan {
public static void main(String[] args) {
// deklarasi variabel
String nama, alamat;
int usia, gaji;
// membuat scanner baru
Scanner keyboard = new Scanner(System.in);
// Tampilkan output ke user
System.out.println("### Pendataan Karyawan PT. Petani Kode ###");
System.out.print("Nama karyawan: ");
// menggunakan scanner dan menyimpan apa yang diketik di variabel nama
nama = keyboard.next();
// Tampilkan outpu lagi
System.out.print("Alamat: ");
// menggunakan scanner lagi
alamat = keyboard.next();
System.out.print("Usia: ");
usia = keyboard.nextInt();
System.out.print("Gaji: ");
gaji = keyboard.nextInt();
// Menampilkan apa yang sudah simpan di variabel
System.out.println("--------------------");
System.out.println("Nama Karyawan: " + nama);
System.out.println("Alamat: " + alamat);
System.out.println("Usia: " + usia + " tahun");
System.out.println("Gaji: Rp " + gaji);
}
}