0

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]);
            }
    }
}
1

5 Answers 5

1

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));
Sign up to request clarification or add additional context in comments.

Comments

1

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));
        }

2 Comments

OK, just one more thing. This only allows me to enter 2 names. Once I enter 2 names it skips to the second for loop immediately without allowing me to enter the third name.
I don't know what your "EasyIn.getStrig()" function is doing, but it is definitely called three times. You should see the output.
1

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.

Comments

1

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));

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.