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]);
            }
    }
}
Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
Pizzaman
  • 425
  • 5
  • 11
  • 19

5 Answers5

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.

Petr Mensik
  • 26,874
  • 17
  • 90
  • 115
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));
        }
Andi Krusch
  • 1,383
  • 8
  • 6
  • 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. – Pizzaman Apr 01 '14 at 11:43
  • I don't know what your "EasyIn.getStrig()" function is doing, but it is definitely called three times. You should see the output. – Andi Krusch Apr 01 '14 at 11:46
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));
RB-Develop
  • 196
  • 1
  • 9
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));
0

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

INK
  • 235
  • 1
  • 11