I can't figure this out.. Help me!


#1

I don’t know how to stop it from concatenating the strings when I try to print them inside a sentence.
Heres the code:

public void run()
    {
        String First = readLine("First string? ");
        String Second = readLine("Second string? ");
        String Third = readLine("Third String? ");
        String Combine = First + Second;
        
        
        if(Combine.equals(Third))
        {
            System.out.println(First + Second + " is equal to " + Third + "!");
        }
        else
        {
            System.out.println(First + Second + " is not equal to " + Third + "!");
        }
        
    }

#2

What do you expect to happen?
What actually happens?


#3

You give it two words; example:
String one = pepper
String two = mint

Then on the third string, you can either input something random or, give it the concatenated string.

String three = peppermint

It will then check to see if you entered the correct concatenated answer, and print it out.
What I can’t figure out is how to print the sentences.

Example Answer:

First string? pepper
Second string? mint
Third string? peppermint

then it will print:
pepper + mint is equal to peppermint!


#4

This is what you want:

public void run()
    {
        String First = readLine("First string? ");
        String Second = readLine("Second string? ");
        String Third = readLine("Third String? ");
        String Combine = First + Second;
        
        
        if(Combine.equals(Third))
        {
            System.out.println(First + " + " +  Second + " is equal to " + Third + "!");
        }
        else
        {
            System.out.println(First + " + " + Second + " is not equal to " + Third + "!");
        }
        
    }

Your problem was that you were concatenating the ‘First’ and ‘Second’ variables together, but expecting them to be printed as “First + Second” rather than as “FirstSecond”. Make sure to keep the spaces before and after the ‘+’ sign in the code, otherwise you will print out “pepper+mint” with no spaces. Hope this helps.


#5

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.