简体   繁体   中英

How can I use while loop that represents split() and also for loop to display selection number for user?

For loop inside While loop not executing at all. It would work fine without the for loop but had to add the for loop in order to have selection numbers displayed on the screen.(eg 1.2.3)


try
{
    BufferedReader br = new BufferedReader(new FileReader(path));
    while((line = br.readLine()) != null) {
        String[] values = line.split(",");
        for (int i = 0; i < zoo.size(); i++) {
            System.out.println("Select your choice.\nPress" + (i + 1) + ". (4Legged): " + values[0] + ", (insect):" + values[1] + ", (bird): " + values[2] );
        }
    }

The txt file example is:

Monkey, Ant, Crow
Cat, Spider, Chicken
Dog, Grasshopper, Magpie

The desired output is: Select your choice.

Press 1. (4Legged): Monkey, (Insect): Ant, (Bird): Crow

Press 2. (4Legged): Cat, (Insect): Spider, (Bird): Chicken

Press 3. (4Legged): Dog, (Insect): Grasshopper, (Bird): Magpie

try
{
BufferedReader br = new BufferedReader(new FileReader(path));
int index = 1;
while((line = br.readLine()) != null) {
    String[] values = line.split(",");
    
        System.out.println("Select your choice.\nPress" + index + ". (4Legged): " + values[0] + ", (insect):" + values[1] + ", (bird): " + values[2] );
    index ++;
}

just use a flag before while loop and use that in your print statement and increment it after printing the values.

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