kwanda zidane kwanda zidane -4 years ago 65
Java Question

I am trying to get the program to only display months that are starting with the letter J but I can't get it to as it only displays one month

This is part of the code where I try to get the program to display all months starting with the letter J
It only shows the same month three times

String[] jMonths = new String[12];

for(int i = 0; i < months.length; i++) {

for(int j = 0; j < jMonths.length; j++) {

if(months[i].charAt(0) == 'J') {
jMonths[j] = months[i];
}
}
}


String message = jMonths[0]+" " + jMonths[5] +" " + jMonths[6];

JOptionPane.showMessageDialog(null, message);


Please help me! Thank You

Answer Source

you do not require an inner loop

for(int i = 0; i < months.length; i++){
    if(months[i].charAt(0) == 'J') {
        jMonths[i] = months[i];
    }
}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download