Reputation: 7
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
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
Reputation: 262534
Without any bounds checking:
for (int i=0; i<arr.length; i++)
System.out.println(arr[i].charAt(i));
Upvotes: 3