• Welcome to Overclockers Forums! Join us to reply in threads, receive reduced ads, and to customize your site experience!

Reading a Single Character in Java

Overclockers is supported by our readers. When you click a link to make a purchase, we may earn a commission. Learn More.

updawg

StarCraft II Fanatic
Joined
May 19, 2006
Location
NOVA
I need help in writing a program that intakes a single character "P or R" and then does something according to the input character. What is the method for reading a single character into memory.

I thought it was console.next() and I tried character buffer and string buffer and a bunch of other things, any help would be extremly helpful to me!

Thanks,
Mike
 
char whatEver = System.in.read(); returns a possible loss of precision error.

However, the following lines worked for me:

String whatEver = console.next();
char blahBlah = whatEver.charAt(0);

Is there an easier way to take in just a single character? Did I miss something in the way you suggested?
 
nextChar() isn't a method of the scanner class, that doesn't work.
 
Hopefully this will shed some light on the differences and possibilities:

Code:
import java.util.Scanner;

public class InChar
{
	public static void main(String args[])
	{
		// System.in.Read Version
		System.out.printf("System.in.Read Version..\nEnter Char ==> ");
		try
		{
			char temp = (char)System.in.read();
			System.out.printf("\nYou Entered: " + temp + "\n");
		}catch(Exception exe)
		{ exe.printStackTrace(); }
		
		// Scanner Version
		System.out.printf("\n\nScanner Version..\nEnter Char ==> "); 
		Scanner kb = new Scanner(System.in);
		String tString = kb.next();
		char temp2 = tString.charAt(0);
		System.out.printf("\nYou Entered: " + temp2 + "\n");
		
		// Don't wait for enter key Version
		/*
		public static void waitForCont()
		{
			out.printf("\nPress C to Continue...\n");
			boolean pressed = false;
			String entered = "";
			while(!pressed)
			{
				entered=kb.next();
				if((entered.equals("C"))||entered.equals("c"))
					pressed=true;
			}
		}
		*/
		
		
		
	}
	

}
 
So all I really had to do was put (char) before System.in.read() ?

and just use public static void main(String[] args) throws java.io.IOException


why wouldn't they have nextChar? But you can use (char)System.in.read();
 
As you said early there isn't a method in the class Scanner that can read a single character but...

You know that to read a String you do:
String read=in.nextLine();

To read the first character of that string you do:
char reading=read.charAt(0);



But if you want to read a single character without using a string you can do this:
char read=in.nextLine().charAt(0);

read as it was a string but it will convert to a char automatically
 
why wouldn't they have nextChar? But you can use (char)System.in.read();
The reason is that the Scanner class is designed for reading in whitespace-separated tokens. It's a convenience class that wraps an underlying input stream. Before scanner all you could do was read in single bytes, and that's a big pain if you want to read words or lines. With Scanner you pass in System.in, and it does a number of read() operations to tokenize the input for you. Reading a single character is a more basic operation.
 
I'm new in Java.can you tell me that how can i get more than one character in java. because i have get characters more than one in c++ but how can i implement this in java?
 
Back