Reputation: 999
I am learning about object oriented concepts right now. I wrote a simple class to accept user input scores but I am getting an out of bounds exception and I'm not sure why! I don't see why this would be accessing indexes over than 4? Here is the code:
The HighScores class which I am instantiating 5 objects into an array:
public class HighScores
{
String name;
int score;
public HighScores()
{
this.name = "";
this.score = 0;
}
public HighScores(String name, int score)
{
this.name = name;
this.score = score;
}
void setName(String name)
{
this.name = name;
}
String getName()
{
return this.name;
}
void setScore(int score)
{
this.score = score;
}
int getScore()
{
return this.score;
}
}
The program manipulating HighScore objects:
import java.util.Scanner;
public class HighScoresProgram
{
public static void main(String[] args)
{
HighScores[] highScoreObjArr = new HighScores[5];
for (int i = 0; i < highScoreObjArr.length; i++)
{
highScoreObjArr[i] = new HighScores();
}
initialize(highScoreObjArr);
sort(highScoreObjArr);
display(highScoreObjArr);
}
public static void initialize(HighScores[] scores)
{
Scanner keyboard = new Scanner(System.in);
for(int i = 0; i < scores.length; i++)
{
System.out.println("Enter the name for for score #" + (i+1) + ": ");
String temp = keyboard.next();
scores[i].setName(temp);
System.out.println("Enter the the score for score #" + (i+1) + ": ");
scores[i].setScore(keyboard.nextInt());
}
}
public static void sort(HighScores[] scores)
{
for(int i = 0; i < scores.length; i++)
{
int smallest = i;
for (int j = i; i < scores.length; i++)
{
if (scores[j].getScore() < scores[smallest].getScore())
smallest = j;
}
HighScores temp = scores[i];
HighScores swap = scores[smallest]; //This is where I'm getting the out of bounds exception.
scores[i] = swap;
scores[smallest] = temp;
}
}
public static void display(HighScores[] scores)
{
System.out.println("Top Scorers: ");
for(int i = 0; i < scores.length; i++)
{
System.out.println(scores[i].getName() + ": " + scores[i].getScore());
}
}
}
Upvotes: 1
Views: 129
Reputation: 319
for (int j = i; i < scores.length; i++)
You're incrementing i instead of j here.
Upvotes: 2
Reputation: 880
I think that the problem is when the loop ends it means that i
is no longer smaller then scores.length
. which means that you are basically checking the of bound when you exit the loop below the line:
for (int j = i; i < scores.length; i++)
Upvotes: 2
Reputation: 2155
The problem here is that you are incrementing same vairable i
in outer and inner loop. Your outer loop runs 4 times but inner loop increments the value if i
further. Use a different variable in inner for loop
Upvotes: 0
Reputation: 5526
i guess below line is issue
for (int j = i; i < scores.length; i++)
try updating sort function as below
public static void sort(HighScores[] scores)
{
for(int i = 0; i < scores.length; i++)
{
int smallest = i;
for (int j = i; j < scores.length; j++)
{
if (scores[j].getScore() < scores[smallest].getScore())
smallest = j;
}
HighScores temp = scores[i];
HighScores swap = scores[smallest]; //This is where I'm getting the out of bounds exception.
scores[i] = swap;
scores[smallest] = temp;
}
}
Upvotes: 5