943,898 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Marked Solved
  • Views: 3755
  • Java RSS
Sep 9th, 2008
0

Simple Scanner Class Question

Expand Post »
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?
Similar Threads
Reputation Points: 104
Solved Threads: 27
Posting Whiz in Training
dmanw100 is offline Offline
239 posts
since Apr 2008
Sep 9th, 2008
0

Re: Simple Scanner Class Question

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

Try this--

java Syntax (Toggle Plain Text)
  1. import java.io.*;
  2.  
  3. public class DataInputFilterStream extends FilterInputStream{
  4.  
  5. public DataInputFilterStream(InputStream is){
  6. super(new DataInputStream(is));
  7. }
  8.  
  9. @Override public int read() throws IOException{
  10. skip(available());
  11. return (available() == 0) ? super.read() : 0;
  12. }
  13.  
  14. public char readChar() throws IOException{
  15. return (char)read();
  16. }
  17. }

java Syntax (Toggle Plain Text)
  1. import java.io.*;
  2.  
  3. public class CharScanner{
  4.  
  5.  
  6. public static void main(String... args){
  7. DataInputFilterStream difs = null;
  8. try{
  9. difs = new DataInputFilterStream(System.in);
  10. }catch(Throwable t){
  11. System.out.println("Unable to initialized object dis - shutting down.");
  12. t.printStackTrace();
  13. System.exit(1);
  14. }
  15. boolean decision = true;
  16. do{
  17. try{
  18. System.out.println("Enter a char--");
  19. System.out.println("You enterred: " + difs.readChar());
  20. System.out.println("To Continue enter the character y");
  21. char userInput = difs.readChar();
  22. decision = ((userInput == 'Y') || (userInput == 'y'));
  23. }catch(Throwable t){
  24. }
  25. }while(decision);
  26.  
  27. try{
  28. difs.close();
  29. }catch(Exception e){
  30. try{
  31. }finally{
  32. System.out.println("An error occurred during stream closing - exiting program--");
  33. System.exit(1);
  34. }
  35. }
  36. }
  37. }
Last edited by Alex Edwards; Sep 9th, 2008 at 3:49 pm.
Reputation Points: 392
Solved Threads: 108
Posting Shark
Alex Edwards is offline Offline
971 posts
since Jun 2008
Sep 9th, 2008
0

Re: Simple Scanner Class Question

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:

java Syntax (Toggle Plain Text)
  1. Scanner yourName = new Scanner(yourInput);
  2. char c = yourName.next().charAt(0);
where yourName and yourInput are whatever you want them to be
Reputation Points: 73
Solved Threads: 22
Posting Pro in Training
sciwizeh is offline Offline
423 posts
since Jun 2008
Sep 9th, 2008
0

Re: Simple Scanner Class Question

Click to Expand / Collapse  Quote originally posted by sciwizeh ...
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:

java Syntax (Toggle Plain Text)
  1. Scanner yourName = new Scanner(yourInput);
  2. 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--

java Syntax (Toggle Plain Text)
  1. import java.io.*;
  2. import java.util.*;
  3.  
  4. public class DriverProgram_64{
  5.  
  6. public static void main(String... args){
  7. BufferedReader br = null;
  8. Scanner kb = null;
  9. try{
  10. br =
  11. new BufferedReader(
  12. new InputStreamReader(
  13. new FileInputStream(
  14. new File("Test_Text.txt")
  15. )
  16. )
  17. );
  18.  
  19. kb = new Scanner(
  20. new FileInputStream(
  21. new File("Test_Text.txt")
  22. )
  23. );
  24.  
  25. System.out.println("BufferedReader test:");
  26. while(br.ready()){
  27. System.out.println((char)br.read());
  28. }
  29.  
  30. System.out.println("\n\n\nScanner test: ");
  31. while(kb.hasNext()){
  32. System.out.println(kb.next().charAt(0));
  33. }
  34. }catch(Exception e){}
  35. finally{
  36. try{
  37. br.close();
  38. kb.close();
  39. }catch(Exception e){
  40. System.exit(1);
  41. }
  42. }
  43. }
  44. }

--with the attached .txt file
Attached Files
File Type: txt Test_Text.txt (106 Bytes, 34 views)
Reputation Points: 392
Solved Threads: 108
Posting Shark
Alex Edwards is offline Offline
971 posts
since Jun 2008
Sep 9th, 2008
0

Re: Simple Scanner Class Question

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!
Reputation Points: 104
Solved Threads: 27
Posting Whiz in Training
dmanw100 is offline Offline
239 posts
since Apr 2008
Jan 16th, 2009
-2

Re: Simple Scanner Class Question

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

Re: Simple Scanner Class Question

Click to Expand / Collapse  Quote originally posted by nilima ...
Would any one give idea About How to attach scanner through VB6
May I suggest the VB forum
Sponsor
Featured Poster
Reputation Points: 1014
Solved Threads: 446
Nearly a Senior Poster
javaAddict is offline Offline
3,259 posts
since Dec 2007
Jan 16th, 2009
0

Re: Simple Scanner Class Question

ok thanx
Reputation Points: 8
Solved Threads: 0
Newbie Poster
nilima is offline Offline
3 posts
since Jan 2009
Jan 16th, 2009
0

Re: Simple Scanner Class Question

but can u give me actual site address where i have to search because as i know this is also a vb forum group
Reputation Points: 8
Solved Threads: 0
Newbie Poster
nilima is offline Offline
3 posts
since Jan 2009
Jan 16th, 2009
0

Re: Simple Scanner Class Question

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.
Featured Poster
Reputation Points: 653
Solved Threads: 151
Nearly a Posting Virtuoso
stephen84s is offline Offline
1,316 posts
since Jul 2007

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: i need help on stack. If pop: stack is empty.
Next Thread in Java Forum Timeline: java Reports





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC