Trying the learn java website, already stuck

utahraptor

Golden Member
Apr 26, 2004
1,078
282
136
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?
 

Ken g6

Programming Moderator, Elite Member
Moderator
Dec 11, 1999
16,836
4,815
75
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.
 

utahraptor

Golden Member
Apr 26, 2004
1,078
282
136
I was thinking the lesson indicated that numbers would be automatically converted to strings when concatenation was underway.
 

Ken g6

Programming Moderator, Elite Member
Moderator
Dec 11, 1999
16,836
4,815
75
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:

utahraptor

Golden Member
Apr 26, 2004
1,078
282
136
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