简体   繁体   中英

I'm trying to use 2 user inputs to populate a 2d list array

I'm trying to populate a 2d list array using 2 user inputs.

Problem I'm having is that in the code below, the 1st for statement isn't producing the outcome I'm expecting, the 2nd for is doing what is needed. Also, with the code below I'm unable to close scanner.

public static void main(String[] args) {

    ArrayList<String> listCon = new ArrayList<String>();
    ArrayList<String> listCol = new ArrayList<String>();

    Scanner txtInput = new Scanner(System.in);

    char addTo = 'y';

    do {

        System.out.println("\nCurrent list is " + listCon + listCol + "\n");

        System.out.println("Would you like to add a country to the list?\n\t"
                            + "( y ) = YES\n\t( n ) = NO");

        addTo = txtInput.next().toLowerCase().charAt(0);

        if (addTo == 'y') {

            System.out.println("Enter country name: ");

            listCon.add(txtInput.next().toLowerCase());

            System.out.println("Enter colour: ");

            listCol.add(txtInput.next().toLowerCase()); 

        } else if (addTo == 'n') { 

            int i = 1;

            int countCon = listCon.size();

            if(countCon == 0) {

                System.out.println("No countries have been entered.");

            } else {

                String str = "country";

                if(countCon > 1) {  

                    str = "countries";
                }

                System.out.println("Thankyou for your input.  We found " + countCon + " " + 
                                    str + " in the list.");

                System.out.println("Listed " + str + ":\n");

                for(String n : listCon) {

                    char[] conDigit = n.toCharArray();

                    conDigit[0] = Character.toUpperCase(conDigit[0]);

                    n = new String(conDigit);

                    for(String b : listCol) {

                        char[] colDigit = b.toCharArray();

                        colDigit[0] = Character.toUpperCase(colDigit[0]);

                        b = new String(colDigit);

                        System.out.println("Country " + i + " : " + n + " - \t" + b);

                        i = i + 1;

                    }
                    break;  
                }
                break;
            }  
        } else { 
            System.out.println("Incorrect input detected.  please try again. \n");              
        }
    } while (true);
} 
      } 

You need to remove extra break from the first for loop to iterate. Otherwise, you break after first iteration.

for(String n : listCon) {
....
    for(String b : listCol) {
    ...
    }
    break;  //remove this! 
}
break;

EDIT

The result im after is Country 1 : France - Blue Country 2 : UK - White Country 3 : Ireland - Green

You need to iterate like this:

for (int i = 0; i < listCon.size() && i < listCol.size(); i++) {
    String n = listCon.get(i);
    char[] conDigit = n.toCharArray();
    conDigit[0] = Character.toUpperCase(conDigit[0]);
    n = new String(conDigit);

    String b = listCol.get(i);
    char[] colDigit = b.toCharArray();
    colDigit[0] = Character.toUpperCase(colDigit[0]);
    b = new String(colDigit);

    System.out.println("Country " + i + " : " + n + " - \t" + b);
}

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