Notices

Overclockers Forums > Software > Programming Tips and Tricks
Programming Tips and Tricks
Forum Jump

Reading a Single Character in Java

Post Reply New Thread Subscribe Search this Thread
 
 
Thread Tools
Old 10-23-06, 05:33 PM Thread Starter   #1
updawg
StarCraft II Fanatic

 
updawg's Avatar 

Join Date: May 2006
Location: NOVA

 
Reading a Single Character in Java


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
updawg is offline Heatware Profile   QUOTE Thanks
Old 10-23-06, 06:57 PM   #2
Trombe
Member

 
Trombe's Avatar 

Join Date: Mar 2005
Location: Austin, Texas

 
char whatEver = System.in.read();
Trombe is offline   QUOTE Thanks
Old 10-26-06, 09:56 PM Thread Starter   #3
updawg
StarCraft II Fanatic

 
updawg's Avatar 

Join Date: May 2006
Location: NOVA

 
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?
updawg is offline Heatware Profile   QUOTE Thanks
Old 10-26-06, 10:03 PM   #4
Midnight Dream
OSPF Loving Member

 
Midnight Dream's Avatar 

Join Date: Mar 2004
Location: Lawrenceville, Georgia

 
Code:
import java.util.Scanner;

...

char userInput;
Scanner scan = new Scanner (System.in);
userInput = scan.nextChar();

__________________
Gigabyte GA-EP43-UD3l - 6GB Patriot 6400 DDR2
Intel Core 2 Duo E7500 2.93GHz - XFX HD 6850 Black Edition

Learn to Fold - T32

A little bit of Heat


Still hate JoT

But I love you! And so does OSPF! Why do you turn us away!
Midnight Dream is offline   QUOTE Thanks
Old 10-26-06, 10:21 PM Thread Starter   #5
updawg
StarCraft II Fanatic

 
updawg's Avatar 

Join Date: May 2006
Location: NOVA

 
nextChar() isn't a method of the scanner class, that doesn't work.
updawg is offline Heatware Profile   QUOTE Thanks
Old 10-26-06, 10:38 PM   #6
Trombe
Member

 
Trombe's Avatar 

Join Date: Mar 2005
Location: Austin, Texas

 
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;
			}
		}
		*/
		
		
		
	}
	

}
Trombe is offline   QUOTE Thanks
Old 10-26-06, 10:52 PM Thread Starter   #7
updawg
StarCraft II Fanatic

 
updawg's Avatar 

Join Date: May 2006
Location: NOVA

 
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();
updawg is offline Heatware Profile   QUOTE Thanks
Old 10-26-06, 11:02 PM Thread Starter   #8
updawg
StarCraft II Fanatic

 
updawg's Avatar 

Join Date: May 2006
Location: NOVA

 
Thanks for you help.
updawg is offline Heatware Profile   QUOTE Thanks
Old 05-28-12, 07:04 PM   #9
galatic
New Member



Join Date: May 2012

 
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
galatic is offline   QUOTE Thanks
Old 06-16-12, 02:33 PM   #10
johan851
Insatiably Malcontent
Senior Member

 
johan851's Avatar 

Join Date: Jul 2002
Location: Seattle, WA

10 Year Badge
 
Quote:
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.

__________________
ASRock Z68 Extreme3 Gen3 | 2500K @ 4.6GHz | 2x4GB Samsung DDR3 | GTX460 768mb
120GB Crucial M4 | 2x 2TB Samsung F4 | Seasonic S12 600w
AV-710 --> y2 DAC --> Custom M^3 --> Custom LM3875 ChipAmp --> Modula MTs
Dual Dell 2007WFP | Watercooled
johan851 is offline   QUOTE Thanks
Old 03-20-13, 01:14 AM   #11
Abdul Rehman
New Member



Join Date: Mar 2013

 
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?
Abdul Rehman is offline   QUOTE Thanks
Old 03-20-13, 10:40 AM   #12
johan851
Insatiably Malcontent
Senior Member

 
johan851's Avatar 

Join Date: Jul 2002
Location: Seattle, WA

10 Year Badge
 
You can use substring() to retrieve a portion of the string.

__________________
ASRock Z68 Extreme3 Gen3 | 2500K @ 4.6GHz | 2x4GB Samsung DDR3 | GTX460 768mb
120GB Crucial M4 | 2x 2TB Samsung F4 | Seasonic S12 600w
AV-710 --> y2 DAC --> Custom M^3 --> Custom LM3875 ChipAmp --> Modula MTs
Dual Dell 2007WFP | Watercooled
johan851 is offline   QUOTE Thanks

Post Reply New Thread Subscribe


Overclockers Forums > Software > Programming Tips and Tricks
Programming Tips and Tricks
Forum Jump

Thread Tools Search this Thread
Search this Thread:

Advanced Search


Mobile Skin
All times are GMT -5. The time now is 05:09 PM.
Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2013, vBulletin Solutions, Inc.
You can add these icons by updating your profile information to include your Heatware ID, Benching Profile ID or your Folding/SETI profile ID. Edit your profile!
X

Welcome to Overclockers.com

Create your username to jump into the discussion!

New members like you have made this the best community on the Internet since 1998!


(4 digit year)

Why Join Us?

  • Share experience
  • Max out your hardware
  • Best forum members anywhere
  • Customized forum experience

Already a member?