I have a text file with the list inside, every line has data that I need insert in the new objects. So data looks like somename=3000
or another type with the slash data another type = 6000
.
I have particular class "Item" that has String
and int
variable. Data need to be inserted into them. Every new object has to be added to the ArrayList<Item>
.
// Calculate the lines for next for each loop
int lineCount = 0;
while (sc.hasNextLine()) {
lineCount++;
sc.nextLine();
}
for (int i = 0; i < lineCount; i++) {
// creating the object
Item item = new Item();
// add item object to items ArrayList
items.add(item);
// add line to String variable lineToString,
while (scaner.hasNextLine()) {
String lineToString = scaner.nextLine();
sc.nextLine();
}
So, I figured out that to do this, I need to
- copy the whole line and put into some string variable;
- split it for integer and string variable;
- insert string parts to the String variable and numbers to the int variable in particular object that was created in iteration time of "for loop".
- add the object with a data inside to the ArrayList.
I used Scanner
to read a text file. When I try to insert the scaner.nextLine
to the String
it's doesn't work; I mean it's executing but variable String lineToString
doesn't have the line from a text file.
Could somebody help with an idea of how better to proceed with this problem? Maybe there is some simpler way to insert the 2 different type of data from the text file line in the object and put it into the ArrayList
? Every line in the text file has different data and has to be in different objects.