Why does appending a string return Indexoutofbound error? (java)
up vote
-1
down vote
favorite
public String coursesTaking() {
Gpa gpa;
int i = rand.nextInt(12);
int p = rand.nextInt(letterGrades.length);
String grade = letterGrades[p];
String coursesTaken = new String[i];
String strArray = new String[coursesTaken.length];
List<Integer> ints = new ArrayList<>();
if (i==0)
return " ";
for(int j=0; j<i; j++) {
for(int h=0; i<999; i++) {
ints.add(h);
}
Collections.shuffle(ints);
String q = String.format("%03d", ints.get(h));
int k = rand.nextInt(22);
Major m = Major.values()[k];
String c = m.toString();
c = c+q;
String grade = gpa.nextLetterGrade();
coursesTaken[j] = c+grade;
strArray[j] = String.valueOf(coursesTaken[j]) + grade;
}
String str = Arrays.toString(strArray);
str = str.substring(0, str.length()-1).replace("[", "");
str = str.substring(0, str.length()-1).replace("]", ",");
str = str.substring(-1).replace(",", grade);
return str;
}
public class Gpa {
private String letterGrades = {"A+", "A", "A-", "B+", "B", "B-", "C+", "C", "C-", "D+", "D", "D-",
"F"};
private java.util.Random rand = new java.util.Random();
private int h;
public String nextLetterGrade() {
int i = rand.nextInt(letterGrades.length);
String grade = letterGrades[i];
return "," + grade + ",";
}
I have two other methods similar to this except that they do not require the additional string of a letter-grade attached. However, when I run my code, I keep getting an error of Indexoutofbounds.
The int current;
variable is taken from a private field I have
private int counter;
and the counter is initialized from a previous method of public String coursesToTake()
where a random integer from 0-12 is picked and the random integer sets the value for counter for the same instance object of the class that contain these methods.
When I run this method without the gpa addition, it works, and when i print out the gpa separately it works as well. However, when I try to append them together, I keep running into an error.
The specific line that causes the error is c = c+grade;
java string indexoutofboundsexception
|
show 9 more comments
up vote
-1
down vote
favorite
public String coursesTaking() {
Gpa gpa;
int i = rand.nextInt(12);
int p = rand.nextInt(letterGrades.length);
String grade = letterGrades[p];
String coursesTaken = new String[i];
String strArray = new String[coursesTaken.length];
List<Integer> ints = new ArrayList<>();
if (i==0)
return " ";
for(int j=0; j<i; j++) {
for(int h=0; i<999; i++) {
ints.add(h);
}
Collections.shuffle(ints);
String q = String.format("%03d", ints.get(h));
int k = rand.nextInt(22);
Major m = Major.values()[k];
String c = m.toString();
c = c+q;
String grade = gpa.nextLetterGrade();
coursesTaken[j] = c+grade;
strArray[j] = String.valueOf(coursesTaken[j]) + grade;
}
String str = Arrays.toString(strArray);
str = str.substring(0, str.length()-1).replace("[", "");
str = str.substring(0, str.length()-1).replace("]", ",");
str = str.substring(-1).replace(",", grade);
return str;
}
public class Gpa {
private String letterGrades = {"A+", "A", "A-", "B+", "B", "B-", "C+", "C", "C-", "D+", "D", "D-",
"F"};
private java.util.Random rand = new java.util.Random();
private int h;
public String nextLetterGrade() {
int i = rand.nextInt(letterGrades.length);
String grade = letterGrades[i];
return "," + grade + ",";
}
I have two other methods similar to this except that they do not require the additional string of a letter-grade attached. However, when I run my code, I keep getting an error of Indexoutofbounds.
The int current;
variable is taken from a private field I have
private int counter;
and the counter is initialized from a previous method of public String coursesToTake()
where a random integer from 0-12 is picked and the random integer sets the value for counter for the same instance object of the class that contain these methods.
When I run this method without the gpa addition, it works, and when i print out the gpa separately it works as well. However, when I try to append them together, I keep running into an error.
The specific line that causes the error is c = c+grade;
java string indexoutofboundsexception
what doesMajor.values()
return?
– 4dc0
Nov 12 at 0:13
it returns a 3 letter abbreviation of a major. That is not where the problem lies I am certain, because when I run the code without the gpa, the major works perfectly fine.
– Seungsoo Im
Nov 12 at 0:13
1
Thought 1: Step through your code with a debugger. Thought 2: Please edit this into a MCVE. Thought 3: Exactly which line throws the exception?
– Elliott Frisch
Nov 12 at 0:14
Please show your stack trace show that we can have an idea where this code is failing
– Scary Wombat
Nov 12 at 0:16
1
The specific line that causes the error isc = c+grade;
. I'm very skeptical. Please provide a Minimal, Complete, and Verifiable example.
– shmosel
Nov 12 at 0:19
|
show 9 more comments
up vote
-1
down vote
favorite
up vote
-1
down vote
favorite
public String coursesTaking() {
Gpa gpa;
int i = rand.nextInt(12);
int p = rand.nextInt(letterGrades.length);
String grade = letterGrades[p];
String coursesTaken = new String[i];
String strArray = new String[coursesTaken.length];
List<Integer> ints = new ArrayList<>();
if (i==0)
return " ";
for(int j=0; j<i; j++) {
for(int h=0; i<999; i++) {
ints.add(h);
}
Collections.shuffle(ints);
String q = String.format("%03d", ints.get(h));
int k = rand.nextInt(22);
Major m = Major.values()[k];
String c = m.toString();
c = c+q;
String grade = gpa.nextLetterGrade();
coursesTaken[j] = c+grade;
strArray[j] = String.valueOf(coursesTaken[j]) + grade;
}
String str = Arrays.toString(strArray);
str = str.substring(0, str.length()-1).replace("[", "");
str = str.substring(0, str.length()-1).replace("]", ",");
str = str.substring(-1).replace(",", grade);
return str;
}
public class Gpa {
private String letterGrades = {"A+", "A", "A-", "B+", "B", "B-", "C+", "C", "C-", "D+", "D", "D-",
"F"};
private java.util.Random rand = new java.util.Random();
private int h;
public String nextLetterGrade() {
int i = rand.nextInt(letterGrades.length);
String grade = letterGrades[i];
return "," + grade + ",";
}
I have two other methods similar to this except that they do not require the additional string of a letter-grade attached. However, when I run my code, I keep getting an error of Indexoutofbounds.
The int current;
variable is taken from a private field I have
private int counter;
and the counter is initialized from a previous method of public String coursesToTake()
where a random integer from 0-12 is picked and the random integer sets the value for counter for the same instance object of the class that contain these methods.
When I run this method without the gpa addition, it works, and when i print out the gpa separately it works as well. However, when I try to append them together, I keep running into an error.
The specific line that causes the error is c = c+grade;
java string indexoutofboundsexception
public String coursesTaking() {
Gpa gpa;
int i = rand.nextInt(12);
int p = rand.nextInt(letterGrades.length);
String grade = letterGrades[p];
String coursesTaken = new String[i];
String strArray = new String[coursesTaken.length];
List<Integer> ints = new ArrayList<>();
if (i==0)
return " ";
for(int j=0; j<i; j++) {
for(int h=0; i<999; i++) {
ints.add(h);
}
Collections.shuffle(ints);
String q = String.format("%03d", ints.get(h));
int k = rand.nextInt(22);
Major m = Major.values()[k];
String c = m.toString();
c = c+q;
String grade = gpa.nextLetterGrade();
coursesTaken[j] = c+grade;
strArray[j] = String.valueOf(coursesTaken[j]) + grade;
}
String str = Arrays.toString(strArray);
str = str.substring(0, str.length()-1).replace("[", "");
str = str.substring(0, str.length()-1).replace("]", ",");
str = str.substring(-1).replace(",", grade);
return str;
}
public class Gpa {
private String letterGrades = {"A+", "A", "A-", "B+", "B", "B-", "C+", "C", "C-", "D+", "D", "D-",
"F"};
private java.util.Random rand = new java.util.Random();
private int h;
public String nextLetterGrade() {
int i = rand.nextInt(letterGrades.length);
String grade = letterGrades[i];
return "," + grade + ",";
}
I have two other methods similar to this except that they do not require the additional string of a letter-grade attached. However, when I run my code, I keep getting an error of Indexoutofbounds.
The int current;
variable is taken from a private field I have
private int counter;
and the counter is initialized from a previous method of public String coursesToTake()
where a random integer from 0-12 is picked and the random integer sets the value for counter for the same instance object of the class that contain these methods.
When I run this method without the gpa addition, it works, and when i print out the gpa separately it works as well. However, when I try to append them together, I keep running into an error.
The specific line that causes the error is c = c+grade;
java string indexoutofboundsexception
java string indexoutofboundsexception
edited Nov 12 at 0:31
asked Nov 12 at 0:09
Seungsoo Im
95
95
what doesMajor.values()
return?
– 4dc0
Nov 12 at 0:13
it returns a 3 letter abbreviation of a major. That is not where the problem lies I am certain, because when I run the code without the gpa, the major works perfectly fine.
– Seungsoo Im
Nov 12 at 0:13
1
Thought 1: Step through your code with a debugger. Thought 2: Please edit this into a MCVE. Thought 3: Exactly which line throws the exception?
– Elliott Frisch
Nov 12 at 0:14
Please show your stack trace show that we can have an idea where this code is failing
– Scary Wombat
Nov 12 at 0:16
1
The specific line that causes the error isc = c+grade;
. I'm very skeptical. Please provide a Minimal, Complete, and Verifiable example.
– shmosel
Nov 12 at 0:19
|
show 9 more comments
what doesMajor.values()
return?
– 4dc0
Nov 12 at 0:13
it returns a 3 letter abbreviation of a major. That is not where the problem lies I am certain, because when I run the code without the gpa, the major works perfectly fine.
– Seungsoo Im
Nov 12 at 0:13
1
Thought 1: Step through your code with a debugger. Thought 2: Please edit this into a MCVE. Thought 3: Exactly which line throws the exception?
– Elliott Frisch
Nov 12 at 0:14
Please show your stack trace show that we can have an idea where this code is failing
– Scary Wombat
Nov 12 at 0:16
1
The specific line that causes the error isc = c+grade;
. I'm very skeptical. Please provide a Minimal, Complete, and Verifiable example.
– shmosel
Nov 12 at 0:19
what does
Major.values()
return?– 4dc0
Nov 12 at 0:13
what does
Major.values()
return?– 4dc0
Nov 12 at 0:13
it returns a 3 letter abbreviation of a major. That is not where the problem lies I am certain, because when I run the code without the gpa, the major works perfectly fine.
– Seungsoo Im
Nov 12 at 0:13
it returns a 3 letter abbreviation of a major. That is not where the problem lies I am certain, because when I run the code without the gpa, the major works perfectly fine.
– Seungsoo Im
Nov 12 at 0:13
1
1
Thought 1: Step through your code with a debugger. Thought 2: Please edit this into a MCVE. Thought 3: Exactly which line throws the exception?
– Elliott Frisch
Nov 12 at 0:14
Thought 1: Step through your code with a debugger. Thought 2: Please edit this into a MCVE. Thought 3: Exactly which line throws the exception?
– Elliott Frisch
Nov 12 at 0:14
Please show your stack trace show that we can have an idea where this code is failing
– Scary Wombat
Nov 12 at 0:16
Please show your stack trace show that we can have an idea where this code is failing
– Scary Wombat
Nov 12 at 0:16
1
1
The specific line that causes the error is
c = c+grade;
. I'm very skeptical. Please provide a Minimal, Complete, and Verifiable example.– shmosel
Nov 12 at 0:19
The specific line that causes the error is
c = c+grade;
. I'm very skeptical. Please provide a Minimal, Complete, and Verifiable example.– shmosel
Nov 12 at 0:19
|
show 9 more comments
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53254507%2fwhy-does-appending-a-string-return-indexoutofbound-error-java%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
what does
Major.values()
return?– 4dc0
Nov 12 at 0:13
it returns a 3 letter abbreviation of a major. That is not where the problem lies I am certain, because when I run the code without the gpa, the major works perfectly fine.
– Seungsoo Im
Nov 12 at 0:13
1
Thought 1: Step through your code with a debugger. Thought 2: Please edit this into a MCVE. Thought 3: Exactly which line throws the exception?
– Elliott Frisch
Nov 12 at 0:14
Please show your stack trace show that we can have an idea where this code is failing
– Scary Wombat
Nov 12 at 0:16
1
The specific line that causes the error is
c = c+grade;
. I'm very skeptical. Please provide a Minimal, Complete, and Verifiable example.– shmosel
Nov 12 at 0:19