Results 1 to 9 of 9
  1. #1
    kira137 is offline Member
    Join Date
    Oct 2009
    Posts
    14
    Rep Power
    0

    Default scanner question

    This is what I have currently
    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));
       }
     }
    I want to make it so that scanner will ignore (String text)'s '[' and ']' and ','.
    How would I do that?

    thank you in advance

  2. #2
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    Your else part should read the next token and check if the stuff to be ignored or not.

  3. #3
    kira137 is offline Member
    Join Date
    Oct 2009
    Posts
    14
    Rep Power
    0

    Default

    but I don't want it to throw exception when it comes across '[' ']' and ','. Is there some code that scanner could just skip them?

  4. #4
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    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.

  5. #5
    kira137 is offline Member
    Join Date
    Oct 2009
    Posts
    14
    Rep Power
    0

    Default

    is this what you meant?
    Java 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");
           }
         }
       }
    but this is giving me error
    if(input.hasNextString() == "[" ll "]" ll ","){

    am I close though?

  6. #6
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    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.

  7. #7
    suhassr is offline Member
    Join Date
    Oct 2009
    Posts
    8
    Rep Power
    0

    Default

    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;

  8. #8
    Vio
    Vio is offline Member
    Join Date
    Oct 2009
    Location
    Belgium
    Posts
    18
    Rep Power
    0

    Default

    Quote Originally Posted by suhassr View Post
    Also, it is wise to compare strings using the String.equals() function;
    Indeed don't compare Strings with "==" but with .equals() because otherwise you will not compare the value of the string but "name" of where your String object String located. (look up the difference between primitive types and non-primitive types).

  9. #9
    seandingobat is offline Member
    Join Date
    Sep 2008
    Posts
    16
    Rep Power
    0

    Default

    if you want to do that , then i suggest you first remove them from your strings.

    for example if you have :

    String text = " Hello my name is dingobats , i want to remove ['']";

    Then why not just remove that [' and '] from the text before inserting it into the scanner?

Similar Threads

  1. Question mark colon operator question
    By orchid in forum Advanced Java
    Replies: 9
    Last Post: 12-19-2010, 08:49 AM
  2. Need help with scanner.
    By mainy in forum New To Java
    Replies: 3
    Last Post: 07-28-2009, 02:11 PM
  3. Scanner
    By choko in forum New To Java
    Replies: 10
    Last Post: 01-24-2009, 03:37 PM
  4. Scanner class question
    By Rgfirefly24 in forum New To Java
    Replies: 5
    Last Post: 04-25-2008, 12:41 AM
  5. need help with scanner
    By whiterex in forum New To Java
    Replies: 1
    Last Post: 04-22-2008, 01:41 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •