Search...
FAQs
Subscribe
Pie
FAQs
Recent topics
Flagged topics
Hot topics
Best topics
Search...
Search within OCPJP
Search Coderanch
Advance search
Google search
Register / Login
Post Reply
Bookmark Topic
Watch Topic
New Topic
programming forums
Java
Mobile
Certification
Databases
Caching
Books
Engineering
Micro Controllers
OS
Languages
Paradigms
IDEs
Build Tools
Frameworks
Application Servers
Open Source
This Site
Careers
Other
Pie Elite
all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Campbell Ritchie
Jeanne Boyarsky
Ron McLeod
Paul Clapham
Liutauras Vilda
Sheriffs:
paul wheaton
Rob Spoor
Devaka Cooray
Saloon Keepers:
Stephan van Hulst
Tim Holloway
Carey Brown
Frits Walraven
Tim Moores
Bartenders:
Mikalai Zaikin
Forum:
Programmer Certification (OCPJP)
stream tokenizer readToken() method or nextToken()
rohit kumar
Greenhorn
Posts: 9
posted 16 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
import java.io.StreamTokenizer; import java.io.InputStreamReader; import java.io.BufferedReader; import java.io.IOException; public class TestFormattedInput { public static void main(String[] args){ FormattedInput kb = new FormattedInput(); for(int i=0;i<5;i++){ try{ System.out.println("Enter an integer: "); System.out.print("Integer read: "+kb.readInt()); System.out.println("Enter a double value: "); System.out.print("Double read: "+kb.readDouble()); System.out.println("Enter a String value: "); System.out.print("String read: "+kb.readString()); }catch(InvalidUserInputException e){ System.out.println("Invalid User exception thrown.\n"+e.getMessage()); } } } } class InvalidUserInputException extends Exception{ public InvalidUserInputException(){ } public InvalidUserInputException(String msg){ super(msg); } } class FormattedInput { private int ttype; private StreamTokenizer tokenizer = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in))); public int readInt()throws InvalidUserInputException{ if(tokenizer.nextToken() != tokenizer.TT_NUMBER){ throw new InvalidUserInputException("readInt() failed"+" user imput not numeric."); } return (int)tokenizer.nval; } public double readDouble() throws InvalidUserInputException{ if(tokenizer.nextToken() != tokenizer.TT_NUMBER){ throw new InvalidUserInputException("readDouble() failed"+" user input not numeric."); } return tokenizer.nval; } public String readString() throws InvalidUserInputException{ if(tokenizer.nextToken()== tokenizer.TT_WORD||ttype == "*/"||ttype == "/*"){ return tokenizer.sval; }else{ throw new InvalidUserInputException("readString() failed"+" user input not String."); } } }
Hi ranchers I am getting error like nextToken() method providing incompatible types while working with TT_WORD - even readToken() method in place of tokenizer.nextToken() dont seem to work as well. What's the way out?
Keith Lynn
Ranch Hand
Posts: 2412
posted 16 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
You might find the Scanner class easier to use.
Manfred Klug
Ranch Hand
Posts: 377
posted 16 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Hi rohit,
I think you would have better chances for a response, if you make a
short example
that demonstrates your problem.
With a little knowledge, a
cast iron skillet
is non-stick and lasts a lifetime.
reply
reply
Bookmark Topic
Watch Topic
New Topic
Boost this thread!
Similar Threads
Can't figure out why variable isn't recognized
endless loop
StackOverflowError
IO
Issue with StreamTokenizer?
More...