0

I have a problem with reading from a text file to an arraylist. The problem is that i don't know how to read in multiple types, because in my arraylist there are Points, Strings, booleans, therefor linesplit doesn't work. I checked all the topics and didn't find a solution to this.

edit: Elrendezes class looks like

  class Elrendezes {
   protected Point Po;
   protected String hely;
   protected String foglalo;
   protected boolean foglalt;
  }

Here's how my file looks like:

 java.awt.Point[x=16,y=13], 1, name1, false

And the method to read is

public static ArrayList<Elrendezes> readDataFromFile(){
      ArrayList<Elrendezes> ElrList = new ArrayList<Elrendezes>();
      FileInputStream fstream = null;
      try
      {
          fstream = new FileInputStream("src/files/DataFile.txt");
          BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
          String strLine = null ;
          String tokens[] = strLine.split(", ");


          while ((strLine = br.readLine()) != null)   {
           tokens = strLine.split(", ");
          // THIS DOES NOT WORK: ElrList.add(new Elrendezes(tokens[0], tokens[1], tokens[2], tokens[3]));
          }
      }
      catch (IOException e) {
        e.printStackTrace();
      }
      finally {
          try { fstream.close(); } catch ( Exception ignore ) {}
      }
      return ElrList;

}
8
  • THIS DOES NOT WORK what doesn't work can please say a little bit about the error you are getting Commented Nov 26, 2012 at 13:32
  • How is the data file written? Did someone else wrote it? Do you have access to this code? Commented Nov 26, 2012 at 13:34
  • i make the file, and this is the error: constructor Elrendezes(String, String, String, String) is undefined, this is because the constructor of Elrendezes is Point, String, String, Boolean Commented Nov 26, 2012 at 13:37
  • So what is your problem? Parsing? Commented Nov 26, 2012 at 13:39
  • i might just make a point to string function, that'll probably help Commented Nov 26, 2012 at 13:39

2 Answers 2

1

As you probably dont know regular expressions, I will use:

  1. Get x and y:

    int v1 = strLine.indexOf("x=");
    int v2 = strLine.indexOf(",y=");
    int v3 = strLine.indexOf("]")  ; 
    string x = strLine.substring(v1 +2, v2);
    string y = strLine.substring(v2 +3, v3);
    
  2. Break point representation from the rest.

    string secondpart = strLine.substring(v3+1);
    
  3. Now break secondpart with only the coma as separator.
  4. Convert string representation, using Integer.parseInt() and etc..

  5. Construct your object back.

Note: written in a hurry, check if the indexes used for x, y and secondpart are correct. I also assumed the input is correct.

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

Comments

0

I think you have to choose a char to split the String.

Cast the split string into boolean, integer etc.

Elrendezes(String[] s){
Point Po = s[0]; // dont know how to cast string to point, never done before
hely = s[1];
foglalo = s[2];
foglalt = Boolean.parseBoolean(s[3]);
}

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.