简体   繁体   中英

How can i read pairs (x,y) of numbers from a .txt file separated by commas in java?

for example 163,15 765,69.9

the 163 and 765 should be stored in one variable (x) and 15 and 69.9 in another variable (y)

im having a lot of problems in learning java :( Thanksss

The following should give you an idea on how to implement this

// The following is pseudo code

ArrayList<Float> data; // we will assume this to be [163,15,765,69.9]
                       // you will need to implement and parse this
                       // yourself

ArrayList<Float> tmp1 = new ArrayList<>();
ArrayList<Float> tmp2 = new ArrayList<>();

for (int i=0; i<data.size(); i++)
{
    if (i % 2 == 0)
    {
        tmp1.add(data[i]);
    }
    else
    {
        tmp2.add(data[i]);
    }   
}

// We now have something that looks like this
// tmp1 = [163, 765]
// tmp2 = [15, 69.9]

Now you just need to form tuples with the following lists.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM