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?

Recommended Answers

All 9 Replies

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);
			}
		}
	}
}

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

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

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!

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

commented: Start your own thread. -1
commented: start your own thread at the VB forum -1

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

May I suggest the VB forum

ok thanx

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

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.