Array List Insertion, #6


#1

I cannot get past the step 6. Although I do get the temperature 89, I get halted by an error saying "Did you indicate the right index...".

This is my code:

ArrayList<Integer> weeklyTemperatures = new ArrayList<Integer>();
	weeklyTemperatures.add(78);
	weeklyTemperatures.add(67);
	weeklyTemperatures.add(89);
	weeklyTemperatures.add(94);

weeklyTemperatures.add(2, 111);
System.out.println ( weeklyTemperatures.get(3) );

#2

Hi @zanxion72 ,

When I paste your code into the main function of the Temperatures class, it works perfectly. Is it possible that you either have some additional code there, or that you have your statements somewhere other than the main function?


#3

It is in the main and in the output I do get the correct temperature: This is all I have.

import java.util.ArrayList;

public class TemperaturesB {

public static void main(String[] args) {

	ArrayList<Integer> weeklyTemperatures = new ArrayList<Integer>();
	weeklyTemperatures.add(78);
	weeklyTemperatures.add(67);
	weeklyTemperatures.add(89);
	weeklyTemperatures.add(94);

weeklyTemperatures.add(2, 111);
System.out.println ( weeklyTemperatures.get(3) );

}

}


#4

Hi @zanxion72 ,

You have this, which is correct ...

System.out.println ( weeklyTemperatures.get(3) );

... but try taking out the space between println and ( to get this ...

System.out.println( weeklyTemperatures.get(3) );

Then, re-submit. Strange, yes, but it should work.


#5

Funny but it does work!

Thank you!


#6

I'm having this same issue actually. :frowning:

Even though this prints out the correct answer to the console, I still get the same issue as OP.






import java.util.ArrayList;

public class TemperaturesB {

public static void main(String[] args) {

	ArrayList<Integer> weeklyTemperatures = new ArrayList<Integer>();
	weeklyTemperatures.add(78);
	weeklyTemperatures.add(67);
	weeklyTemperatures.add(89);
	weeklyTemperatures.add(94);
	
weeklyTemperatures.add(2, 111);
System.out.println ( weeklyTemperatures.get(3) );

}

}


#7

Several hours later still having the same problem and I came across this website:

http://laxativer.blogspot.ca/2015/11/data-structures.html.

Despite lack of translation, was still able to find the section needed. When I copy and pasted their sample of code:

weeklyTemperatures.add(2,111);
System.out.println( weeklyTemperatures.get(3) );


#8

This works for me and I think after removing some extra spaces in my own code and it works also that the problem was in these empty spaces around the code. You can see them and check them out. Just remove them. It should working now.

weeklyTemperatures.add(2, 111);
System.out.println ( weeklyTemperatures.get(3) );

I removed the space after 2, in the first one and after printin keyword.


#9

Thanks this really helps :smiley: