1

I have wrote 3 text files that contain 3 columns, Latitude, Longitude and Device_ID. Each file has between 50 - 100 different different longitude/latitude coordinates and between 2/4 device_id's. I wrote code that reads from these txt files and prints them on screen. My next step is to put this information into an ArrayList. I am aiming to get 3 ArrayLists for each text file, so that would be 9 ArrayLists in total. I have no idea how to create an ArrayList that would take the data?

Here is what I have done so far;

public static void main (String args []) throws FileNotFoundException
{       
    File textA = new File("C:/Users/Daniel Dold/Desktop/Routes/RoutesA.txt");
    File textB = new File("C:/Users/Daniel Dold/Desktop/Routes/RoutesB.txt");
    File textC = new File("C:/Users/Daniel Dold/Desktop/Routes/RoutesC.txt");
    Scanner scannerA = new Scanner(textA);
    Scanner scannerB = new Scanner(textB);
    Scanner scannerC = new Scanner(textC);
    int lineNumber = 1;
    while(scannerA.hasNextLine())
    {
        String line = scannerA.nextLine();
        System.out.println(line);
        lineNumber ++;
    }
    System.out.println();
    while(scannerB.hasNextLine())
    {
        String line = scannerB.nextLine();
        System.out.println(line);
        lineNumber ++;
    }
    System.out.println();
    while(scannerC.hasNextLine())
    {
        String line = scannerC.nextLine();
        System.out.println(line);
        lineNumber ++;
    }
}

The printed result is;

51.51092066     -0.14513969     430
51.51073703     -0.14553129     431
51.51059347     -0.14583707
51.51047662     -0.1461026
51.51049331     -0.14613747
51.51048997     -0.14616698
51.51040651     -0.14641106
51.51035643     -0.14649421
51.51032972     -0.14652103
51.51027296     -0.14666057
51.51022789     -0.1467517
51.51021286     -0.14695287
51.51020285     -0.14709234
51.51021119     -0.14715671
51.51039983     -0.14728009
51.51031302     -0.14770388
51.51023081     -0.14809616
51.51037396     -0.14816656
51.51053838     -0.1482477
51.51074914     -0.14835298
51.51081507     -0.14799356
51.51090271     -0.14752015
51.51114643     -0.14763012
51.5114227      -0.14775619
51.51160215     -0.14783665
51.51171149     -0.147264
51.51179829     -0.14679461
51.51195938     -0.14597654
51.51213215     -0.14510482
51.51223063     -0.14473334
51.5123045      -0.14447115
51.51207831     -0.14425389
51.51192098     -0.14410034
51.51178786     -0.14396623
51.51166058     -0.14383413
51.51149532     -0.14366381
51.51131795     -0.14348007
51.51114768     -0.14330506
51.51108133     -0.1434841
51.5109987      -0.1436866
51.51094236     -0.14381736
51.51105087     -0.1439441
51.51122406     -0.14414862
51.51129876     -0.14423512
51.51131169     -0.14427468
51.51130418     -0.14431558
51.51123699     -0.14445908
51.51112348     -0.14470249
51.5109649      -0.14504045

