1

I have a constructor in a class called Sport

public Sport(String i_sportName,int i_usagefee, int i_insurance, int i_affiliation, ArrayList<Integer> i_courtNo)

I'm wondering how I would pass information read from a textfile from another class called Club into the constructor of Sport that has the format :

//name,usagefee,affilfee,userfee,courtno Tennis,12,44,23,12,13,15,6,7

This is my current code to load the text file

public void loadSports()
{
     try
    {
        FileReader input = new FileReader("location/sports.txt");
        BufferedReader br = new BufferedReader(input);
        String line;

        String result;
        String delims = ",";
        System.out.print("\f");
        sports = new ArrayList<Sport>();
        ArrayList<Integer>courtNo = new ArrayList<Integer>();
        while((line=br.readLine())!=null)
        {
            //System.out.println(line);
            String[] linearr = line.split(delims);
            for(int i=0;i<linearr.length;i++)
            {
                System.out.println(linearr[0]);
                //Sport sports = new Sport(linearr[0],linearr[1],linearr[2],linearr[3],courtNo.add(1,2,3,4));
            }
        }
        input.close();
    }
    catch(IOException e)
    {
       e.printStackTrace();
    }
}

1 Answer 1

1

No, your attempt like this:

String[] linearr = line.split(delims);
Sport sports = new Sport(linearr[0], linearr[1], linearr[2], 
    linearr[3], courtNo.add(1,2,3,4));

won't work that simply since your constructor takes non-String parameters. You could either create an all-String constructor, something I don't recommend, or simply massage the data within the while loop so that it matches the constructor requirements, including parsing numeric Strings into numbers, but doing so within try/catch blocks.

As to the ArrayList, yes, create one within the while loop, and then fill it with the data in the text file, perhaps using a for loop if you know how many items to add, or an inner while loop if you don't, and then pass that in to the constructor.

To store your ints in an ArrayList:

  • Create the ArrayList in the while loop.
  • When you are parsing your line of text, you'll have 1 String, several individual Strings representing your individual int parameters -- you'll need to parse each of these, and then your ArrayList String/ints. When you get to them loop through them with a for loop, parsing each String into an int and then adding it into the ArrayList via the add(...) method.
  • Then when done with that inner for loop, pass all the parameters into the constructor.

The code you should do yourself.

Sign up to request clarification or add additional context in comments.

4 Comments

sorry i dont understand what you mean by massage the data? Could you provide an example?
@YongJ: I mean change Strings into numbers such as ints via Integer.parseInt(...).
The constructor I am passing to has an arraylist of integers there. I'd like to store the last few numbers from the textfile into it. How do i do that? Do i separate them by index number? This is what i have for now. Sport sports = new Sport(linearr[0],Integer.parseInt(linearr[1]),Integer.parseInt(linearr[2]),IntegerparseInt(linearr[3]),courtNo.add(i));
@YongJ: see edit to answer. You'll need an inner for loop, you'll want to parse each int before passing it into the constructor. Don't rush or over-compress your code as that's asking for disaster.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.