Ali
Ali

Reputation: 558

Split file input by comma into an array Java, and re-writing to the file

Say I have a file text.txt containing the following on a single line:

abc, def, ghi, jkl

I am trying to get a list of data within the file so myList will result in ["abc", "def", "ghi", "jkl"], but it isn't seem to be working:

String[] tokens;
try {
        Scanner scanner = new Scanner("text.txt");
        while (scanner.hasNext()) { tokens = scanner.nextLine().split(","); }
} catch (IOException e) {
        e.printStackTrace();
}
System.out.println(tokens.length); // Prints 1 instead of 4

And trying to print index 0-3 gives an out of bounds error (obviously because the length is just 1)

I then want to change the data in one of the index (in the array), and re-write/over-write it back to the file. I think this answer should work for that, but only after I can get my array right. https://stackoverflow.com/a/1016291/4669619

Edit: At the moment, I know my file only has 4 things separated by 3 commas, as shown in the sample text file.

Upvotes: 0

Views: 2250

Answers (5)

Ali
Ali

Reputation: 558

Thanks for the valuable input guys, but I believe I have solved my problem:

String[] tokens;
try {
    Scanner scanner = new Scanner("text.txt");
    tokens = scanner.nextLine().split(",");
} catch (IOException e) {
    e.printStackTrace();
}

I will select this as the correct answers if I get a few up votes to show that my solution is indeed (one of the) correct one!

Upvotes: 0

DnR
DnR

Reputation: 3507

Edit: You are not reading the text.txt file. with new Scanner("text.txt") you are using constructor Scanner(String source)`, while what you want is Scanner(File source)

I think this is what you are looking for using ArrayList:

ArrayList<String> tokens = new ArrayList<String>();
try {
        File file = new File("text.txt");
        Scanner scanner = new Scanner(file);
        while (scanner.hasNext()) { 
            String[]  str= scanner.nextLine().split(",");
            for(int i=0;i<str.length;i++){
                tokens.add(str[i]);
            }
        }
} catch (IOException e) {
        e.printStackTrace();
}
System.out.println(tokens.size());

Upvotes: 2

Scary Wombat
Scary Wombat

Reputation: 44834

you are overwriting tokens for each line (I think you have a blank line)

try

String[] tokens;
try {
    scanner = new Scanner(new File ("text.txt"));
    while (scanner.hasNextLine())       // change this
    { 
         tokens = scanner.nextLine().split(","); 
         System.out.println(tokens.length); 
     }
} catch (IOException e) {
    e.printStackTrace();
}

Upvotes: 3

Aakash
Aakash

Reputation: 2109

Your code suggests that your loop will run until the END of file, and return the last line from file. Can you check if this is what you need? Maybe your last line does not contain ",".

Upvotes: 0

jmcg
jmcg

Reputation: 1567

When you declare an array, you need to specify its size. Then you can assign a token into a specific position on your array.

String[] myStringArray = new String[3];
myStringArray[0] = "sample string";

Your problem is you dont really know exactly how many elements your array will contain. So the best solution is to use an ArrayList

ArrayList<String> myStringArrayList = new ArrayList<String>();
myStringArrayList.add("sample string");

try to incorporate that in your code.

Upvotes: -2

Related Questions