954,536 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Simple Scanner Class Question

I'm a computer science major with a background in C++, not Java. My professor told the class that the Scanner class has a nextInt(), nextDouble(), nextFloat(), etc for every primative type. But whenever I try to use nextChar(), the compiler gives me an "undefined symbol" error. That means that there is no nextChar() method. Any ideas what my professor meant?

I used nextLine() in my code so I could compare a single character with the users input (ex. 'Y', 'N') but is there a better way?

dmanw100
Posting Whiz in Training
242 posts since Apr 2008
Reputation Points: 104
Solved Threads: 27
 

This approach was probably unnecessary, but I took it anyways.

Try this--

import java.io.*;

public class DataInputFilterStream extends FilterInputStream{

	public DataInputFilterStream(InputStream is){
		super(new DataInputStream(is));
	}

	@Override public int read() throws IOException{
		skip(available());
		return (available() == 0) ? super.read() : 0;
	}

	public char readChar() throws IOException{
		return (char)read();
	}
}
import java.io.*;

public class CharScanner{


	public static void main(String... args){
		DataInputFilterStream difs = null;
		try{
			difs = new DataInputFilterStream(System.in);
		}catch(Throwable t){
			System.out.println("Unable to initialized object dis - shutting down.");
			t.printStackTrace();
			System.exit(1);
		}
		boolean decision = true;
		do{
			try{
				System.out.println("Enter a char--");
				System.out.println("You enterred: " + difs.readChar());
				System.out.println("To Continue enter the character y");
				char userInput = difs.readChar();
				decision = ((userInput == 'Y') || (userInput == 'y'));
			}catch(Throwable t){
			}
		}while(decision);

		try{
			difs.close();
		}catch(Exception e){
			try{
			}finally{
				System.out.println("An error occurred during stream closing - exiting program--");
				System.exit(1);
			}
		}
	}
}
Alex Edwards
Posting Shark
972 posts since Jun 2008
Reputation Points: 392
Solved Threads: 109
 

your professor is correct except for one thing, Scanner Doesn't have a nextChar method, i believe it is the only primitive not supported.

use this instead:

Scanner yourName = new Scanner(yourInput);
char c = yourName.next().charAt(0);

where yourName and yourInput are whatever you want them to be

sciwizeh
Posting Pro in Training
457 posts since Jun 2008
Reputation Points: 77
Solved Threads: 23
 

your professor is correct except for one thing, Scanner Doesn't have a nextChar method, i believe it is the only primitive not supported.

use this instead:

Scanner yourName = new Scanner(yourInput);
char c = yourName.next().charAt(0);

where yourName and yourInput are whatever you want them to be

Yes, this version is much easier to understand.

Also the DataInputFilterStream is flawed and will only work for the InputStream provided by the System class, and does not work well with files.

The reason why I don't like the Scanner class very much, and felt the need to make my own is due to this test (and many others regarding Scanner), below--

import java.io.*;
import java.util.*;

public class DriverProgram_64{

	public static void main(String... args){
		BufferedReader br = null;
		Scanner kb = null;
		try{
			br =
				new BufferedReader(
					new InputStreamReader(
						new FileInputStream(
							new File("Test_Text.txt")
					)
				)
			);

			kb = new Scanner(
							new FileInputStream(
								new File("Test_Text.txt")
							)
			);

			System.out.println("BufferedReader test:");
			while(br.ready()){
				System.out.println((char)br.read());
			}

			System.out.println("\n\n\nScanner test: ");
			while(kb.hasNext()){
				System.out.println(kb.next().charAt(0));
			}
		}catch(Exception e){}
		finally{
			try{
				br.close();
				kb.close();
			}catch(Exception e){
				System.exit(1);
			}
		}
	}
}


--with the attached .txt file

Attachments Test_Text.txt (0.1KB)
Alex Edwards
Posting Shark
972 posts since Jun 2008
Reputation Points: 392
Solved Threads: 109
 

Thanks for your help! My first project is coming along very nicely now. I'm using a String to compare input with acceptable values. Thanks!

dmanw100
Posting Whiz in Training
242 posts since Apr 2008
Reputation Points: 104
Solved Threads: 27
 

Would any one give idea About How to attach scanner through VB6

nilima
Newbie Poster
3 posts since Jan 2009
Reputation Points: 8
Solved Threads: 0
 
Would any one give idea About How to attach scanner through VB6

May I suggest the VB forum

javaAddict
Nearly a Senior Poster
Team Colleague
3,329 posts since Dec 2007
Reputation Points: 1,014
Solved Threads: 448
 

ok thanx

nilima
Newbie Poster
3 posts since Jan 2009
Reputation Points: 8
Solved Threads: 0
 

but can u give me actual site address where i have to search because as i know this is also a vb forum group

nilima
Newbie Poster
3 posts since Jan 2009
Reputation Points: 8
Solved Threads: 0
 

This is a Java Forum, but I guess you are too ignorant to notice that.
There is also a VB forum on this site, but since you are too lazy to even see where it is, here is its link.

stephen84s
Nearly a Posting Virtuoso
1,443 posts since Jul 2007
Reputation Points: 668
Solved Threads: 154
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You