Results 1 to 6 of 6
  1. #1
    Noceo is offline Member
    Join Date
    Oct 2012
    Posts
    9
    Rep Power
    0

    Question Scanner: useDelimiter(); to get rid of quotes (" ")

    Hey there,

    I got a text including some quotation marks (e.g. "Bla bla bla"). That means that the scanner reads that as three different words, where it should be the same. I know of the useDelimiter method and I'm using it. But I can't get it to ignore quotation marks. Please help?

    - Jake

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,574
    Rep Power
    12

    Default Re: Scanner: useDelimiter(); to get rid of quotes (" ")

    I know of the useDelimiter method and I'm using it.
    How are you using it?

    It seems to me that what counts as a delimiter will change depending on whether you are inside a quoted string or not. Others may chime in - because I'm no expert with this stuff - but I'd use a StreamTokenizer.

    Java Code:
    import java.io.IOException;
    import java.io.Reader;
    import java.io.StreamTokenizer;
    import java.io.StringReader;
    
    public class StreamTokenizerEg {
        public static void main(String[] args) throws IOException {
            String data = "\"Bla bla bla\" is 3 words in quotes.";
            Reader in = new StringReader(data); // or reader from file...
            
            StreamTokenizer tokenizer = new StreamTokenizer(in);
            int wordCount = 0;
            int numCount = 0;
         
            while(tokenizer.nextToken() != StreamTokenizer.TT_EOF) {
                if(tokenizer.ttype == StreamTokenizer.TT_NUMBER) {
                    System.out.println("NUMBER " + tokenizer.nval);
                    numCount++;
                } else if(tokenizer.ttype == StreamTokenizer.TT_WORD) {
                    System.out.println("WORD   " + tokenizer.sval);
                    wordCount++;
                } else {
                    System.out.println("OTHER  " + tokenizer.sval);
                }
            }
            
            System.out.println(wordCount + " words, and " + numCount + " numbers");
        }
    }
    StreamTokenizer doesn't seem very clever about numbers, but that doesn't play any part in your problem (and it can be set up not to tokenise numbers). It is, however, better at detecting quoted strings.
    Last edited by pbrockway2; 11-08-2012 at 08:44 AM.

  3. #3
    Noceo is offline Member
    Join Date
    Oct 2012
    Posts
    9
    Rep Power
    0

    Default Re: Scanner: useDelimiter(); to get rid of quotes (" ")

    Right now I'm using a Scanner, but as it is text files, it might be a better solution to implement the StreamTokenizer (didn't know of that before). I'm gonna rewrite some code and see if it works.

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,574
    Rep Power
    12

    Default Re: Scanner: useDelimiter(); to get rid of quotes (" ")

    Change the line in my code from in=new StringReader() to in=new FileReader(filename) and see what happens.
    Last edited by pbrockway2; 11-08-2012 at 08:46 AM.

  5. #5
    Noceo is offline Member
    Join Date
    Oct 2012
    Posts
    9
    Rep Power
    0

    Default Re: Scanner: useDelimiter(); to get rid of quotes (" ")

    How come when i type the following code, it prints: -3?

    Java Code:
    // Using FileReader and StreamTokenizer instead of scanner
    Reader readFile = new FileReader(fullFilename);
    StreamTokenizer readText = new StreamTokenizer(readFile);
    System.out.println(readText.nextToken());
    There is no "-3" anywhere in the text it reads...


    Never mind, I figured that one out myself. New question:
    The text reader does not need to read from the file until after a specific line (not specific by number, but a specific string). Like "*** START OF THIS PROJECT GUTENBERG EBOOK " + title + " ***" where title is the book title. And then end in the same manner. How do i get it to skip lines?
    Last edited by Noceo; 11-08-2012 at 11:02 AM.

  6. #6
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,574
    Rep Power
    12

    Default Re: Scanner: useDelimiter(); to get rid of quotes (" ")

    Read each line and just do nothing until you hit the line where the interesting text begins.

Similar Threads

  1. Replies: 3
    Last Post: 10-30-2012, 04:06 PM
  2. Replies: 1
    Last Post: 02-01-2012, 09:37 PM
  3. Scanner "nextLine" Command [Homework]
    By btgrant in forum New To Java
    Replies: 1
    Last Post: 10-08-2011, 03:45 AM
  4. trouble with Scanner(new File("input"));
    By ronyosi in forum New To Java
    Replies: 9
    Last Post: 10-28-2010, 12:34 AM
  5. Replies: 1
    Last Post: 10-20-2008, 08:35 AM

Posting Permissions

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