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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T20:23:15+00:00 2026-06-14T20:23:15+00:00

I have some issues with the passing array values to the constructor. I have

  • 0

I have some issues with the passing array values to the constructor. I have a file.txt which contains some lines with values, for example:

peter|berlin|germany|0930295235|foo.

I figured out how to transform the lines into the arrays. But I don’t know how to pass the values from arrays to the constructor to transform the arrays into the objects with the array attributes.

Here is the main class:

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.InputStreamReader;


public class FileReader {

    public static void main(String[] args) {
        try {
            // Open the file that is the first 
            // command line parameter
            FileInputStream fstream = new FileInputStream("file.txt");
            // Get the object of DataInputStream
            DataInputStream in = new DataInputStream(fstream);
            BufferedReader br = new BufferedReader(new InputStreamReader(in));
            String strLine;

            // Read the file line by line
            while ((strLine = br.readLine()) != null) {
                // Print the content on the console
                String s[] = strLine.split("\\|");
                // System.out.println(java.util.Arrays.toString(s));
                // System.out.println (strLine);
                for (String element : s) {
                    System.out.println(element);
                    // HERE I NEED TO PASS THE ARRAY VALUES FOR EACH LINE TO TRANSFORM THE ARRAYS TO OBJECTS
                    Item item = new Item(element,element,element,element,element);
                    System.out.println("*************************************");
                }
                // Close the input stream
                in.close();
            } catch (Exception e) { //Catch exception if any
                System.err.println("Error: " + e.getMessage());
        }
    }
}

Here is the class Item:

public class Item {

    private String name;
    private String city;
    private String state;
    private String number;
    private String foo;

    public Object(String name, String city, String state, String number,String foo){
        this.name = name;
        this.city = city;
        this.state = state;
        this.number = number;
        this.foo = foo; 
    }

    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getCity() {
        return city;
    }
    public void setCity(String city) {
        this.city = city;
    }
    public String getState() {
        return state;
    }
    public void setState(String state) {
        this.state = state;
    }
    public String getNumber() {
        return number;
    }
    public void setNumber(String number) {
        this.number = number;
    }
    public String getFoo() {
        return foo;
    }
    public void setFoo(String foo) {
        this.foo = foo;
    }
}

I need objects for more work with the data. I appreciate any help.

  • 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-14T20:23:16+00:00Added an answer on June 14, 2026 at 8:23 pm

    I think what you want to do is something like the following, although it’s not entirely clear.

    String s[] = strLine.split("\\|");
    Item item = new Item(s[0], s[1], s[2], s[3], s[4]);
    

    Also note that after you’ve created your new Item object it will fall out of scope nearly immediately, so you need to store it in some sort of container declared outside of your while loop:

    List<Item> items = new ArrayList<Item>();
    String line;
    
    while((line = br.readLine()) != null) {
        String[] words = line.split("\\|");
        Item item = new Item(words[0], words[1], words[2], words[3], words[4]);
        items.add(item);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some issues in setting the classpath for the a project for which
I have some issues with a PHP file that is not working properly. The
Have some issues with passing large amount of data (3 MB) from uboot to
I have an array of values being made available, but unfortunately some of the
I have some issues with my EJB 3 timer where it is launched more
I'm have some issues adding a class to 2 navigation links on a theme.
I seems to have some issues in order to compile gproc from uwiger. I
Im starting with CoCos2d development and I think I have some issues with nodespace,
Building my first SL MVVM application (Silverlight4 RC) and have some issues i don't
I am integrating PayPal in my website But I have some issues in it.

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.