Results 1 to 9 of 9
Thread: scanner question
- 10-12-2009, 09:57 AM #1
Member
- Join Date
- Oct 2009
- Posts
- 14
- Rep Power
- 0
scanner question
This is what I have currently
I want to make it so that scanner will ignore (String text)'s '[' and ']' and ','.Java Code:public Polynomial(String text) throws IllegalArgumentException { ... Scanner input = new Scanner(text); int count = 0; IntList number = new IntList(count); while(input.hasNext()){ if(input.hasNextInt()){ number.add(input.nextInt()); //System.out.print(input.next()); //count++; } else { throw new IllegalArgumentException("input string is not integer"); } } coefficients = new IntList(count); for(int i = number.size(); i>=0; i--) { coefficients.add(number.get(i)); } }
How would I do that?
thank you in advance
- 10-12-2009, 10:02 AM #2
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
Your else part should read the next token and check if the stuff to be ignored or not.
- 10-12-2009, 10:40 AM #3
Member
- Join Date
- Oct 2009
- Posts
- 14
- Rep Power
- 0
but I don't want it to throw exception when it comes across '[' ']' and ','. Is there some code that scanner could just skip them?
- 10-12-2009, 10:46 AM #4
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
Like I said, in the else part, read the next char, if the char is any of those chars then do nothing (ignore) otherwise throw the SillyLookingDataFoundException.
- 10-12-2009, 10:57 AM #5
Member
- Join Date
- Oct 2009
- Posts
- 14
- Rep Power
- 0
is this what you meant?
but this is giving me errorJava Code:while(input.hasNext()){ if(input.hasNextInt()){ //number.add(input.nextInt()); System.out.print(input.next()); count++; } else { if(input.hasNextString() == "[" ll "]" ll ","){ } else { throw new IllegalArgumentException("input string is not integer"); } } }
if(input.hasNextString() == "[" ll "]" ll ","){
am I close though?
- 10-12-2009, 11:06 AM #6
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
Of course it gives errors. hasNextString() method doesn't exist. Always check the API specs for methods before using them.
Read the API specs for hasNext(String pattern) and next(String pattern) methods.
So you basically check if there is input matching that pattern and read input that matches that pattern if it exists.
- 10-12-2009, 11:17 AM #7
Member
- Join Date
- Oct 2009
- Posts
- 8
- Rep Power
- 0
The reason this statement
if(input.hasNextString() == "[" ll "]" ll ",")
could be giving an error could be that the next token maybe like "[xyz]" & you are trying
to compare "[xyz]" to "[" which will always be false.
Try using -- input.next().contains("[") || input.next().contains("]") || input.next().contains(",")
Also, it is wise to compare strings using the String.equals() function;
- 10-12-2009, 12:55 PM #8
Member
- Join Date
- Oct 2009
- Location
- Belgium
- Posts
- 18
- Rep Power
- 0
- 10-12-2009, 03:09 PM #9
Member
- Join Date
- Sep 2008
- Posts
- 16
- Rep Power
- 0
Similar Threads
-
Question mark colon operator question
By orchid in forum Advanced JavaReplies: 9Last Post: 12-19-2010, 08:49 AM -
Need help with scanner.
By mainy in forum New To JavaReplies: 3Last Post: 07-28-2009, 02:11 PM -
Scanner
By choko in forum New To JavaReplies: 10Last Post: 01-24-2009, 03:37 PM -
Scanner class question
By Rgfirefly24 in forum New To JavaReplies: 5Last Post: 04-25-2008, 12:41 AM -
need help with scanner
By whiterex in forum New To JavaReplies: 1Last Post: 04-22-2008, 01:41 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks