| | |
Simple Scanner Class Question
Thread Solved
![]() |
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?
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?
This approach was probably unnecessary, but I took it anyways.
Try this--
Try this--
java Syntax (Toggle Plain Text)
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(); } }
java Syntax (Toggle Plain Text)
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); } } } }
Last edited by Alex Edwards; Sep 9th, 2008 at 3:49 pm.
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:
where yourName and yourInput are whatever you want them to be
use this instead:
java Syntax (Toggle Plain Text)
Scanner yourName = new Scanner(yourInput); char c = yourName.next().charAt(0);
My site, random PM's from people I haven't hear from before will be DELETED
"If people are good only because they fear punishment, and hope for reward, then we are a sorry lot indeed.",
"If we knew what it was we were doing, it would not be called research, would it? "-Albert Einstein
"If people are good only because they fear punishment, and hope for reward, then we are a sorry lot indeed.",
"If we knew what it was we were doing, it would not be called research, would it? "-Albert Einstein
•
•
•
•
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:
where yourName and yourInput are whatever you want them to bejava Syntax (Toggle Plain Text)
Scanner yourName = new Scanner(yourInput); char c = yourName.next().charAt(0);
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--
java Syntax (Toggle Plain Text)
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
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.
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.
"Any fool can write code that a computer can understand. Good programmers write code that humans can understand."
"How to ask questions the smart way ?"
"How to ask questions the smart way ?"
![]() |
Similar Threads
- Couple of errors in my code (Java)
- A Simple Question (Java)
- Summing Issue from Array (Java)
- A program with Menus and Loops...HELP!! (Java)
- Yellow flashing question mark/ "X".....GRRRRRRR (Viruses, Spyware and other Nasties)
- Simple algorithm question/needed (Java)
- Audio: Reading Internet "Playlist" and outputting audio (Java)
- Just a simple question about java (Java)
Other Threads in the Java Forum
- Previous Thread: i need help on stack. If pop: stack is empty.
- Next Thread: java Reports
| Thread Tools | Search this Thread |
911 actionlistener addball addressbook android applet application apps array automation binary bluetooth businessintelligence button card character class client code collision component consumer crashcourse css csv database desktop eclipse ee error fractal free ftp game givemetehcodez graphics gui html image integration j2me japplet java javaarraylist javac javadoc javaee javafx javaprojects jni jpanel julia jvm linked linux loan mac method migrate mobile netbeans objects online oriented phone physics printf problem program programming project projects radio recursion replaydirector reporting researchinmotion rotatetext rsa scanner se server service set sms software sort sql swing test textfield textfields threads time tree trolltech ubuntu update utility windows






