Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: How to compare the current token with upcoming or previous token using Scanner (or some alternative)?

  1. #1
    Junior Member
    Join Date
    Sep 2013
    Posts
    18
    Thanks
    3
    Thanked 3 Times in 1 Post

    Default How to compare the current token with upcoming or previous token using Scanner (or some alternative)?

    Say I am reading in a text file that has String codes that represents rows of seats on an aircraft.

    Here is what a line from the file would look like -

    1A XXX XXX XXX  1D  1G XXX XXX XXX XXX  1J
     3A XXX XXX XXX  3D  3G XXX XXX XXX XXX  3J 
    XXX XXX XXX XXX XXX XXX XXX XXX XXX  8H  8J 
     9A  9B XXX XXX  9D  9E  9G XXX XXX  9H  9J

    The alphanumeric code refers to an airline seat, the "XXX" refers to a non-seat (aisle).
    So you can gather from this snippet that if a token contains an "A" or "J", it's a window seat.
    But, what is the best way to see if it's an aisle seat? The only way I can think of to check if the token is an aisle seat is that if the current token represents a seat, but the next token is "XXX", OR the last token was "XXX", it's an aisle seat.
    I am not sure how I would program that though. I represent the current token using
    String word = lineScan.next()

    So, the current token is represented as lineScan.next(). If I want to compare word with a previous or upcoming token on the line, how would I do that?
    Would an if statement like this valid?
     if (word.equals(lineScan.next())

    I am guessing that could read the next token, but I want to make sure I am not moving the scanner to the next token by doing that.

    Really, I am just stuck here and would like some guidance.

    Thanks!


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,585
    Thanks
    50
    Thanked 2,235 Times in 2,207 Posts

    Default Re: How to compare the current token with upcoming or previous token using Scanner (or some alternative)?

    If you want to be able to push a token back so it can be read again, look at using the StreamTokenizer class.
    It has a pushBack() method.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Sep 2013
    Posts
    18
    Thanks
    3
    Thanked 3 Times in 1 Post

    Default Re: How to compare the current token with upcoming or previous token using Scanner (or some alternative)?

    interesting. I have no experience with streamtokenizer yet. I will do some reading up on it and see if it helps me out.

    thanks!

    --- Update ---

    I did more research and it looks like reading in the line as a String and then splitting it using the String.split method would be even more ideal.

Similar Threads

  1. Syntax error on Token else?
    By SgtGarrison in forum Loops & Control Statements
    Replies: 15
    Last Post: March 20th, 2013, 06:26 AM
  2. Syntax error on token ";", { expected after this token please HELP
    By Creeper in forum What's Wrong With My Code?
    Replies: 6
    Last Post: March 1st, 2012, 02:12 PM
  3. Every token to a string?
    By Hjorth96 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: March 21st, 2011, 06:07 AM
  4. Syntax error on token ";", @ expected after this token
    By MagicMojo in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 16th, 2011, 07:48 AM
  5. Getting length of individual token?
    By Kimimaru in forum What's Wrong With My Code?
    Replies: 10
    Last Post: February 10th, 2011, 01:48 AM