简体   繁体   中英

Use variables outside while loop

In my java project i will read a csv file. If the file includes a "nr" with the value 10, I want to print the "name" on the screen.

while ((line = reader.readLine()) != null) {
    scanner = new Scanner(line);
    scanner.useDelimiter(";");
    while (scanner.hasNext()) {
        String data = scanner.next();
        if (index == 0)
            nr = Integer.parseInt(data);
            if (nr == 10){
                // ?????
            }
        else if (index == 1)
            name = data;
        else if (index == 2)
            id = data;
        else
            System.out.println("invalid data::" + data);
        index++;
    }
    index = 0;
    forward(nr, name, id);  
}

For example my csv is having "nr" 10, two times:

10;name1;id1
20;name2;id2
10;name3;id3

So i want to print name1 and name3 on the screen, how can i use these variables outside the while loop?

I'm not a Java programmer, but if I have understood the question correctly, you can define a variable outside of the scope of the While loop and assign it from within. See below.

NB In Java, variables can only be used within the scope that they are declared.

ArrayList<String> list = new ArrayList<String>();
while (...) {
    list.add("NameToAdd");
}
for(String name: list){
    System.out.print(name);
};

In java variables can't be used outside of the scope they where defined (unlike Javascript for example)

So you have two options to store them in variables defined outside of the loop, or print them the moment you obtain them and not use the name1 and name3 variables at all.

You can change Input value according to your requirement, here i am using Arralist data:

public static void main(String s[]) {

    String name = "", id = "";
    ArrayList<String> nameList = new ArrayList<String>();
    nameList.add("10;name1;id1");
    nameList.add("20;name2;id2");
    nameList.add("10;name3;id3");
    nameList.add("30;name4;id4");
    nameList.add("10;name5;id5");
    nameList.add("10;name6;id6");

    int index = 0;
    int nr;

    for (String line : nameList) {
        Scanner scanner = new Scanner(line);
        scanner.useDelimiter(";");
        index = 0;
        while (scanner.hasNext()) {
            String data = scanner.next();
            //System.out.println(":: " + data + " Index:: " + index);

            if (index == 0) {
                nr = Integer.parseInt(data);
                index++;
                if (nr == 10) {
                    name = scanner.next();
                    id = scanner.next();
                    System.out.println(nr + ";" + name + ";" + id);
                }
            }

        }

    }
}

Like Tom mentioned before you can do something like:

String str = "";
int nr = 0;
int index = 0;
while ((line = reader.readLine()) != null) {
    scanner = new Scanner(line);
    scanner.useDelimiter(";");
    while (scanner.hasNext()) {
        String data = scanner.next();
        if (index == 0)
            nr = Integer.parseInt(data);
        else if (index == 1)
            name = data;
            if (nr == 10)
                str += data + "\n"  
        else if (index == 2)
            id = data;
        else
            System.out.println("invalid data::" + data);
        index++;
    }
    index = 0;
    forward(nr, name, id);  
}
System.out.println(str);

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