FAQ: Learn Java: Variables - String

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

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

Learn Java

FAQs on the exercise String

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!

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

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

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!

I don’t understand what this is asking me to do. I can’t remember the concept from the previous screen and it’s really frustrating not being able to learn such a simple language.

2 Likes

I understand your frustration. Learning to code or to program can be very challenging, but don’t worry: that’s the fun of being a Programmer :slight_smile: Keep practicing and making those mistakes. That’s what Programmers do all the time. It’s a lifestyle and practice makes perfect. Making mistakes is how Programmers learn to be good Programmers. Stick with it. So far, so good :slight_smile:

2 Likes

I’m not sure I could commit to a programming ‘lifestyle’, trying to understand this stuff is seriously making me crazy. I just wanted to learn some skills to be more employable. I spend hours each day working on these tutorials, I can’t really commit beyond the time I have :frowning:

I understand. While Java is generally not the first coding language one would want to go with while learning to code, I highly recommend JavaScript: Drawing & Animation as a first coding language. Best wishes!

Thank you for the recommendation, I’m currently doing the Intro to JavaScript course for at least the 3rd time…it’s not easy at all. I’m thinking I may not be suited to programming, i wish I didn’t need it to have a job.

Is there a reason why String is capitalized, but all the other data types are lowercase? This seems like an infuriating inconsistency.

2 Likes

I can understand your frustration my friend, sometimes it feels like giving up but we wont make it if we just give up, I also go through the same but I take a break and start doing it again, I hope you get past this,
Greetings

What is the difference between:

        String greeting = "Hello World";
        String greeting = new String("Hello World");
1 Like

None, other than syntax. Both do the same thing. Some will want you to show intent and the second line will do that. Others want more concise which the first line will do.

2 Likes

It marked my code correct despite my output being without quotes. Also the examples dont use the String in the code but yellow text or the display text. Wouldn’t it want me to use the “String” its asking us to define at the beginning?
Confused…

System.out.println(openingLyrics);

1 Like

image
It seems like they want us to have the quotation marks printed, but it gives me an error when I use the escape sequence /" inside the string’s value. It marks correct when I don’t use the escape sequence and simply write

String openingLyrics = "Yesterday, all my troubles seemed so far away";

Which is strange, but okay…

3 Likes

Yeah, the accepted answer should be:

String openingLyrics = "\"Yesterday, all my troubles seemed so far away\"";

Or it seems like the question is phrased incorrectly.

2 Likes

So yeah, this assignent is bugged, it clearly should accept “” Yesterday… away “” and not without the ".

Post your code. Works for me?

For some reason, this page doesn’t take the way of generating a “new String”…
https://www.codecademy.com/courses/learn-java/lessons/learn-java-variables/exercises/string

String openingLyrics = new String(“Yesterday, all my troubles seemed so far away”);

Java String class provides a lot of methods to perform operations on strings such as compare(), concat(), equals(), split(), length(), replace(), compareTo(), intern(), substring() etc.
I could be wrong, but his site can have overzealous test in some lessons. Sometimes they do so because they want a specific answer.

This is what I got

public class Song {
    public static void main(String[] args) {
        // Create a variable called openingLyrics that holds "Yesterday, all my troubles
        // seemed so far away".
        String openingLyrics = "Yesterday, all my troubles seemed so far away";
        String openingLyrics = new String(“Yesterday, all my troubles seemed so far away”);

        // Call System.out.println() to print out openingLyrics.
        System.out.println(openingLyrics);
    }
}

Its the end of 2021 and this openingLyrics exercise still has the wrong accepted answer, how about this gets fixed or explain what we are missing?

1 Like

Could I get some help with what I’m doing wrong here? I heavily assume it’s something very basic that I’ve missed as I started today, but I’ve been stuck on this one for like an hour trying to figure it out. I’m a new user as stated earlier so I can only post one image. I posted the one with the print line, but without it, it still shows it’s incorrect.

Okay, so there’s an error in the answer, I just found the “check solution” button, I feel like an idiot. In the “correct” answer, they want it without quoatation marks… That was a waste of time…

“Correct” solution

public class Song {

public static void main(String args) {

String openingLyrics = "Yesterday, all my troubles seemed so far away";



System.out.println(openingLyrics);

}

}