Simple Scanner Class Question

Thread Solved

Join Date: Apr 2008
Posts: 160
Reputation: dmanw100 is on a distinguished road 
Solved Threads: 12
dmanw100's Avatar
dmanw100 dmanw100 is offline Offline
Junior Poster

Simple Scanner Class Question

 
0
  #1
Sep 9th, 2008
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?
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 973
Reputation: Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough 
Solved Threads: 107
Alex Edwards's Avatar
Alex Edwards Alex Edwards is offline Offline
Posting Shark

Re: Simple Scanner Class Question

 
0
  #2
Sep 9th, 2008
This approach was probably unnecessary, but I took it anyways.

Try this--

  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. }

  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.
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 408
Reputation: sciwizeh is on a distinguished road 
Solved Threads: 22
sciwizeh's Avatar
sciwizeh sciwizeh is offline Offline
Posting Pro in Training

Re: Simple Scanner Class Question

 
0
  #3
Sep 9th, 2008
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:

  1. Scanner yourName = new Scanner(yourInput);
  2. char c = yourName.next().charAt(0);
where yourName and yourInput are whatever you want them to be
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
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 973
Reputation: Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough 
Solved Threads: 107
Alex Edwards's Avatar
Alex Edwards Alex Edwards is offline Offline
Posting Shark

Re: Simple Scanner Class Question

 
0
  #4
Sep 9th, 2008
Originally Posted by sciwizeh View Post
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:

  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--

  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, 4 views)
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 160
Reputation: dmanw100 is on a distinguished road 
Solved Threads: 12
dmanw100's Avatar
dmanw100 dmanw100 is offline Offline
Junior Poster

Re: Simple Scanner Class Question

 
0
  #5
Sep 9th, 2008
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!
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 3
Reputation: nilima is an unknown quantity at this point 
Solved Threads: 0
nilima nilima is offline Offline
Newbie Poster

Re: Simple Scanner Class Question

 
-2
  #6
Jan 16th, 2009
Would any one give idea About How to attach scanner through VB6
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 1,653
Reputation: javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all 
Solved Threads: 223
Featured Poster
javaAddict's Avatar
javaAddict javaAddict is online now Online
Posting Virtuoso

Re: Simple Scanner Class Question

 
0
  #7
Jan 16th, 2009
Originally Posted by nilima View Post
Would any one give idea About How to attach scanner through VB6
May I suggest the VB forum
Check out my New Bike at my Public Profile at the "About Me" tab
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 3
Reputation: nilima is an unknown quantity at this point 
Solved Threads: 0
nilima nilima is offline Offline
Newbie Poster

Re: Simple Scanner Class Question

 
0
  #8
Jan 16th, 2009
ok thanx
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 3
Reputation: nilima is an unknown quantity at this point 
Solved Threads: 0
nilima nilima is offline Offline
Newbie Poster

Re: Simple Scanner Class Question

 
0
  #9
Jan 16th, 2009
but can u give me actual site address where i have to search because as i know this is also a vb forum group
Reply With Quote Quick reply to this message  
Join Date: Jul 2007
Posts: 1,175
Reputation: stephen84s is a glorious beacon of light stephen84s is a glorious beacon of light stephen84s is a glorious beacon of light stephen84s is a glorious beacon of light stephen84s is a glorious beacon of light stephen84s is a glorious beacon of light 
Solved Threads: 125
Featured Poster
stephen84s's Avatar
stephen84s stephen84s is offline Offline
Veteran Poster

Re: Simple Scanner Class Question

 
0
  #10
Jan 16th, 2009
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.
"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 ?"
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC