Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

My 'for loop' inside another 'for loop' is not working

for (int i=0; i<Intlength; i++){ 
    int intPosition; 
    intPosition=strAlphabet.indexOf(strMessage.charAt(i));
    System.out.println(intPosition); 
    System.out.println("BREAK"); 
    for (int k=0; k<Intlength2; k++){ 
       int intPosition2; 
       intPosition2=strAlphabet.indexOf(strKeyword.charAt(k));
       System.out.println(intPosition2);                
       System.out.println("BREAK-------------"); 
    }
}

i will ask the user to type in two words. one is a message and one is a keyword.

the first loop above will check that if i will add 1, and print out the first letters position number. for example if the message was "red". i would first want it to output the position number of "r" which is 17. then it mmust move to the second loop, and do the exact same for the keyword. for example if the keyword was "cat" i would want it to print the first letter position of the first letter in this case "c" has the position value of 2. in this way i want the output to be as such:

  • first letter position of message

  • first letter position of keyword

  • second letter position of message

  • second letter position of keyword

etc.

therefore sticking to the message "red" and the keyword "cat" i would want the output as such:

17
2
4
0
3
19

i have added in break texts to distinguish what was happening to my coding and this was the result.

Please give me a message:
red
Thank you! Now please give me a keyword:
cat
17
BREAK
2
BREAK-------------
0
BREAK-------------
19
BREAK-------------
4
BREAK
2
BREAK-------------
0
BREAK-------------
19
BREAK-------------
3
BREAK
2
BREAK-------------
0
BREAK-------------
19
BREAK-------------

as you can see it putputs the first letter position of the message, then all three positions of the keyword, and goes onto the second position letter of the message then again outputting all three position values of the keyword. how do i fix this to get the output i want, i am sure that i am not writing the forloop correctly.

like image 876
superstar Avatar asked Feb 24 '26 05:02

superstar


1 Answers

What you've got are nested for loops to make it do what you suggested they just need to be one after the other, not nested inside each other. When you put the second for loop inside the first what you're telling it to do is print each letter in the keyword for every letter in the message.

What you want is a loop like this (It's unclear what you want it to do if one string is longer than the other, I'm assuming you want it to stop with the shorter string but you can change that.)

if(strMessage.length > keyword.length){
    intLength = keyword.length;
} else {
    intLength = strMessage.length;
}

for (int i=0; i<intLength; i++){ 
    //Print the position of the i'th letter of the message
    int intPosition; 
    intPosition=strAlphabet.indexOf(strMessage.charAt(i));
    System.out.println(intPosition); 

    //Print the position of the i'th letter of the keyword
    int intPosition2; 
    intPosition2=strAlphabet.indexOf(strKeyword.charAt(i));
    System.out.println(intPosition2); 

}
like image 180
Andrew Aitken Avatar answered Feb 25 '26 17:02

Andrew Aitken