user1061354
user1061354

Reputation: 7

How would you print out the character of that string at the index of the string for a given array of Strings

Sorry my question is kind of hard to understand. So in a array such as String []arr = {"abs, "def", "ghi"}; I want to know how it can print out this a e i

this is my code that i have tried to write but failed.

public static void printCharsAt(String[] arr) { 
    System.out.println("how many inputs do you want in your array");
    String array[] = {"hello", "Ap", "CS"};        
    System.out.println("What term of the array do you want, do you wan the first, 2nd, or third");
    tnt char = kb.nextInt(); 
    System.out.println(array[char]);
}

Upvotes: 0

Views: 92

Answers (3)

Jomoos
Jomoos

Reputation: 13083

You have to loop through each String in the array. You may use for each loop in Java or the traditional for loop. Once you get a String, You can use the charAt() method of String class to obtain the character at a particular position. Don't forget the length method also, It may come in handy. No code, since it is homework :)

Upvotes: 2

Artem
Artem

Reputation: 4397

Pay attention on charAt(int index) method

Upvotes: 1

Thilo
Thilo

Reputation: 262534

Without any bounds checking:

for (int i=0; i<arr.length; i++)
    System.out.println(arr[i].charAt(i));

Upvotes: 3

Related Questions