hi what i want is to separate a file in many variables and store it in an arrayList to use it for creating objects and printing.
The file is as follows:
L1234; Land; 50000 euros; 120 sq.m.; Artemida Attikis; near the sea; for sale
FF4234; Flat; 120000 euros; 72 sq.m.; floor 1; 2 bedrooms; Abelokipoi Athens; for sale
HC178; House; 250000 euros; 120 sq.m.; 4 bedrooms; Menidi; for sale
FL2; Flat; 105000 euros; 70 sq.m.; floor 2; 2 bedrooms; Patisia Athens; sold
H12311; House; 310000 euros; 180 sq.m.; 5 bedrooms; Anthoupoli; for sale
H12621; House; 280000 euros; 200 sq.m.; 5 bedrooms; Menidi; sold
H72621; House; 210000 euros; 110 sq.m.; 3 bedrooms; Peristeri; sold
L1288; Land; 50000 euros; 130 sq.m.; Artemida Attikis; near the sea; sold
L1298; Land; 45000 euros; 130 sq.m.; Artemida Attikis; near the sea; sold
i want the int variables and strings in different variables. So far i have this:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package prea;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;
/**
*
*
*/
public class PropertyList {
private ArrayList<Property> list;
public PropertyList() {
list = new ArrayList<Property>();
separate("file.txt");
}
public void print() {
for(Property f:list)
System.out.println(f.toString());
}
private void separate(String filename){
File f = new File(filename);
try {
Scanner sc = new Scanner(f).useDelimiter(";");
while(sc.hasNext()) {
String line[] = sc.nextLine().split(" ");
if(line[1].trim().equals("Flat")) {
String code = line[0].trim();
String type = line[1].trim();
int price = Integer.parseInt(line[2].trim());
int size = Integer.parseInt(line[3].trim());
int floors = Integer.parseInt(line[4].trim());
int bedrooms = Integer.parseInt(line[5].trim());
String location = line[6];
String sale = line[7];
Property p = new Flat(floors, bedrooms, code, type, price, size, location, sale);
list.add(p);
}
else if(line[1].trim().equals("Land")) {
String code = line[0].trim();
String type = line[1];
int price = Integer.parseInt(line[2]);
int size = Integer.parseInt(line[3]);
String location = line[4];
String comment = line[5];
String sale = line[5];
Property p =new Land(comment, code, type, price, size, location, sale);
list.add(p);
}
else if(line[1].trim().equals("House")) {
String code = line[0].trim();
String type = line[1];
int price = Integer.parseInt(line[2]);
int size = Integer.parseInt(line[3]);
int bedrooms = Integer.parseInt(line[4]);
String location = line[5];
String sale = line[6];
Property p = new House(bedrooms, code, type, price, size, location, sale);
list.add(p);
}
}
}
catch(Exception e) {
System.out.println("File not found");
}
}
public ArrayList<Property> getlist(){
return list;
}
}
But when i try to see what my ArrayList has in it it shows nothing. Please help if you can. Also ignore all the other variables and objects they are from other classes and they work. Ohh and also consider the first "if" as the correct one allthough the other "else if" are not completed it compiles and it should at least print me the ones that the "if" catches.