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 9153373
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T12:15:43+00:00 2026-06-17T12:15:43+00:00

I have a scanner reading from a text file. When it gets to a

  • 0

I have a scanner reading from a text file. When it gets to a certain line in the file, I want to be able to call a method that uses the Scanner as a parameter in its current state – as in, I want the scanner to be passed (along with the file used) at exactly what line it’s on.

public static void createEntry(File list, int mediaTypeNum, String mediaType) throws FileNotFoundException {
    Scanner mediaReader = new Scanner(list);
    int occurrence = mediaTypeNum;
    int scannerCounter = 0;
    String match;
    String title = "";
    String director = "";
    while (mediaReader.hasNext() && scannerCounter < occurrence) {
        match = mediaReader.nextLine();
        if (match.equalsIgnoreCase(mediaType)) {
            scannerCounter++;
            if (scannerCounter == occurrence) {
                // based on type, create the media object
                // createMediaObject(list, mediaReader, mediaType)
                title = mediaReader.nextLine();
                director = mediaReader.nextLine();
            }
        }
    }

createMediaObject() will have a switch case, and based on the case, the Scanner will read some following lines until there’s a blank line. For each media type, the number of lines differ.

Thank you in advance!

  • 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-17T12:15:44+00:00Added an answer on June 17, 2026 at 12:15 pm

    Of course, it is in fact true to any object in Java. No implicit copies of objects are created at any point.*

    What you have to be careful about is that Scanner has a “sequential” state and if you’re passing it around a lot, it’s very easy to lose track of what state it is in. Especially if conditional processing is performed, where different execution paths might end up leaving it at different positions.

    A good approach to this is to always document in the method Javadoc what state the Scanner is expected to be in and what state it’s left at the end.

    *There’s a big caveat to that statement: multithreading. But it would be highly off topic to discuss that here in detail.

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

Sidebar

Related Questions

I have a program reading from a text file (currently 653 lines long) all
I have a scanner that is reading text into a UITextField, looks like it
I have a Java problem that involves reading a text file and check that
I'm reading a text file and storing a set of unique words from that
i have implement a small code of reading from a file with scanner and
I have created some application, which is reading from System.in using the following method:
I have a program that takes a list of names from a file and
I have 2 scanner objects, scan1 and scan2. scan1 is taking a line from
I am reading two files and I want for each line in file one
I am reading a file line by line using Scanner. So my question is

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.