user3430778
user3430778

Reputation: 1

string input with arrays

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)

Upvotes: 0

Views: 87

Answers (6)

TIMBERings
TIMBERings

Reputation: 391

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]

Upvotes: 0

gbtimmon
gbtimmon

Reputation: 4322

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.

Upvotes: 0

Ezequiel
Ezequiel

Reputation: 3592

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

Upvotes: 0

blackpanther
blackpanther

Reputation: 11486

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

Upvotes: 0

Shreyos Adikari
Shreyos Adikari

Reputation: 12744

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

Upvotes: 0

rgettman
rgettman

Reputation: 178263

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

Upvotes: 2

Related Questions