Pizzaman
Pizzaman

Reputation: 425

Access certain location in array of strings

I want to enter in 3 names into the array. Then I want to access the position of the second character in the second name.

First of all I get an out of bounds exception in line 13. Also, the second for loop I know is completely off so excuse that.

Why am I getting the exception and how would I access certain character positions in certain strings?

class Names
{
    public static void main(String[]args)
    {
        int index;
        String names[];
        names = new String [3];

        for(index = 1; index <= names.length; index++)
            {
                System.out.println("Enter name " + index);
                names[index] = EasyIn.getString();
            }
        for(index = 0; index < names.length; index++)
            {
                System.out.println(names[1][2]);
            }
    }
}

Upvotes: 0

Views: 109

Answers (5)

user2575725
user2575725

Reputation:

try this:

in ur first loop:

for(index = 0; index < names.length; index++)

remove ur second loop as don't wana show each element, u can do it directly like this:

System.out.println(names[1].charAt(1));

Upvotes: 1

RB-Develop
RB-Develop

Reputation: 196

A couple of things.

You can make the array of String objects in one go like String[] names = new String[3].

In your first loop you start like this:

for(index = 1; index <= names.length; index++)

That is wrong, you start with your index at 0 unless you want to skip the first element. Then you define index <= names.length;, which is also wrong. The length returns the actual length, but we now know an array starts at 0. You should define it like this:

for(index = 0; index < names.length; index++)

As others have said before, if you want to get a certain char from the string you use `myString.charAt( )', so in your case you should write something like this:

System.out.println(names[<an index>].charAt(2));

Upvotes: 1

Petr Mensik
Petr Mensik

Reputation: 27496

You can create static array and than read the value from it

String[] array = {"Peter", "John", "Andre"}; //now you have three items in array
char secondChar = array[1].charAt(1); //first get second element, than get second char from it

So now secondChar is o - also remember that arrays starts from 0 so that's why index 1 means second item in the array - same applies for charAt, String is just basically array of characters.

Upvotes: 1

Andi Krusch
Andi Krusch

Reputation: 1383

Your code should look like this:

        int index;
        String names[];
        names = new String [3];

        for(index = 0; index < names.length; index++)
        {
            System.out.println("Enter name " + index);
            names[index] = EasyIn.getString();
        }
        for(index = 0; index < names.length; index++)
        {
            if (names[index] != null && names[index].length() > 2)
                System.out.println(names[index].charAt(2));
        }

Upvotes: 1

INK
INK

Reputation: 235

If you want to access a certain character in a string, use mystring.charAt(index)

Upvotes: 0

Related Questions