• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

Trying the learn java website, already stuck

utahraptor

Golden Member
Hello,

I am attempting exercise number #2 at: http://www.learnjavaonline.org/en/Variables_and_Types

The exercise at the end is:

Create all the the primitives with different values. Concatenate them into a string and print it to the screen so it will print: H3110 w0r1d 2.0 true

This is the code I have wrote:

Code:
public class Main {
    public static void main(String[] args) {
        byte zero = 0;
        short firstwordnumericfirsthalf = 31;
        int secondwordnonzeronumeric = 1;
        long firstwordnumericsecondhalf = 10;
        float twopointo = 2.0f;
        double auxzero = 0.0f;
        boolean lastword = true;
        char one = 'H';
        char two = 'w';
        char three = 'r';
        char four = 'd';
            
        String output = one + firstwordnumericfirsthalf + firstwordnumericsecondhalf + " " + two + zero + three + secondwordnonzeronumeric + four + " " + twopointo + " " + lastword;
        System.out.println(output);
        
    }
}

My resulting output is: 113 w0r1d 2.0 true

I can't figure out what is breaking down in the first word that is supposed to be: H3110

Any ideas?
 
The "+" operator can add numbers or "add" Strings. What type is a "char" (number or String)? How can you force a number to be a String?

Or you could start "adding" from an empty string ("") instead.
 
I was thinking the lesson indicated that numbers would be automatically converted to strings when concatenation was underway.
 
I was thinking the lesson indicated that numbers would be automatically converted to strings when concatenation was underway.

That's correct. Evaluation of operators of equal precedence (have you gotten to that yet?) proceeds from left to right. Take:

Code:
        String output = 1 + 1 + 1 + " is three";
        System.out.println(output);

What's the first operator operating on? Two numbers. So the evaluation of the expression proceeds as follows:

Code:
1 + 1 + 1 + " is three"
2 + 1 + " is three"
3 + " is three"
"3 is three"

The operator being evaluated in the expression didn't see a String until the last step, so concatenation didn't happen until then.
 
Last edited:
Thanks Ken! I have not learned about operators of equal precedence yet, but by going on what you said I was able to fix the code by adding the portion in red:

Code:
public class Main {
    public static void main(String[] args) {
        byte zero = 0;
        short firstwordnumericfirsthalf = 31;
        int secondwordnonzeronumeric = 1;
        long firstwordnumericsecondhalf = 10;
        float twopointo = 2.0f;
        double auxzero = 0.0f;
        boolean lastword = true;
        char one = 'H';
        char two = 'w';
        char three = 'r';
        char four = 'd';
            
        String output =[COLOR=Red]"" +  [/COLOR]one + firstwordnumericfirsthalf + firstwordnumericsecondhalf + " " + two + zero + three + secondwordnonzeronumeric + four + " " + twopointo + " " + lastword;
        System.out.println(output);
        
    }
}

Output: H3110 w0r1d 2.0 true
 
Back
Top