0

I"m currently trying to read text from a text file (of course) and then trying to add them to my arraylist but it's not working when i try to use the add method.

import java.io.*;
import java.util.*;
import java.lang.Object.*;
public class Inventory
{
private String store;
private ArrayList<Flooring> products = new ArrayList();
public Inventory(String args[], ArrayList<Flooring> products) throws IOException{
    FileReader reader = new FileReader("Lowes.txt");
    BufferedReader text = new BufferedReader(reader);
    String line = text.readLine();

    while(line != null){
        products.add(line);
        line = text.readLine();
    }

    FileReader reader1 = new FileReader("Sam's Club Flooring.txt");
    BufferedReader text1 = new BufferedReader(reader1);

    FileReader reader2 = new FileReader("Home Depot.txt");
    BufferedReader text2 = new BufferedReader(reader2);
}

}
3
  • Please define what you mean by "not working". Commented Dec 2, 2015 at 14:32
  • What is the exception? Commented Dec 2, 2015 at 14:32
  • What is Flooring. Do you really need to import import java.lang.Object.*; Commented Dec 2, 2015 at 14:34

5 Answers 5

1

The add method is not working because the ArrayList contains objects of type Flooring. So it will only accept Flooring objects and not String.

private ArrayList<Flooring> products = new ArrayList();

SOLUTIONS:

  • Change type to ArrayList<String>
  • Fill some String attribute of a Flooring object and add it to list
Sign up to request clarification or add additional context in comments.

7 Comments

So pretty much i can create a "Flooring = text.readLine();" and it would read the flooring as the string in the text file?
what? Flooring = text.readLine() is not valid, but you can use String s = text.readLine() as I explain in answer
Also, what is Flooring object? Post the class code in the question
Well the Array List is supposed to hold Strings of text from the file and the Text is types of Wood flooring, so i figured that the object type would be flooring, but it would never compile so i changed it to being String... and also I am not very good with using stackoverflow so i have no clue on how to do something on this feed... sorry..
nono, text types does not mind here, just use ArrayList<String> instead of ArrayList<Flooring>
|
0

your ArrayList is defined to only hold variables of the type Flooring, hence you can´t add String values to it.

You might want to change the List definition to List<String>.

Or you want to to create new instances of Flooring depending on the line.

products.add(new Flooring(line));

Comments

0

products is an ArrayList of Flooring objects, you are trying to add line to it. line is a String.

If you want to add line, you want to initialize your products List like this:

private ArrayList<String> products = new ArrayList<String>();

Comments

0

You cannot add String object to a non String arraylist.

private ArrayList<Flooring> products = new ArrayList();

This line clearly defines that ArrayList products will accept only objects of class Flooring.

Comments

0

The add method is not working because the ArrayList contains objects of type Flooring. So it will only accept Flooring objects and not String.

private ArrayList products = new ArrayList(); SOLUTIONS:

Change type to ArrayList Fill some String attribute of a Flooring object and add it to lis

Comments

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.