简体   繁体   中英

Splitting String doesn't work. (value of local variable not used)

I want to read Data from an csv file. This is my code:

    public void readCSV(String path) {
    monologList = new ArrayList<Monolog>();
    String row = new String();

    try {
        br = new BufferedReader(new FileReader(path));
        try {

            while ((row = br.readLine()) != null) {
                String[] splitted = row.split("%");
            }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

I can run the code, but it won't save any data to splitted[]. splitted[] won't even show up during debugging. Eclipse tells me : " The value of the local variable splitted is not used "

I use the same code in an other project, to read Data from "/proc/net/arp" (Linux/Android) and it works with no problems.

Any Ideas?

Sincerly, Wolfen

EDIT: SOLVED. Since I didn't used splitted after it got initialized, eclipse just removed it during debugging.

1. Keep the scope of String[] splitted Outside the while loop .

2. String[] splitted is not able to persist the value outside the while block, cause its scope gets over.

debuggers tend to not save data about variables that are initiated but never called. adding a print on that data in the next line will solve the problem.

Your variable splitted has been declared and defined both within that while loop. Declare it outside. Because of it not being so, it is being limited to while loop only, so can't be found outside it. Do it like so

String[] splitted;
int i = 0;
while ((row = br.readLine()) != null) {
     splitted[i++] = row.split("%");
}

You should try something like:

List<String> myList = new ArrayList<String>();
while ((row = br.readLine()) != null) {
                myList.addAll(Arrays.asList(row.split("%")));
            }

then later, you can exploit the content of your list.

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