FAQ: Learn Java: Arrays - Review

This community-built FAQ covers the “Review” exercise from the lesson “Learn Java: Arrays”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Learn Java

FAQs on the exercise Review

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!
You can also find further discussion and get answers to your questions over in Language Help.

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head to Language Help and Tips and Resources. If you are wanting feedback or inspiration for a project, check out Projects.

Looking for motivation to keep learning? Join our wider discussions in Community

Learn more about how to use this guide.

Found a bug? Report it online, or post in Bug Reporting

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

import java.util.Arrays;

public class Classroom {
  
  public static void main(String[] args){
    // question 1)
    String[] students = {"Sade", "Alexus", "Sam", "Koma"};
    // question 2) 
    double[] mathScores = new double[4];
    // question 3)
    mathScores[0] = 94.5;
    // question 4)
    mathScores[2] = 76.8;
    // question 5)
    System.out.println("The number of students in the class is " + students.length);
  }
}

And the output was:

The number of students in the class is 4

My solution for step 8/8

Hey, what did I do wrong??? I did exactly as the code asked and the code itself didn’t have any errors but the course said I had it wrong and is asking if I did a double which I did! I added the next question steps after to show I know what I’m doing, so yes I have tried it without those code lines and it still didn’t like the way I formatted the double, but I did it exactly as they wanted! am I missing something???

1 Like

Hello @script8303178085 and welcome to the Codecademy Forums!

Take a closer look at your indentation. Remember that only the code inside the main method will be automatically executed.

Additionally, proper formatting usually dictates that we shouldn’t have a space between double and [4].

I would like to share my humoristic extension to this exercise,
which uses more values and some string concatenations:

import java.util.Arrays;

public class Classroom {
  
  public static void main(String[] args){

    String[] students = {"Sade", "Alexus", "Sam", "Koma"};
    double[] mathScores = new double[4];

    mathScores[0] = 94.5;
    mathScores[2] = 76.8;

    System.out.println("The number of students in the class is " + students.length + ".\n");
    System.out.println(students[0] + " got a grade of " + mathScores[0] + "% on the test.");
    System.out.println(students[2] + " got a grade of only " + mathScores[2] + "% on the same test...");
    System.out.println("As for " + students[3] + ", she got a grade of... " + mathScores[3] + "% on the test?!\n");
    System.out.println("Oh, that's right: I forgot to set all the values in the mathScores array '-_-");
  }
}

Happy coding!

1 Like

Hi. This question is neither here nor there but I’d appreciate a reply nonetheless. I have this code…

public class Review 
{
  public static String removeDups(int[] nums) 
  {
    String res = "";
    return Arrays.stream(nums).mapToObj(i -> (i + "")).collect(Collectors.joining(" "));
  }

  public static void main(String[] args) 
  {
     System.out.println(removeDups(new int[]{1, 2, 2, 2, 3, 2, 5, 2, 6, 6, 3, 7, 1, 2, 5}));
  }

}	

The code gives this object reference. How do I get a human-readable string representation of it?

So, the actual problem was only in space?
I had the same issue, I continued till the end and the result in terminal was good, but I had the same error message about completing the task.
When I deleted that space, it was ok.

Yes. There should not be a space between double and the opening square bracket. In order for your code to compile properly, you have to follow this syntax:

double[] myArray = new double[5];

In my previous message, I mentioned that the indentation was incorrect. I was wrong. The indentation in the screenshot, while not best practice, does not have any effect on how the code compiles or runs.

1 Like

I thought spaces have no impact at all.
Thank you very much :slightly_smiling_face:

1 Like

I think my issue is the words the lessons use aren’t consistent in the instructions. For example in this lesson they used words like declaring and initializing to describe what actions to take. Then in the instructions they said I need to create. I find myself feeling dumb often because I misunderstand these subtle differences in communication. Am I the only one?

My final code is

import java.util.Arrays;

public class Classroom {

public static void main(String args){
String students = {“Sade”, “Alexus”, “Sam”, “Koma”};
double mathScores = new double[4];
mathScores[0] = 94.5;
mathScores[2] = 76.8;
System.out.println("The number of students in the class is " + students.length + “.”);
}
}

I was told earlier that String is a class (though, for some reason, Codecademy insists that it’s an object). What is surprising for me is that double is a class too, it turns out! Otherwise, how would I be able to create an object with double[] mathScores = new double[4]; (when you use that new operator, you technically create an object, don’t you?)? Are primitives, in fact, classes?

Why do they have me making an array in main and not in a class using a constructor? I wish these lesson would tell me why I’m sometimes putting lines of code in some places and other times in others.