Tuesday, 9 October 2012

How to input a char in Java...

Original Code:
import java.lang.*;
import java.util.Scanner;

public class Program {
    public static void main(String[] args) {
        char cCharacter1,
            cCharacter2;
        Scanner scanner = new Scanner(System.in);
        System.out.print("Type in a character ");
        cCharacter1 = scanner.findWithinHorizon(".", 0).charAt(0);
        System.out.print("And now another one... ");
        cCharacter2 = scanner.findWithinHorizon(".", 0).charAt(0);
        System.out.print("The characters you typed are " + cCharacter1 + " and " + cCharacter2);
    }
}


Example of an interaction:
Type in a character m
And now another one... y
The characters you typed are m and y

No comments:

Post a Comment