51.49647926     -0.11145785     428
51.49666837     -0.11155306     435
51.49686708     -0.11165432     440
51.49705494     -0.11173612
51.498177       -0.11215657
51.49827719     -0.1121968
51.49853767     -0.11243283
51.49874472     -0.11250794
51.49888665     -0.11248111
51.4992348      -0.11213779
51.50008135     -0.11133045
51.50027253     -0.11114135
51.50053885     -0.11094555
51.50085609     -0.11072829
51.50109735     -0.11061162
51.50132443     -0.11054724
51.50160827     -0.11052578
51.50174435     -0.11048287
51.50185371     -0.11040777
51.50200899     -0.1102294
51.50218096     -0.10991424
51.50235043     -0.10951861
51.50261506     -0.10873407
51.50284881     -0.10804206
51.50305        -0.10740772
51.5030717      -0.10729775
51.50320527     -0.10689139
51.50337306     -0.10640725
51.50340813     -0.10628253
51.5036018      -0.10536924
51.50370698     -0.10499373
51.50376291     -0.10474026
51.50376583     -0.1047121
51.50376333     -0.10467119
51.50374663     -0.10458469
51.50356298     -0.10458737
51.50334635     -0.10459274
51.50260672     -0.1046209
51.50251572     -0.10461688
51.5022227      -0.10462761
51.50189796     -0.10459676
51.50175353     -0.10461554
51.50147553     -0.10466248
51.50095627     -0.10468527
51.50058977     -0.10469064
51.50039943     -0.10467589
51.49902858     -0.10473221
51.49892088     -0.10472685
51.49889751     -0.10462694
51.49886954     -0.10456122
51.49879106     -0.104466
51.49874013     -0.10444119
51.49870966     -0.10443449
51.49867877     -0.10443248
51.49864788     -0.10443717
51.49861782     -0.10445192
51.49858443     -0.1044546
51.49855813     -0.10444656
51.49849468     -0.10441303
51.49834857     -0.10431379
51.49777667     -0.10360837
51.49697099     -0.1026012
51.49614942     -0.10152295
51.49600498     -0.10134056
51.49571442     -0.10100528
51.49571192     -0.10099657
51.49568228     -0.10096103
51.49566558     -0.10092817
51.49565264     -0.10086916 
51.49564554     -0.10079071
51.49563218     -0.10049432
51.49561089     -0.10034613
51.49555286     -0.10014765
51.49548231     -0.10003634
51.49514625     -0.09992704
51.49508363     -0.09991228
51.49504981     -0.09991631
51.49502184     -0.09993039
51.49499345     -0.0999619
51.49478471     -0.10047689
51.49477303     -0.10056406
51.49477219     -0.10061905
51.49478388     -0.10070353
51.4949851      -0.10117158
51.49527733     -0.10182738
51.49543013     -0.1023075
51.4955967      -0.10293178
51.49573446     -0.10340653
51.49591857     -0.104006
51.49614024     -0.10471612
51.49636358     -0.10537728
51.49647588     -0.10570653
51.49655102     -0.10591037
51.49680818     -0.10646425
51.49703527     -0.10692827
51.49718389     -0.10722331
51.49724567     -0.10737888
51.49729159     -0.10750428
51.4973254      -0.10765247
51.49733        -0.1077155
51.49732749     -0.10775842
51.49733167     -0.10782949
51.49720894     -0.10850273
51.49714715     -0.10867305
51.49685827     -0.10986529
51.49652681     -0.11124663

51.50177649     -0.05012445     426
51.50210374     -0.05050666     429
51.50253617     -0.0509908      436
51.50265346     -0.05072191     443
51.50274404     -0.05055025
51.50301702     -0.05011841
51.503242       -0.04975095
51.50362768     -0.04908308
51.50409182     -0.04814565
51.50424291     -0.04773125
51.50437147     -0.0473316
51.50446955     -0.04699029
51.50449084     -0.04689507
51.50460103     -0.04640624
51.50465362     -0.04618093
51.50527135     -0.04514828
51.50535983     -0.04511878
51.50566035     -0.04506781
51.50588239     -0.04475668
51.50633816     -0.04415586
51.50670044     -0.04277721
51.50676471     -0.04252575
51.50684818     -0.04212744
51.50690328     -0.04173316
51.50692748     -0.04136502
51.50694835     -0.04037864
51.50692999     -0.03981739
51.50689117     -0.0390315
51.50687072     -0.03860503
51.50672631     -0.03755696
51.50658149     -0.03651693
51.50652598     -0.03614276
51.5064279      -0.03572434
51.50622631     -0.03506183
51.50616495     -0.03487475
51.50611278     -0.03475807
51.50605226     -0.03466084
51.50579474     -0.03435977
51.50577554     -0.03434367
51.50566744     -0.03427662
51.50522293     -0.03414184
51.50495664     -0.03413178
51.50471706     -0.0341586
51.50449835     -0.03419347
51.50445328     -0.03420688
51.5042354      -0.03416396
51.50346907     -0.03374956
51.50327456     -0.03367446
51.50285382     -0.03346659
51.5025416      -0.03336198
51.50246897     -0.03333516
51.50195556     -0.03335796
51.50168591     -0.03337539
51.50139205     -0.03335528
51.50094458     -0.03334991
51.50028338     -0.03336332
51.50020991     -0.03338881
51.50017068     -0.0334277
51.50013812     -0.0335028
51.50008886     -0.03416665
51.49998951     -0.03468163
51.49991688     -0.03483452
51.49985343     -0.03489755
51.49964805     -0.03499008
51.49957459     -0.03506921
51.49964638     -0.03540851
51.49998617     -0.03537096
51.50039024     -0.03537498
51.50086777     -0.03544472
51.50147887     -0.03558017
51.5024314      -0.03577597
51.50331964     -0.03588326
51.50394907     -0.03604553
51.50432973     -0.03622524
51.50455846     -0.03638886
51.50489153     -0.03672547
51.50502426     -0.0369253
51.50520874     -0.03726594
51.505359       -0.03759987
51.50554014     -0.03820471
51.50564031     -0.0388243
51.5056712      -0.03928296
51.50566953     -0.0402271
51.50564616     -0.04049934
51.50559357     -0.04134424
51.50553847     -0.04186727
51.50532978     -0.04298575
51.50514947     -0.04372202
51.50479303     -0.04486061
51.50411185     -0.04632242
51.50325285     -0.04770778
51.50260672     -0.04881553
51.50207577     -0.04964702
51.50185037     -0.04999302

