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;










share|improve this question
























  • 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

















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;










share|improve this question
























  • 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















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;










share|improve this question















 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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 12 at 0:31

























asked Nov 12 at 0:09









Seungsoo Im

95




95












  • 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




















  • 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


















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



















active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















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






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































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.




draft saved


draft discarded














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





















































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







Popular posts from this blog

Xamarin.iOS Cant Deploy on Iphone

Glorious Revolution

Dulmage-Mendelsohn matrix decomposition in Python