0

I have an array of strings which are artists and when a user types one of the strings into the console I want it to print out the position of that string in the array; so this is the code I have at the moment

String artist;
int location = 0;
String currentArtist;
String [] myArray = {"Rihanna", 
                     "Cheryl Cole", 
                     "Alexis Jordan", 
                     "Katy Perry", 
                     "Bruno Mars",
                     "Cee Lo Green",
                     "Mike Posner",
                     "Nelly",
                     "Duck Sauce",
                     "The Saturdays"};

System.out.println("Please enter an artists name: ");
Scanner scanner = new Scanner(System.in);

artist = scanner.next();

while (location < myArray.length) {
    location++;   
    currentArtist = myArray[location];
    if (artist == currentArtist) {
        System.out.println(location);
    }
}

But when I run this and enter a name in I get the following error;

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 10 at chartposition.ChartPosition.main(ChartPosition.java:26) Java Result: 1

and line 26 is this;

currentArtist = myArray[location];

I'm not sure where I'm actually going wrong so any help would be appreciated. Thanks in advance!

EDIT 1

I've changed the if statement from this;

if (artist == currentArtist) {
    System.out.println(location);
}

to this;

if (artist.equals(currentArtist)) {
    System.out.println(location);
}

It now prints out the location of the string in the array but still get the same error

3
  • If you used a list instead of an array you could use its indexOf method and do so that in one line. Commented May 23, 2014 at 23:16
  • 4
    You need to increment location++; after calling currentArtist = myArray[location];, otherwise you'll end up with an index that is out of bounds (as the error suggests). Also don't compare String values using ==. Commented May 23, 2014 at 23:17
  • @ZouZou okay thank you that's removed the error, but now it only prints out the location if I type a string that doesn't have a space in the middle any idea why? Commented May 23, 2014 at 23:24

1 Answer 1

2

You are incrementing the counter too early, because of that in the last iteration it has the value myArray.length

Move location++ at the end of the loop, or use a for loop

Sign up to request clarification or add additional context in comments.

3 Comments

okay thank you that's removed the error, but now it only prints out the location if I type a string that doesn't have a space in the middle any idea why?
Yes, because scanner.next() takes the next token (= everything until the next blank) use scanner.nextLine() instead
do you mean scanner.nextLine();?

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.