Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7929353
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T20:02:46+00:00 2026-06-03T20:02:46+00:00

I currently have a If statement that checks to see if the next word

  • 0

I currently have a If statement that checks to see if the next word in a document equals a certain length. After it returns true, I want it to add that word to an array or string etc. My problem is once I use the next() command, it proceeds to the next value in the document even though I used it in the condition of the If statement. I am somewhat new to Java so I hope this simple click makes sense.

Scanner in1 = new Scanner(inputFile);
String inputString;

while(in1.hasNextLine()){
     if(in1.next().length() == 6){
          inputString = in1.next();
     }
}

Basically I have a file that contains several words, all different lengths on multiple lines. I want to catch the word that has 6 characters in it. The actual while loop and file is more complex, the problem is that it jumps to the next word after it evaluates the If statement. So if it detects that the next word has 6 characters, it loads the following word that comes after it into the variable. Any help or advice is welcome!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T20:02:50+00:00Added an answer on June 3, 2026 at 8:02 pm

    You can split this loop up into two pieces:

    1. Get the next token and store it, and
    2. Use that cached token.

    For example:

    while(in1.hasNextLine()) {
         String current = in1.next();
         if(current.length() == 6) {
              inputString = current;
         }
    }
    

    This is a standard technique when using iterators or scanners for precisely the reason you’ve noted – the next() method can’t be called multiple times without producing different values.

    As an aside, other programming languages have designed their iterators so that getting the value being iterated over and advancing to the next location are separate steps. Notably, the C++ STL considers these to be different operations.

    Hope this helps!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a filter_before filter that checks to see if a param is true.
I currently have this sql statement that I wrote and it works but it's
I have an sql statement that currently is just returning all the end parent
I currently have a statement which reads if(Arrays.asList(results).contains(Word)); and I want to add at
I have a property in a composite control that checks to see if the
I currently have this statement in my SQL View (SQL Server 2008 R2) which
Hey guys quick question, I currently have an insert statement $query= INSERT into new_mail
I currently have the following MySQL statement to replace the HTML entity for a
I have an if statement currently written (wrongly) as: <%# Eval(AccumHolidays).ToString() != ? Eval(AccumHolidays).ToString()
Currently I have a service that uses a UserNamePasswordValidator to authenticate the client user.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.