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

  • Home
  • SEARCH
  • 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 6334067
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T18:35:17+00:00 2026-05-24T18:35:17+00:00

I need to retrieve two lines from 1 .txt files and output them to

  • 0

I need to retrieve two lines from 1 .txt files and output them to a dialog box. My code as of now is

private String getfirstItem() {
     String info = "";
     File details = new File(myFile);
     if(!details.exists()){
            try {
                details.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

      BufferedReader read = null;
    try {
        read  = new BufferedReader (new FileReader(myFile));
    } catch (FileNotFoundException e3) {
        e3.printStackTrace();
    }

     for (int i = baseStartLine; i < baseStartLine + 1; i++) {
                  try {
                info = read.readLine();
                } catch (IOException e) {

                    e.printStackTrace();
                }
            } 
            firstItem = info;             
          try {
              read.close();
        } catch (IOException e3) {

            e3.printStackTrace();
        }
          return firstItem;
}

private String getsecondItem() {
    File details = new File(myFile);
    String info = "";
   BufferedReader reader = null;
  if(!details.exists()){
try {
    details.createNewFile();
} catch (IOException e) {
    e.printStackTrace();

}}



try {
reader   = new BufferedReader (new FileReader(myFile));
} catch (FileNotFoundException e3) {
    e3.printStackTrace();
    }

 for (int i = modelStartLine; i < modelStartLine + 1; i++) {
          try {
     info= reader.readLine();
            } catch (IOException e) {
        e.printStackTrace();}
        modelName = info;}  try {
          reader.close();
} catch (IOException e3) {
    e3.printStackTrace();
    }
 return secondItem;
}

However, i keep getting the same value for both? modelStartLine=1 and baseStartLine =2

  • 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-05-24T18:35:18+00:00Added an answer on May 24, 2026 at 6:35 pm

    You’re never actually skipping any lines. You start your loop index from a different number, but you still loop only once, from the beginning of the file. Your loop should look something like this:

    public string readNthLine(string fileName, int lineNumber) {
        // Omitted: try/catch blocks and error checking in general
        // Open the file for reading etc.
    
        ...
    
        // Skip the first lineNumber - 1 lines
        for (int i = 0; i < lineNumber - 1; i++) {
            reader.readLine();
        }
    
        // The next line to be read is the desired line
        String retLine = reader.readLine();
    
        return retLine;
    }
    

    Now you can just call the function like this:

    String firstItem = readNthLine(fileName, 1);
    String secondItem = readNthLine(fileName, 2);
    

    However. Since you just want the first two lines of the file, you can just read them both initially:

    // Open the file and then...
    String firstItem = reader.readLine();
    String secondItem = reader.readLine();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

From this string: /resources/pages/id/AirOceanFreight.xhtml I need to retrieve two sub-strings: the string after pages/
I need to retrieve information from two separate models which are similar but not
I need to retrieve data from two tables. the first is a list of
I have two tables and need to retrieve some columns from both tables based
I need to retrieve data from MsAccess/SQL Server (two separate projects in which I
I need to retrieve all items from two lists that contains a given value.
I need to retrieve columns from two tables and I have used an INNER
I am developing an application where i need to retrieve multiple audio files from
I need to retrieve out the nominator and denominator into two int type variables,
Hi I'm new to Oracle 10g. I need retrieve the all the sublist from

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.