0

My program is reading from an external file and putting each line of data into an array list. There are 5 different values on a line in the external file and the values are separated by commas. I just can't figure out how to get that first value in the array list to use in other parts of my code. The first value in the file is the name and that is what I am trying to get to match it with the entry they make for changeName.

System.out.println("Enter the name of the player you would like to change the amount for");
    String changeName = FileUtility.getInput().nextLine();

    Scanner s = new Scanner(new File(FILE_NAME));
    String str = "";
    ArrayList<String> list = new ArrayList<String>();

    while (s.hasNext()) {
        list.add(s.next());
    }
Lino
  • 19,604
  • 6
  • 47
  • 65
  • 1
    I suggest that you familiarize yourself with the [String documentation](https://docs.oracle.com/javase/7/docs/api/java/lang/String.html). You can find functions that will be helpful by browsing this page. – Code-Apprentice Sep 26 '17 at 22:53
  • Are you aware of the `split` method of the `String` class? – Dawood ibn Kareem Sep 26 '17 at 22:57
  • @Code-Apprentice I used str.split and got the error saying string[] cannot be converted to ArrayList used this code list = str.split(","); – Ryan Blanchard Sep 26 '17 at 22:57
  • `str.split(",")` returns Array – Sergei Sirik Sep 26 '17 at 23:00
  • @Sergei Sirik Do you know a way I could read the file into an array and then split it that way? – Ryan Blanchard Sep 26 '17 at 23:01
  • To get a list out of string you can use smth like this `List list = new ArrayList<>(Arrays.asList("asdf,asdf,asd,fa,sdf,as,df,asd".split(",")));` – Sergei Sirik Sep 26 '17 at 23:03
  • Perhaps you need a 2D array, where each line is itself read into an array? Would that help? – Dawood ibn Kareem Sep 26 '17 at 23:05
  • @SergeiSirik I created an array and used to .toArray to insert everything from the array list. now when I go to use the str.split I get an error that a String cannot be converted to a String [] this is my array list List temps = new ArrayList(); String [] tempsArray = temps.toArray(new String[0]); tempsArray = str.split(",")[0]; – Ryan Blanchard Sep 26 '17 at 23:18
  • The quickest solution is to just use an array rather than an ArrayList. – Code-Apprentice Sep 27 '17 at 01:22
  • It seems like your most recent comment is about a new error. If you have not yet posted a new question, you should. – Code-Apprentice Sep 27 '17 at 01:23
  • https://stackoverflow.com/questions/46438127/writing-a-2d-array-to-a-csv-file – Ryan Blanchard Sep 27 '17 at 01:32
  • I don't fully understand what you are asking, but for the first value, you'd access it using list.get(0). If you are struggling to separate values based on the comma, look into creating substrings, and dividing them on commas. [Edit] Could [this](https://stackoverflow.com/questions/30587956/how-to-split-string-before-first-comma) be of use? – Ben Hayward Sep 26 '17 at 22:56

1 Answers1

0

Ok, if I got you right, this will do what you want:

        Scanner s = new Scanner(new File("test.txt"));
        String str = "";
        List<List<String>> list = new ArrayList<List<String>>();

        while (s.hasNext()) {
            list.add(Arrays.asList(s.nextLine().split(",")));
        }

        // then you can acces random element from the list this way
        System.out.println(list.get(0).get(1));

        // or print all values in the list
        for (List<String> elem : list) {
            System.out.println(Arrays.toString(elem.toArray()));
        }

Output:

asdf
[asdf, asdf, asdf, asdf, asdf]
[asdf, asdf, asdf, asdf, asdf]

My test.txt:

asdf,asdf,asdf,asdf,asdf
asdf,asdf,asdf,asdf,asdf
Sergei Sirik
  • 1,249
  • 1
  • 13
  • 28
  • you are a life saver! Exactly what I wanted thank you! – Ryan Blanchard Sep 26 '17 at 23:30
  • just one more question, how would I go about getting the first value in a different line? Right now im doing if(list.get(0).contains(firstValue)){ do stuff} but when I enter the first value on the second line the if statement is false – Ryan Blanchard Sep 26 '17 at 23:36
  • You have a matrix. First index is row num and second column num. So you can access any row like `list.get(0) list.get(1) list.get(2)....` and then you can access any column on this row like `list.get(1).get(0)` – Sergei Sirik Sep 26 '17 at 23:41
  • Okay now how would I edit one of the values in the list.get(0) the [0] of that arraylist has a size of 5 and I need to edit the value in for [4] so basically list [0] [4] if that makes sense. – Ryan Blanchard Sep 26 '17 at 23:49
  • Ok) `list.get(0).set(4, "anyvalue");` But you have to check standard api docs for such basic stuff https://docs.oracle.com/javase/7/docs/api/java/util/List.html – Sergei Sirik Sep 26 '17 at 23:56