0

Below is the code.\ I imagine you use a for loop and then another but I cannot seem to make it work. I attempted research however most topics were too complex since I am a novice. I'm trying to find a way to get the fifth character out of each string within the variable. I'll use the information given to me so i can then solve the rest of my program. I have more to do

public static void main (String[]args)
    {
        String[] decoder = {"Nexa2f5", "Z52Bizlm" , "Diskapr" , "emkem9sD", "LaWYrUs", "dAStn78L", "mPTuriye", "aaeeiuUu", "IL8Ctmpn"};
        int character = 4;
        for(int i=0; i<=decoder.length-1; i++)
        {

        }
    }

I am trying to get the third and fourth characters of the odd numbered Strings. I am trying to put the letters into an array and decode the message. I am also trying to print the 5th character of all other words. I'm having issues commenting right and I've tried to reply a couple times but no dice.

  • Do you need to find the string "4" or the asci character represented by 4? If you need to find the location of the string "4", something like http://stackoverflow.com/questions/2275004/in-java-how-to-check-if-a-string-contains-a-substring-ignoring-the-case should work. – aplassard Apr 21 '14 at 00:56

3 Answers3

0

Here is what I would do:

    String[] decoder = {"Nexa2f5", "Z52Bizlm" , "Diskapr" , "emkem9sD", "LaWYrUs", "dAStn78L", "mPTuriye", "aaeeiuUu", "IL8Ctmpn"};
    for (String s: decoder){
        String myChar = s.substring(4, 5);
        // now continue to process myChar
    }
Raf100
  • 1
  • 1
  • 1
0
class test {
    public static void main (String[]args)
    {
        String[] decoder = {"Nexa2f5", "Z52Bizlm" , "Diskapr" , "emkem9sD", "LaWYrUs", "dAStn78L", "mPTuriye", "aaeeiuUu", "IL8Ctmpn"};
        int character = 4;
        for(int i=0; i<=decoder.length-1; i++)
        {
        String temp = decoder[i];
        System.out.println(temp.substring(4,5));
        }
    }
}
jpr
  • 185
  • 3
  • 6
  • 20
0
 for(String s : decoder) {
      for(char c : s.toCharArray()) {
           //loops through ever char of every string
      }
 }

 char letter;
 for(String s : decoder) {
      if(s.length > 4) {
           letter = s.chatAt(5);
      }
 }

Choose your weapon. I'm not completely sure how you want to do this, but this is the format you'd want for looping through variables in an array.

Vince
  • 14,470
  • 7
  • 39
  • 84