Expand Authors Top

If you have a few years of experience in the Java ecosystem and you’d like to share that with the community, have a look at our Contribution Guidelines.

Expanded Audience – Frontegg – Security (partner)
announcement - icon User management is very complex, when implemented properly. No surprise here.

Not having to roll all of that out manually, but instead integrating a mature, fully-fledged solution - yeah, that makes a lot of sense.
That's basically what Frontegg is - User Management for your application. It's focused on making your app scalable, secure and enjoyable for your users.
From signup to authentication, it supports simple scenarios all the way to complex and custom application logic.

Have a look:

>> Elegant User Management, Tailor-made for B2B SaaS

Java Top

Get started with Spring 5 and Spring Boot 2, through the Learn Spring course:

> CHECK OUT THE COURSE

1. Overview

In this tutorial, we'll see how to take character input from the Scanner class.

2. Scan a Character

Java Scanner doesn't provide any method for taking character input similar to nextInt(), nextLine(), etc.

There are a few ways we can take character input using Scanner.

Let's first create an input string:

String input = new StringBuilder().append("abc\n")
  .append("mno\n")
  .append("xyz\n")
  .toString();

3. Using next()

Let's see how we can use the next() method of Scanner, and the charAt() method of String class to take a character as input:

@Test
public void givenInputSource_whenScanCharUsingNext_thenOneCharIsRead() {
    Scanner sc = new Scanner(input);
    char c = sc.next().charAt(0);
    assertEquals('a', c);
}

The next() method of Java Scanner returns a String object. We are using the charAt() method of the String class here to fetch the character from the string object.

4. Using findInLine()

This method takes a string pattern as input, and we'll pass “.” (dot) to match only a single character. However, this will return a single character as a string, so we'll use charAt() method to get the character:

@Test
public void givenInputSource_whenScanCharUsingFindInLine_thenOneCharIsRead() {
    Scanner sc = new Scanner(input);
    char c = sc.findInLine(".").charAt(0);
    assertEquals('a', c);
}

5. Using useDelimeter()

This method also scans only one character but as an object of string similar to findInLine() API. We can similarly use the charAt() method to get the character value:

@Test
public void givenInputSource_whenScanCharUsingUseDelimiter_thenOneCharIsRead() {
    Scanner sc = new Scanner(input);
    char c = sc.useDelimiter("").next().charAt(0);
    assertEquals('a', c);
}

6. Conclusion

In this tutorial, we've learned how to take char input using the Java Scanner.

As always, the complete source code for the examples is available over on GitHub.

Java bottom

Get started with Spring 5 and Spring Boot 2, through the Learn Spring course:

>> CHECK OUT THE COURSE
Generic footer banner
Comments are closed on this article!