0
import java.util.Scanner;

public class ProgramAssignment1 {

    public static void main(String[] args) {
        reader();
    }

    public static void reader() {
        Scanner input = new Scanner(System.in);
        System.out.println("Please enter the Number of Students you would like to input for");
        int count = input.nextInt();
        int[] scores = new int[count];
        String[] name = new String[count];
        for (int i = 1; i <= count;i++) {
             System.out.println("Please input the students names ");
            name[i] = input.nextLine();
            System.out.println("What is there score?");
            scores[i] = input.nextInt();
        }

        for (int a = 1; a <= 10; a++) {
            System.out.print(name[a]);
            System.out.print(" "+scores[a]);
        }
        System.out.println();
    }

}

so basically i need user input to an array but it keeps giving me the same error

Example run:

Please enter the Number of Students you would like to input for
2
Please input the students names 
What is there score?
5
Please input the students names 

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 2
    at ProgramAssignment1.reader(ProgramAssignment1.java:18)
    at ProgramAssignment1.main(ProgramAssignment1.java:7)
Java Result: 1
BUILD SUCCESSFUL (total time: 5 seconds)
Joshua Pinter
  • 45,245
  • 23
  • 243
  • 245
  • 2
    Arrays are 0 base indexed. I.e, indices start from 0 to array.length -1 . Read this: http://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html – Alexis C. Mar 17 '14 at 21:39
  • ok it doesnt let me input the string now – user3430778 Mar 17 '14 at 21:49
  • 1
    Use `input.nextLine()` after `input.nextInt()` to consume the '\n'. See also http://stackoverflow.com/questions/7056749/scanner-issue-when-using-nextline-after-nextxxx – Alexis C. Mar 17 '14 at 21:49

6 Answers6

2

In Java, indexes of an array go from 0 through length - 1, not 1 through length. Arrays are 0-based. You're looping one too high, and you're running off the end of your array.

Change

for (int i = 1; i <= count;i++) {

to

for (int i = 0; i < count;i++) {

You'll need to change your a for loop similarly (stop when count is reached, not 10).

rgettman
  • 176,041
  • 30
  • 275
  • 357
0

1. You are iterating for loop to 10.

for (int a = 1; a <= 10; a++)

Suppose the count is less than 10. What will happen? ArrayIndexOutOfBoundsException

2. Change

`for (int i = 1; i <= count;i++)` 

in to

for (int i = 0; i < count;i++) 
Shreyos Adikari
  • 12,348
  • 19
  • 73
  • 82
0

Your problem is that the index starts from 0 not 1.
Change:

for (int i = 1; i <= count;i++) {
    System.out.println("Please input the students names ");
    name[i] = input.nextLine();
    System.out.println("What is there score?");
    scores[i] = input.nextInt();
}

To:

for (int i = 0; i <= count;i++) {
    System.out.println("Please input the students names ");
    name[i] = input.nextLine();
    System.out.println("What is there score?");
    scores[i] = input.nextInt();
}
blackpanther
  • 10,998
  • 11
  • 48
  • 78
0

Arrays start at index 0. Try to iterate in for bucles starting with 0.

Ezequiel
  • 3,477
  • 1
  • 20
  • 28
0

Arrays in Java, like most programming langages Arrays are addressed with an offset.

That means name[1] point to the object 1 past the begining of the name array, becuase the offset is one.

conversely the first element of an array would be 0 past that start of the array or name[0] and the last element will be the length of the array minus one past the begining of the array or name[ name.length - 1 ]

In your code for (int i = 1; i <= count;i++) { goes through the offsets 1,2,3,4,5, but 5 is 5 past the begining of the array, or the sixth element of a 5 element array.

Adjust you code so the loop uses the offsets 0,1,2,3,4 and you should be good to go.

gbtimmon
  • 4,238
  • 1
  • 21
  • 36
0

ZouZou is right. Arrays are 0 based, you are going to want to run your loops from 0 to array.length-1.

For example if I have:

int[] array = {1,2,3,4,5,6,7,8,9,10}
for(int i=0; i<array.length; i++ {
    System.out.println(array[i] + " ") 
}

I will get: 1 2 3 4 5 6 7 8 9 10

If my for loop was from i=1; i<=array.length; i++, I would get: 2 3 4 5 6 7 8 9 10 [OUT OF BOUNDS]

TIMBERings
  • 361
  • 1
  • 4
  • 17