Massassi Forums Logo

This is the static archive of the Massassi Forums. The forums are closed indefinitely. Thanks for all the memories!

You can also download Super Old Archived Message Boards from when Massassi first started.

"View" counts are as of the day the forums were archived, and will no longer increase.

ForumsDiscussion Forum → Java Kwestion
Java Kwestion
2007-10-24, 1:38 PM #1
How do I use a Scanner method to read a character? I know how to use scan.nextInt to read an integer and scan.nextLine() etc, but what if I only want the user to enter a single character?

2007-10-24, 1:52 PM #2
http://java.sun.com/j2se/1.3/docs/api/java/io/BufferedReader.html
2007-10-24, 1:54 PM #3
You could also read a byte and cast it to a char.
Bassoon, n. A brazen instrument into which a fool blows out his brains.
2007-10-24, 2:14 PM #4
Originally posted by Emon:
You could also read a byte and cast it to a char.


That gives me a possible loss of precision error.

2007-10-24, 2:18 PM #5
Then use a BufferedReader, since casting to a char is a bad idea anyway.

If you want to continue to use the Scanner for other things, try reading in one line and then using the BufferedReader on that.
Bassoon, n. A brazen instrument into which a fool blows out his brains.
2007-10-24, 2:31 PM #6
Or use any character as the termination character, then just read the first character of the resultant string using charAt(0).

Loads of ways of doing it really.
Detty. Professional Expert.
Flickr Twitter
2007-10-24, 2:50 PM #7
You can cast ints as chars.
2007-10-24, 9:33 PM #8
Code:
using System.IO

public void Func()
{
	using (TextReader tr = new StreamReader(@"c:\temp\text.txt")
	{
		int wChar;
		do
		{
			wChar=tr.Read();
			Console.Write(string.Format(" {0} ",Convert.ToChar(wChar)));
		}
		while (wChar != -1)
	}
}

Aw ****. Wrong language. My bad.
Code to the left of him, code to the right of him, code in front of him compil'd and thundered. Programm'd at with shot and $SHELL. Boldly he typed and well. Into the jaws of C. Into the mouth of PERL. Debug'd the 0x258.
2007-10-24, 9:36 PM #9
Hahha, jg. It looks damn close enough tho.
"it is time to get a credit card to complete my financial independance" — Tibby, Aug. 2009
2007-10-24, 10:35 PM #10
I thought this was about coffee and it's relation to star wars.
Epstein didn't kill himself.
2007-10-24, 11:56 PM #11
Code:
import java.io.*;
import java.util.*;

class Test
{
	public static void main(String[]args)
	{
		try
		{
			Scanner s=new Scanner(new File(args[0]));
			System.out.printf("%c%n",s.nextLine().charAt(0));
		}
		catch(FileNotFoundException e)
		{
			System.err.printf("File not found, lol.%n");
			System.exit(-1);
		}
		catch(NoSuchElementException e)
		{
			System.err.printf("File is empty, lol.%n");
			System.exit(-1);
		}
	}
}


Detty's version.
"it is time to get a credit card to complete my financial independance" — Tibby, Aug. 2009
2007-10-25, 12:20 AM #12
Yeah I though it said Jawa.
2007-10-25, 12:35 AM #13
Originally posted by BombayZeus:
How do I use a Sc
use a better language :mad:
2007-10-25, 12:40 AM #14
like python? :awesome:
"it is time to get a credit card to complete my financial independance" — Tibby, Aug. 2009
2007-10-25, 8:12 AM #15
Originally posted by Freelancer:

That's better.
Code to the left of him, code to the right of him, code in front of him compil'd and thundered. Programm'd at with shot and $SHELL. Boldly he typed and well. Into the jaws of C. Into the mouth of PERL. Debug'd the 0x258.
2007-10-25, 8:23 AM #16
I wonder if JediGandalf is actually one of those people who's employed to promote products on forums.
Detty. Professional Expert.
Flickr Twitter
2007-10-25, 6:56 PM #17
Originally posted by Detty:
I wonder if JediGandalf is actually one of those people who's employed to promote products on forums.

I am actually. I got :10bux: from it.
Code to the left of him, code to the right of him, code in front of him compil'd and thundered. Programm'd at with shot and $SHELL. Boldly he typed and well. Into the jaws of C. Into the mouth of PERL. Debug'd the 0x258.
2007-10-25, 10:38 PM #18
doesn't have to be complicated, here's one based on what Detty said about charAt.

Code:
Scanner scan = new Scanner(whatever_you're_scanning_from);
Char ch;
String line = scan.nextLine();
if(line.length() != 1)
{
   // If you want to give an error for having more than one char, put that code here
}
else
   ch = line.charAt(0);


If you don't care whether the user types more than one letter (and you only want to read the first no matter what) then you don't even need the if/else. Just say ch=line.charAt(0);
Sam: "Sir we can't call it 'The Enterprise'"
Jack: "Why not!"
2007-10-26, 2:52 AM #19
by making the terminator string a regex that matches any character you don't need the if/else, the input will end after you type the first character.
Detty. Professional Expert.
Flickr Twitter
2007-10-27, 2:45 AM #20
But would that give you the option to display some sort of error message?

(just a note, I've never used regex before, so this may or may not have been a stupid question. Sorry if it is ;))
Sam: "Sir we can't call it 'The Enterprise'"
Jack: "Why not!"
2007-10-28, 4:04 PM #21
Yes it would. You just have to catch whatever exceptions you need to manually.
"it is time to get a credit card to complete my financial independance" — Tibby, Aug. 2009

↑ Up to the top!