This is probably an easy question for lots of people out there, but if you can help me in any itd be great, cheers

Dan
  • 2,020
  • 5
  • 32
  • 55
  • What would the three arraylists for a file hold ? – Adarsh Feb 20 '14 at 12:12
  • No because that question is about creating and adding elements manually.I want to create 3 ArrayLists, 1 for each column and for that that data to be added as it comes in – Dan Feb 20 '14 at 12:14
  • @Adarsh The three arraylists would hold one column of the results printed. SO arraylist 1 will hold all of the data on the left while arraylist 2 will hold all of the data in the middle column and arraylist 3 holds all of the data on the right column – Dan Feb 20 '14 at 12:16
  • That question should give you the basics of how to create, add to, and get content from an `ArrayList`, which should be a good enough starting point (if you already know all this, you should indicate that by attempting to use this knowledge to solve your problem, showing your attempt in the question). Trust me when I say, if you ever want to be a programmer, or possibly even pass whatever course you're taking, it wouldn't help you a whole lot to just have your code written for you. – Bernhard Barker Feb 20 '14 at 12:24
  • 1
    I am not looking to have my code written for me, whats the point in that? I am just trying to get information on where to go next, as I am completely stuck at the moment – Dan Feb 20 '14 at 12:27
  • @Dukeling, the question that you linked to is *not* the same as this question. Please remove your close vote in case reviewers close this question as a duplicate without looking (as some appear to have done already). – Sheridan Feb 20 '14 at 13:29
  • @Sheridan As it stands, this question appears to be asking about the basics regarding how to use an ArrayList, which is exactly what the linked question addresses, which is why I think it's a duplicate (and I certainly won't remove my close vote if I think the question should be closed and, if I didn't, I wouldn't need prompting to do so). What makes you think the others voting to close don't agree with my reasoning, or think the question should be closed for another reason (unclear or too broad comes to mind), but think closing it as a duplicate of a fairly related question is better? – Bernhard Barker Feb 20 '14 at 13:50

6 Answers6

2

GOOD LORD STOP

If you get to a point where you're managing 9 freaking ArrayList objects for something as simple as reading from a file, then you've gone wrong in your design. You want 3 ArrayLists per file? No no, let's take the Object oriented approach.

Example

public class Position
{
    private String longitude;
    private String latitude;
    private String id;

    public Position(String longitude, String latitude, String id) {
        this.longitude = longitude;
        this.latitude = latitude;
        this.id = id;
    }
}

And when you're reading your values, create an ArrayList of type Position, and populate it.

String longitude, latitude, id;
List<Position> positions = new ArrayList<Position>();
// Grab those values from their files

positions.add(new Position(logitude, latitude, id));

Now you've got a more manageable, but still improvable, 3 ArrayList objects; one per file.

christopher
  • 26,815
  • 5
  • 55
  • 89
  • I'm not sure that Device is the right object to model - perhaps Position? As each position can have multiple devices at it. Of course, it depends on the usage of this information. – selig Feb 20 '14 at 12:22
  • Agreed. I was more trying to convey the concept that an OOP approach would greatly solve this, but I'll change the object name. – christopher Feb 20 '14 at 12:24
  • Ha I was thinking 9 was a bit much myself. Basically what I wanted to do was get the deviceId to travel along those coordinates, hence splitting them up into different arraylists. But with your method, could I call a deviceId to follow a sequence of specific coordinates? – Dan Feb 20 '14 at 12:43
  • With my method, you can access the exact same data and do what you want with it. This method just maps an id to a longitude and latitude, which is what I suspected was your intention. Was it? – christopher Feb 20 '14 at 14:30
  • Yup thats exactly it, cheers for the help – Dan Feb 20 '14 at 14:39
  • No problem. If you found this answer most helpful, please mark it as correct. – christopher Feb 20 '14 at 14:39
0

You can create an ArrayList to hold every line of every file, or create 3 differents array list for files.

Something like this:

ArrayList<String> arrayList = new ArrayList<String>();

Then to add something in the ArrayList you should use add method.

