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.

Recommended Answers

All 7 Replies

You seem to be mixing parsing with a Scanner and parsing with String.split, and getting the two confused.
Your Scanner splits the file by the ';' chars, but you don't use that in your parsing, you just read whole lines. Then you try to split each of those lines on a blank char instead of a ';'.
I would keep the readLine, but then split the line on the ';' character, after that your code is heading in the right direction.
ps: When faced with a bug like this, add a load of print statements to your code so you can see what's going on. Eg if you printed your line[] array you would immediately see that it's using the wrong delimiter.

> catch(Exception e) {
>    System.out.println("File not found");
> }

This is a mistake. There are many other Exceptions that could be thrown from that try block (eg file access/reading errors, NumberFormatException from parseInt, or an array index exception if there are not enough tokens in the line[] array). By supressing the real exception and printing your own guess you are potentially misleading yourself. You could spend hours trying to fix a "file not found" when the real problem is in the parsing.
Either restrict your catch to the specific exception (FileNotFoundException) or include an e.printStackTrace(); so youcan see the truth.

commented: Excellent advice :) +13

so what am i supposed to write? i've been banging my head on this one cause it compiles perfectly no errors at all but it prints nothing when i press the button from my Gui.
what i want is to keep only the values i need from the file.
e.g "310000 euros" i only want the number "310000" and not the "euros"
the same goes for the rest so have to split the file twice but as you said i've messed up the splits and now i'm just lost.

You have choices, but what I would do is:
Read one line at a time
Split that on the ';' delimiters
Where one of those splits needs to be futher parsed (like your eg above) do a second split, eg

String[] tokens = line.split(";");
// tokens[2] is now "310000 euros"
String[] money = tokens[2].split(" ");
// money[0] is now "310000" and money[1] is "euros"

And don't forget to use print statements to see what your code is doing!

great thanks that helps with the spliting but do you have any idea of why when i try to print my arraylist it does nothing?

Maybe none of the if tests was working because the parsing was wrong. but...

use print statements to see what your code is doing!

ye i used the for each print method i had for the list and it printd now im just gonna tweek the rest of the project. Thanks u saved my butt :P

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.