Results 1 to 3 of 3
  1. #1
    mcmb03 is offline Member
    Join Date
    Jan 2010
    Posts
    2
    Rep Power
    0

    Post Read a single character entered

    Hi, I've only been doing java for a few days now so cut me a little slack :)

    I'm making a "guess the random number" game and at the end I want to have the user input if they'd like to play again with a simple 'y' / 'Y' or 'n' / 'N'

    If I could just be told at least how to input a character and then read it's ascii code I can figure it out. Thanks!

  2. #2
    tim's Avatar
    tim
    tim is offline Senior Member
    Join Date
    Dec 2007
    Posts
    435
    Rep Power
    7

    Default

    Hi there.

    This code will read and print a single character. The BufferedReader class has some more methods you might find interesting, including readLine().
    Java Code:
    import java.io.*;
    
    public class Main {
    	public static void main(String[] args) {
    		System.out.println(readCharacter());
    	}
    	
    	public static char readCharacter() {
    		try {
    			BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
    			return (char) reader.read();
    		} catch (Exception e) {
    			e.printStackTrace();
    		}
    		return 0;
    	}
    }
    Eyes dwelling into the past are blind to what lies in the future. Step carefully.

  3. #3
    mcmb03 is offline Member
    Join Date
    Jan 2010
    Posts
    2
    Rep Power
    0

Similar Threads

  1. Replies: 8
    Last Post: 09-15-2009, 11:53 AM
  2. Read from a certain character to a certain character
    By blackstormattack in forum New To Java
    Replies: 0
    Last Post: 03-16-2009, 11:36 AM
  3. Read character from Image area
    By sundarjothi in forum Advanced Java
    Replies: 2
    Last Post: 01-12-2009, 05:00 AM
  4. Replies: 5
    Last Post: 11-22-2008, 01:34 PM
  5. how to read single Keystroke without using return
    By oomanamemni in forum New To Java
    Replies: 1
    Last Post: 02-14-2008, 07:21 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •