0
import java.util.Scanner;
public class Start
{

public static void main(String[] args) 
{
    Scanner reader = new Scanner(System.in);
    String name = null, helloworlduserattempt = null;
    Boolean helloworldcorrect;

    System.out.println("Welcome to the Java Teacher!");
    System.out.println();
    System.out.println("Type in your name");
    name = (reader.nextLine());
    System.out.println("First " + name + ", lets go over some basics. \nAnything you type to the console in Java Teacher will appear like this;");
    System.out.println();
    System.out.println("___________________________________________________________________________________________");
    System.out.println();
    System.out.println("*your printed text here*");
    System.out.println();
    System.out.println("___________________________________________________________________________________________");

    System.out.println("\n\nThe first exercise is called \"hello world\". This is the starting point for most people while learning how to code.\nThis simply entails writting the phrase \"hello world!\" to the console.");
    System.out.println("\nTo do this, we need to type in \"System.out.println(\"Hello World\");\"");
    System.out.println("\nLets have a look at what that actually means. System.out.println basically tells the system to output to the console the following message on a new line.\nYou could type print instead of println, but this would not write to a new line. Also, notice that System has a capital S");


    System.out.println("Now you try! type in what you think will print \"Hello World!\" to the console");

    do 
    {
        helloworlduserattempt = (reader.nextLine());

        String[] array = helloworlduserattempt.split("\""); 
        array[0] = array[0] + "\"";
        array[2] = "\"" + array[2];
        System.out.println(array[0] + array[1] + array [2]);
        if (helloworlduserattempt == "System.out.println(\"Hello World!\");")
        {
           System.out.println("___________________________________________________________________________________________");
    System.out.println("Hello World!");
    System.out.println("___________________________________________________________________________________________");
    System.out.println("Well done " + name  + "! that was perfect");
    helloworldcorrect = true;
        }
    else if (array[0] == "System.out.println(\"" && array[2] == "\");")
    {
    System.out.println("___________________________________________________________________________________________");
    System.out.println(array[1]);
    System.out.println("___________________________________________________________________________________________");
    System.out.println("youve got the code right " + name + ", but the actual text is incorrect");
    helloworldcorrect = false;
    }
    else 
        {
        System.out.println("___________________________________________________________________________________________");
        System.out.println("Error");
        System.out.println("___________________________________________________________________________________________");
        System.out.println("Oops! thats not quite right " + name + ", try again. remember you can look up at what i said earlier!");
        helloworldcorrect = false;
        }
    }
    while (helloworldcorrect = false);
}
}

Every time I input the correct answer (System.out.println("Hello World!");) it says error. I've searched possible solutions, and I've been through the entire code three times now, any help or guidance would be greatly appreciated.

riccardo.cardin
  • 7,971
  • 5
  • 57
  • 106
Caitlin Craik
  • 121
  • 1
  • 2
  • 4
  • 4
    possible duplicate of [How do I compare strings in Java?](http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – Florent Bayle Jul 01 '15 at 14:01
  • You are checking condition with print statement inside if() ! It's wrong – udit043 Jul 01 '15 at 14:01
  • Please read about [string comparison in Java](http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java). If that does not help, provide a [Minimal, Complete, and Verifiable example](http://stackoverflow.com/help/mcve) that demonstrates your problem. – Robin Krahl Jul 01 '15 at 14:02
  • Also, your do-while condition is not a valid expression: you need the == comparison operator, not the = assignment operator – Daniel Brady Jul 01 '15 at 14:17

2 Answers2

2

Strings in java are objects, and hence doing String == String compares if they are the same object, not if they contain the same value. Instead try using string1.equals(string2)

Stig Tore
  • 266
  • 1
  • 13
1

Use equals or equalsIgnoreCase to compare strings in java. Don't use == operator to compare String.

Tamil Maran
  • 289
  • 1
  • 13