How Do You Write a Java Program to Accept a Character as Input and Do Something with It?

Problem scenario
You want to accept user input with a Java program. How do you write such a program?

Solution
Here is the program:

import java.util.Scanner;

public class acceptChar {

public static void main(String[] args) {
   System.out.println("Please enter a character, then press enter: ");
   Scanner in = new Scanner(System.in);
   char letter = in.next().charAt(0);
   System.out.println(letter);
  }
 }

Leave a comment

Your email address will not be published. Required fields are marked *