-2

I need to convert a .txt file into arrays and will then plot it in a graph. My problem is this:

I have a text file containing x,y values:

23, 92
26, 16
45, 14
32, 11
43, 17
46, 58

image here for x and y values

and so on.....(so many values)

and i want to convert it into arrays:

double [] x= { 23, 26, 45, 32, 43, 46,...}
double [] y= { 92, 16, 14, 11, 17, 58,...}

This arrays will be set in a graph, but i need to set this first into arrays for me to plot it.

I still don't have a code for this yet. Please help :(

DarkCygnus
  • 7,420
  • 4
  • 36
  • 59
Johanna
  • 7
  • 7
  • This seems like an assignment question, asking such questions directly is discouraged. A better way would be to split your problems into smaller problems e.g. 1) how to read a txt file in a particular programming language e.g. ruby 2) how to use arrays in ruby, and then you should try to connect these dots to complete your assignment. Also these questions are trivial and you need to search existing question base before posting them as new questions. – Usman Jun 12 '16 at 21:27

2 Answers2

1
public class ReadFile{
    public static void main(String[] args){
        BufferedReader br  = new BufferedReader(new BufferedReader(new FileReader("pathToFile")));
        String line = "";
        // reading when each line has x, y
        while((line = br.readLine())!=null){
             String[] t = line.split(",");
             double x = Double.parseDouble(t[0].trim());
             double y = Double.parseDouble(t[1].trim());
             // store it in the array/list
             // or create a Class Pair having x & y coordinate      
        } 
    }
}

with Pair class it will look like this

class Pair{
   double x , y;
   Pair(double x , double y){
     this.x = x;
     this.y = y;
   }
}

// the reading logic
    public class ReadFile{
        public static void main(String[] args){
            List<Pair> pointList = new ArrayList<Pair>();
            BufferedReader br  = new BufferedReader(new BufferedReader(new FileReader("pathToFile")));
            String line = "";
            while((line = br.readLine())!=null){
                 String[] t = line.split(",");
                 double x = Double.parseDouble(t[0].trim());
                 double y = Double.parseDouble(t[1].trim());
                 // store it in the array/list
                 // or create a Class Pair having x & y coordinate
                 Pair p = new Pair(x,y);
                 pointList.add(p);       
            } 
        }
    }
Ankit Deshpande
  • 3,476
  • 1
  • 29
  • 42
1

Since you do not know upfront how many vertices you have, consider using two List collections in place of two arrays.

This would let you follow the algorithm below:

  • Prepare two lists, listx and listy
  • In a loop, read two integers, x and y
  • If two integers are not available, exit the loop
  • Otherwise, add x to listx and y to listy

Once the loop is over, you have your points in two parallel collections.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523