I have a text file contain a coordinate
100,20
240,100
23,45


I manage to read row and split it into and integer.
and get it to point x=100 ,y=20.
the problem is in the end i got a null

when i print in console show a null like this

100
20
240
100
23
45
null
null


my code to read row and then split is below

public String[] arrayData(String a) throws FileNotFoundException, IOException {
        openFile(a);
        int y = countRow(a);
        String[] dataJ = new String[y];
        z.useDelimiter("\n");
        int i = 0;
        while (z.hasNext()) {
            dataJ[i] = z.next();
            i++;
        }
        z.close();
        return dataJ;
    }

    public Point[] splitArraytoPoint(String[] a) {
        String[] arr;
        String delimiter = ",";
        int j = 0;
        Point[] dataK = new Point[a.length];
        for (int i = 0; i < a.length; i++) {
            arr = a[i].split(delimiter);

            for (int k = 0; k < 2; k++) {
                //System.out.println(i + " " + k + " " + arr[k]);
                if (k == 0) {
                    System.out.println(Integer.parseInt(arr[0].trim()));
                    dataK.x=Integer.parseInt(arr[0].trim());
                } 
                if(k==1) {
                    System.out.println(Integer.parseInt(arr[1].trim()));
                    dataK.y=Integer.parseInt(arr[1].trim());
                }
            }
            j++;
        }
        return dataK;
    }

where is the problem....
help plz.

Edited 5 Years Ago by pigmario: n/a

ouf a lot complicated for nothing

ArrayList<Point> getPoint(String filename) {
   ArrayList<Point> al = new ArrayList<Point>();
   Scanner scan = new Scanner(new File(filename));
   while(scan.hasNextLine()) {
      String line = scan.nextLine();
      String[] point = line.split(",");
      al.add(new Point(Integer.parseInt(point[0]), Integer.parseInt(point[1]));
   }
   return al;
}

I have a text file contain a coordinate
100,20
240,100
23,45


I manage to read row and split it into and integer.
and get it to point x=100 ,y=20.
the problem is in the end i got a null

when i print in console show a null like this

100
20
240
100
23
45
null
null


my code to read row and then split is below

public String[] arrayData(String a) throws FileNotFoundException, IOException {
        openFile(a);
        int y = countRow(a);
        String[] dataJ = new String[y];
        z.useDelimiter("\n");
        int i = 0;
        while (z.hasNext()) {
            dataJ[i] = z.next();
            i++;
        }
        z.close();
        return dataJ;
    }

    public Point[] splitArraytoPoint(String[] a) {
        String[] arr;
        String delimiter = ",";
        int j = 0;
        Point[] dataK = new Point[a.length];
        for (int i = 0; i < a.length; i++) {
            arr = a[i].split(delimiter);

            for (int k = 0; k < 2; k++) {
                //System.out.println(i + " " + k + " " + arr[k]);
                if (k == 0) {
                    System.out.println(Integer.parseInt(arr[0].trim()));
                    dataK.x=Integer.parseInt(arr[0].trim());
                } 
                if(k==1) {
                    System.out.println(Integer.parseInt(arr[1].trim()));
                    dataK.y=Integer.parseInt(arr[1].trim());
                }
            }
            j++;
        }
        return dataK;
    }

where is the problem....
help plz.

PBL just changed your code.. Im here to help buddy, we dont need to change your code, its your style, why change it right?...

try changing your first for loop in the splitArraytoPoint method to:
I did not test the code, so im not sure if its going to work, I just read your code and annalyzed it as much as I can. Hope this helps. Tell me if it did or not, ASAP.

Ex: see the (-1) in the logical test?( a.length-1). try that.

for(int i=0; i < a.length-1;i++)
{
//code etc.
}

Edited 5 Years Ago by JKP(): n/a

Ok..i already solve my problem..

it is null because in my main class wrong in count the row of the text...
heheh

thx.

Ok..i already solve my problem..

it is null because in my main class wrong in count the row of the text...
heheh

thx.

Great.. you solved your own problem, now I'll never know if my solution really worked. Oh well, as long as you solved it, thats all that matters. Good job.

This question has already been answered. Start a new discussion instead.