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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T23:52:13+00:00 2026-05-12T23:52:13+00:00

any way easier to do this?? i’m trying to import a file which is

  • 0

any way easier to do this??

i’m trying to import a file which is four lines:

name
phone
mobile
address

I’m using:

public void importContacts() {
    try {
        BufferedReader infoReader = new BufferedReader(new FileReader(
                "../files/example.txt"));
        int i = 0;
        String loadContacts;

        while ((loadContacts = infoReader.readLine()) != null) {
            temp.add(loadContacts);
            i++;
        }

        int a = 0;
        int b = 0;

        for (a = 0, b = 0; a < temp.size(); a++, b++) {
            if (b == 4) {
                b = 0;
            }

            if (b == 0) {
                Name.add(temp.get(a));
            }

            if (b == 1) {
                Phone.add(temp.get(a));
            }

            if (b == 2) {
                Mobile.add(temp.get(a));
            }

            if (b == 3) {
                Address.add(temp.get(a));
            }
        }
    }

    catch (IOException ioe) {
        JOptionPane.showMessageDialog(null, ioe.getMessage());

    }

    txtName.setText(Name.get(index));
    txtPhone.setText(Phone.get(index));
    txtMobile.setText(Mobile.get(index));
    txtAddress.setText(Address.get(index));

}

is their an easier way? looks long winded!

  • 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-12T23:52:14+00:00Added an answer on May 12, 2026 at 11:52 pm

    Why not just:

    public String readLine(BufferedReader pReader) {
        try {
            return pReader.readLine();
        } catch(IOException IOE) {
            /* Not a very good practice but let say "We don't care!" */
            // Return null if the line is not there (like there was no 4 lines in the file)
            return null;
        }
    }
    
    public void importContacts() {
        try {
            BufferedReader infoReader = new BufferedReader(new FileReader("../files/example.txt"));
            txtName   .setText(readLine(infoReader));
            txtPhone  .setText(readLine(infoReader));
            txtMobile .setText(readLine(infoReader));
            txtAddress.setText(readLine(infoReader));
        } catch (IOException ioe) {
            JOptionPane.showMessageDialog(null, ioe.getMessage());
        }
    }
    

    Hope this helps.

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

Sidebar

Related Questions

Any easier way to write this if statement? if (value==1 || value==2) For example...
I am not sure if this is possible or if there's any easier way
Having the hours and minutes, is there any easier or better way to set
Is there any way to check whether a file is locked without using a
Is there any way to apply an attribute to a model file in ASP.NET
Is there any way to have something that looks just like a file on
When you open a .txt file with fopen Is there any way to delete
I am trying to find a way to copy a resource file to a
Is there any way to utilize a master.less file to house all of the
I'm trying to find an easier way to create <td> elements and inserting text

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.