int [] wronglist = new int [incorrect];   
			
			
for (int i = 0; i < 6; i++){
				
   for (int j = 0; j < i; j++){
					
      if (studentArray[i] != ansArray[i])
						
          wronglist[i] = (j+1);

ok i'm trying to write a program that figures out what number did the user got wrong on a test.

for example, the user inputs 6 letters for 6 questions on a multiple choice test.
the program have find out which number they got wrong if any.

studentArray is the student's answers array and ansArray is the correct answers.
i am very confused on the logic right now.

what i am trying to do is if one the answers does not match, then that index will be added to 1, and be added to a new array (wronglist). the code is wrong, but i can't figure out how to fix it.

Dani AI

Generated

Quick summary and likely causes

The "[I@14318bb" print problem that pointed out is the classic “array identity string” symptom; that was fixed earlier in the thread. The remaining crash (IndexOutOfBoundsException raised when writing into the wrong-list array) almost always comes from one of these: the precomputed capacity (the incorrect count) does not match how many mismatches are actually recorded, the write-index (j) is not reset or is allowed to grow past the array length, or different scopes/variables are being mixed (e.g., passing the wrong incorrect into the method).

Focused troubleshooting steps

  • Log the values at the start of the method and inside the mismatch branch: print the incoming incorrect value, the list/array length, and the current i and j before any write. That will show whether j ever reaches or exceeds the allocated size.
  • Ensure j is initialized inside the method that fills the array (not left over from another call).
  • Don’t allocate and then immediately overwrite the same array unnecessarily; either allocate in the caller and fill it, or let the method build and return a new collection.

Safer alternative (no pre-count required)

Use a dynamic collection while collecting missed question numbers, then convert to an array if needed:

List<Integer> missed = new ArrayList<>();
for (int i = 0; i < student.length; i++) {
    if (!answersMatchAt(i)) missed.add(i + 1);
}
int[] wronglist = missed.stream().mapToInt(Integer::intValue).toArray();

Notes and pitfalls to avoid

Mixing up local versus outer incorrect values, reusing j across calls, or assuming a computed count is correct are the most common causes. ’s index approach can work but must ensure the index and capacity agree; ’s suggestion to inspect array contents (rather than the object identity) is helpful when verifying results.

Recommended Answers

All 14 Replies

for (int i = 0; i < 6; i++){
				
   if (studentArray[i] != ansArray[i])
					
      for(int j = 0; j < incorrect; j++){
						
          wronglist[j] = (i+1);

ok here is a better solution, i think. i figure since j will be only affected if the first loop finds a wrong answer, but since j will be incremented until it hits its limited is a problem.

I dont get your question completely, but what if it is done differently like:

int wrong=0;
int index=0;

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


if (studentArray != ansArray)
{
wronglist[index]=wrong;
wrong++;
index++;
}

See if it's works.

There is one catch in code please initialize

int wrong=1;

i think your code is trying to find out how many questions that the user got wrong. i'm trying to find out which numbers specially that they got wrong.

ok here's what i got today. i already wrote a part that calculates the number of wrong answers that the user has.

i used that variable(incorrect), and put it in the array index size.

int [] wronglist = new int [incorrect];

i wrote this part today, but it didn't work. when i print wronglist, it gives me a bunch of crap.

int j = 0;
			
int [] wronglist = new int [incorrect];
			
for (int i = 0; i < 6; i++){
				
if (studentArray[i] != ansArray[i]){
					
wronglist[j] = (i + 1);
					
j++;
					
					
}
											
					
}

What kind of crap exactly? Code looks sensible to me

I@14318bb
[I@14318bb
[I@14318bb

stuff like this...

here is the part that prints the array:

else if(result && correct < 6){
			
System.out.println("the questions you got wrong: ");
			
			
int [] wrong1 = new int [incorrect];
			
wrong1 = questionsMissed(studentAs, answers, incorrect); 
//method leads back to the part where it figures which numbers are wrong//
			
for (int i = 0; i < incorrect; i++){
				
System.out.println(wrong1);
			
}
		
}

OK, you're trying to print the whole array on line 12, so all you get is the internal pointer to that array. I guess you intended to say System.out.println(wrong1); ?

that's it!!! oh crap, how did i forget!!! thanks!!!!!

Or to see the whole array, use the Arrays.toString() method to print out the elements on one line enclosed in []s

Nice point Norm
Mark thread as solved?

my program is giving me an out of bounds error if i fail the test.

else{
			System.out.println("you've failed");
			
			
			System.out.println("the questions you got wrong: ");
			int [] wronglist = new int [incorrect];
			wronglist = questionsMissed(studentAs, answers, incorrect);
			for (int i = 0; i < incorrect; i++){
			System.out.println(wronglist[i]);
			}


		}

the error is pointing at
wronglist[j] = (i+1);

if i get more than 3 questions wrong, then the error would pop.

i indented the System.out.print after the for loop, and still gets the error.

even if i get 1 question wrong, it would give me:

6 //number that i actually got wrong.
0
0
0
0
0

if i get 3 wrong, then it'll print correctly,

Without the current version of all the relevant code there's nothing that I can do here. Also, I'm about to go out, so I'll be offline till tomorrow. Just debug it one step at a time - put loads of print statements into each step of the code until one of them prints out a wrong/unexpected value. Never assume you don't need to print a value because you know what it is - if your understanding of the code was 100% perfect it would work, wouldn't it?
It may be tedious, but it always works. Good luck. J

thanks. that helped! it's working now.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.