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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T18:44:28+00:00 2026-06-03T18:44:28+00:00

I have some trouble reading file in Java. What a file looks like: Answer

  • 0

I have some trouble reading file in Java.

What a file looks like:

Answer 1:
1. This is an apple
2. Something
Answer 2:
1. This is a cool website 
2. I love banana
3. This is a table
4. Programming is fun
Answer 3. 
1. Hello World
.... 

What I want to do is separate them into two items:
One is the Answer number; The other one is list of answers.

So assuming I have a object class called Answer:
String of answer number
List of answers.

This is what I have done so far to debug my code before I put it into object class. But I’m not able to get the correct result

public void reader(String file) throws FileNotFoundException, IOException {
    FileReader fR = new FileReader(file);
    BufferedReader bR = new BufferedReader(fR);
    String line = null;

    int count = 0 ;
    String blockNum = "";
    String printState = "" ;
    while ((line = bR.readLine()) != null) {
        if(line.contains("Answer")){
            //System.out.println("Contain Answer statement: " + line);
            count++;
            blockNum = line; 
            printState = "";
        }
        else{
            //System.out.println("No Answer Statement: " + line);
            printState += line + " / " ;
        }

        System.out.println( count + " " + blockNum + "   " + printState );
    }

    // Close the input stream
    bR.close();
    fR.close();
}

I’m pretty sure I did something stupid while I’m coding. I’m not too sure how to read it so that it will have separate it.

Right now the output looks like this:

1 Answer 1:   
1 Answer 1:   1. This is an apple / 
1 Answer 1:   1. This is an apple / 2. Something / 
2 Answer 2:   
2 Answer 2:   1. This is a cool website  / 
2 Answer 2:   1. This is a cool website  / 2. I love banana / 
2 Answer 2:   1. This is a cool website  / 2. I love banana / 3. This is a table / 
2 Answer 2:   1. This is a cool website  / 2. I love banana / 3. This is a table / 4. Programming is fun / 
3 Answer 3.    
3 Answer 3.    1. Hello World / 

But I want the output to be something like this:

1 Answer 1:   1. This is an apple / 2. Something / 
2 Answer 2:   1. This is a cool website  / 2. I love banana / 3. This is a table / 4. Programming is fun /    
3 Answer 3.    1. Hello World / 
  • 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-03T18:44:29+00:00Added an answer on June 3, 2026 at 6:44 pm

    One solution use a flag for printTime as a boolean.

    boolean printTime = false;
    
    ...
       if(line.contains("Answer")) {
    
           if (printTime!= false) {
              System.out.println( count + " " + blockNum + "   " + printState );
              printTime=false;
           }
            ...
      }else{
            //System.out.println("No Answer Statement: " + line);
            printState += line + " / " ;
            printTime=true; // you have one answer 
      }
      ...    
    

    add a little extra print at the end of the while for last answer

    that way you can have several printState for one answer in one line.

    But correct “java” way of handle this is to create your objects:

    List<Answer> listOfAnswer = new LinkedList<Answer>();
    Answer answer;
    ...
    if(line.contains("Answer")){
                //System.out.println("Contain Answer statement: " + line);
                count++;
    
                answer = new Answer(line);
                listOfAnswer.add(answer)
            }
            else{
                answer.add(new Answer(line));
            }
    }
    ...
    

    and only after print them out 🙂

    System.out.println(listOfAnswer.toString());
    

    Simpler solution is to use a

    Map<Integer, LinkedList<String>> answers = new LinkedHashMap<Integer, LinkedList<String>();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some trouble reading from an ini file using boost program options. The
I'm having some trouble reading from an Excel spreadsheet in C#. I have this
I'm having trouble reading in supplementary unicode characters using Java. I have a file
Im having some trouble writing a getstring function, this is what I have so
I have some trouble with my trac installation (version 11.4). What should I do
I have some trouble with jquery ui-events: In my application, there are some sliders.
I have some trouble achieving adequate real-time performance from my application and wondering if
I'm have some trouble with the fulltext CONTAINS operator. Here's a quick script to
I am mlearning javascript and have some trouble creating an onject via prototype. I
Good Morning, i have some Trouble with the Zend Framework and Zend_DB_Select, i want

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.