while(scannerA.hasNextLine())
{
    String line = scannerA.nextLine();
    arrayList.add(line);
    System.out.println(line);
    lineNumber ++;
}

By reading comments, i understand you want to use 3 differents ArrayList. The code is the same, just add 3 ArrayList. And in the while cycle you should use one time arrayList1, second cycle arrayList2 etc.

I used String because you use it in your code, you should change it if you need a different type.

Marco Acierno
  • 14,682
  • 8
  • 43
  • 53
0

Your Id list would need to be a list of lists as you can have more than one id.

So for each file you would have

List<Integer> LongList = new ArrayList<>;
List<Integer> LatList = new ArrayList<>;
List<List<Integer>> IdList = new ArrayList<>;

Split each line on the tab i.e.

String[] parts = line.split(\t);

Then add each part to the right 'ArrayList' i.e.

LongList.add(Double.parseDouble(parts[0]));
LatList.add(Double.parseDouble(parts[1]));
List<Integer> ids = new ArrayList<>();
IdList.add(ids);
for(int i =2;i<parts.length;i++)
    ids.add(Integer.parseInt(parts[i]));

You could then iterate over them together. Where you have no ids you would have an empty list in IdList.

selig
  • 4,834
  • 1
  • 20
  • 37
0

How to use an ArrayList

Declaring an arraylist

Your ArrayList will most likely be filled with doubles. So to declare an ArrayList that you can fill with doubles you, type this:

ArrayList<Double> list = new ArrayList<Double>();

This basically just says that I want to create an arraylist and it will be full of Doubles

Adding items to an arraylist

This is simple.

just do list.add(Double)

And Marco just beat me to it. I'm not deleting this so I'm going to post it anyway.

Shaun Wild
  • 1,237
  • 3
  • 17
  • 34
0

You want to use 3 ArrayLists for storing each of the data from a line for a file? It's not clear why you would do that, since from your output i see that the last column can be empty for some lines. I suggest creating a class for storing the data from a line and parsing each line to a new instance of the class that will be added to your array. You may use Double for latitude and longitude and parse the String before storing the result in the ArrayList.

public final class FileLine {

private final String latitude;
private final String longitude;
private final String[] deviceID;

public FileLine(final String latitude, final String longitude, final String[] deviceID) {
    this.latitude = latitude;
    this.longitude = longitude;
    this.deviceID = deviceID;
}

public String getLatitude() {
    return latitude;
}

public String getLongitude() {
    return longitude;
}

public String[] getDeviceID() {
    return deviceID;
}

}

    final ArrayList<FileLine> file1Lines = new ArrayList<FileLine>();
    while(scannerA.hasNextLine())
    {
        final String[] line = scannerA.nextLine().split(" ");                        
        if (line.length > 2) {
            file1Lines.add(new FileLine(line[0], line[1], Arrays.copyOfRange(line, 2, line.length - 1)));
        } else if (line.length == 2) {
            file1Lines.add(new FileLine(line[0], line[1], null));
        } else if (line.length == 1) {
            file1Lines.add(new FileLine(line[0], null, null));
        }
    }
Slimu
  • 2,301
  • 1
  • 22
  • 28
  • 1
    A file line may have multiple device ids or zero. – selig Feb 20 '14 at 12:26
  • In this case just change the way the devices are stored to an array. Also use Arrays.copyOfRange() to copy all the items, if the split length is greater than 2 (there is at least one deviceID). – Slimu Feb 20 '14 at 12:29
  • I want to create a file that will make the devices with the ID's listed in the right most column to move around those coordinates. So at most I will have 4 devices moving around 1 set of coordinates. I presume an arraylist is the best possible way to go about this – Dan Feb 20 '14 at 12:30
  • ArrayLists are resizable, so they should do your job just fine. If you need to make optimizations due to lack of resources, you can go with an Array, but you must know before the maximum number of lines. You can even go without any collection if you decide to process each move event right after you read a line from a file. – Slimu Feb 20 '14 at 12:42
  • Another thing that you should clarify are the character(s) used to separate the values. The code above assumes you are using one " " (space) character for separating them. If you decide to another value, replace the one set as an argument to the split method. – Slimu Feb 20 '14 at 12:49
0

You define an array list of Strings using the below line,

List<String> list = new ArrayList<String>();

Now your line has two or three values separated by spaces. You can use the spaces as delimiters and split them using

 while(scannerA.hasNextLine())
  {
    String line = scannerA.nextLine();
    String[] values = line.split("    ");        
  }

And iterate through the String[] to fetch values and add each of those values into three Arrays created using the above given line.

Adarsh
  • 3,613
  • 2
  • 21
  • 37