0

This java code is not printing what I want it to print.It is printing the last line "The above command is only acceptable "

import java.util.Scanner;

public class Recap1 {

    public static void main(String[] args) {

        Scanner input = new Scanner(System.in);
        System.out.println("Hi ! I am your dumb assistant, Dumbo");
        System.out.println("Tell me your name");
        String YourName = input.nextLine();
        System.out.println("these are a list of commands which can tell me what to do :-");
        String CurrentAffairs = "Tell me the current affairs";
        String Dinner = "Cook my dinner" ; 
        String Marriage ="Will you marry me ?";
        String Name = "What is my name ?";
        String gift = "Buy me a gift";
        System.out.println("Tell me the current affairs ");
        System.out.println("Cook my dinner ");
        System.out.println("Will you marry me ?");
        System.out.println("What is my name ?");
        System.out.println("Buy me a gift");
        System.out.println("now write a command !!!");
        String FirstCommand = input.nextLine();

        if (FirstCommand == CurrentAffairs)
            System.out.println("The Year is 2016" + "You are reading this" + "You are smiling " + "You are stupid");
        else if
            (FirstCommand == Dinner)
            System.out.println("I can only cook roasted human brain covered with melted intestines sprinkled with blood sauce.I need the nearest human availible to me.Will you volunter? ");
        else if
            (FirstCommand == Marriage)
            System.out.println("You are fine but I am afraid you are not of my type");
        else if 
            (FirstCommand == Name)
            System.out.println("Your name must be " + "  " + YourName);
        else if
            (FirstCommand == gift)
            System.out.println(" Give me some money and I will buy a gift for you.Deal ?");
        else
            System.out.println("Only the above commands are acceptable !!!!!!!!!!!!!!");
    }
}
Tom
  • 16,842
  • 17
  • 45
  • 54
  • 1
    Yeah, you can't compare input strings with `==`, use `equals()` instead. – markspace Feb 22 '16 at 18:35
  • Never use that many if loops. Its a bad coding practice. Why don't you take numbers as input and then use a String [] for this. If you need an efficient code, let me know. I will help write an efficient code. – shreshta bm Feb 22 '16 at 18:43
  • You could try the use of a case statement. – DarkJade Feb 22 '16 at 18:47

4 Answers4

1

Yeah, you've got to compare strings using .equals()

if (FirstCommand.equals(CurrentAffairs))
{
    System.out.println( "The Year is 2016" + "You are reading this" + "You are smiling " + "You are stupid" );
}

and you're probably better off having this entire if statement as a switch statement.

switch ( FirstCommand )
{
    case "Tell me the current affairs":
        System.out.println(The Year is 2016" + "You are reading this" + "You are smiling " + "You are stupid);
        break;
    default:
        System.out.println( "Only the above commands are acceptable !!!!!!!!!!!!!!" );
        break;
}
Tom C
  • 804
  • 2
  • 11
  • 24
0

You should use

 if (FirstCommand.equals( CurrentAffairs))

NOTE: for compare two string value need to use equals() instead of == because == compare reference value not a content

Piyush Gupta
  • 2,181
  • 3
  • 13
  • 28
0

Try this its working fine.

public class Recap1 {

public static void main(String[] args) {

    Scanner input = new Scanner(System.in);
    System.out.println("Hi ! I am your dumb assistant, Dumbo");
    System.out.println("Tell me your name");
    String YourName = input.nextLine();
    System.out.println("these are a list of commands which can tell me what to do :-");
    String CurrentAffairs = "Tell me the current affairs";
    String Dinner = "Cook my dinner" ; 
    String Marriage ="Will you marry me ?";
    String Name = "What is my name ?";
    String gift = "Buy me a gift";
    System.out.println("Tell me the current affairs ");
    System.out.println("Cook my dinner ");
    System.out.println("Will you marry me ?");
    System.out.println("What is my name ?");
    System.out.println("Buy me a gift");
    System.out.println("now write a command !!!");
    String FirstCommand = input.nextLine();
    if (FirstCommand.equalsIgnoreCase(CurrentAffairs))
        System.out.println("The Year is 2016" + "You are reading this" + "You are smiling " + "You are stupid");
    else if (FirstCommand.equalsIgnoreCase(Dinner))
        System.out.println("I can only cook roasted human brain covered with melted intestines sprinkled with blood sauce.I need the nearest human availible to me.Will you volunter? ");
    else if (FirstCommand.equalsIgnoreCase(Marriage))
        System.out.println("You are fine but I am afraid you are not of my type");
    else if(FirstCommand.equalsIgnoreCase(Name))
        System.out.println("Your name must be " + "  " + YourName);
    else if (FirstCommand.equalsIgnoreCase(gift))
        System.out.println(" Give me some money and I will buy a gift for you.Deal ?");
    else
        System.out.println("Only the above commands are acceptable !!!!!!!!!!!!!!");
}

}

sudhir
  • 11
  • 2
0

This is an efficient way to accomplish what you are trying to do. Instead of taking the string as an input, you take integer. Makes this simple and error free.

import java.util.Scanner;

public class Recap1 {

public static void main(String[] args) {

    Scanner input = new Scanner(System.in);
    System.out.println("Hi ! I am your dumb assistant, Dumbo");
    System.out.println("Tell me your name");
    String YourName = input.nextLine();
    System.out.println("these are a list of commands which can tell me what to do :-");
    String[] replies = {"The Year is 2016" + "You are reading this" + "You are smiling " + "You are stupid"
    ,"I can only cook roasted human brain covered with melted intestines sprinkled with blood sauce.I need the nearest human availible to me.Will you volunter? ",
    "You are fine but I am afraid you are not of my type","Your name must be ",
    " Give me some money and I will buy a gift for you.Deal ?"};


    System.out.println("1.Tell me the current affairs ");
    System.out.println("2.Cook my dinner ");
    System.out.println("3.Will you marry me ?");
    System.out.println("4.What is my name ?");
    System.out.println("5.Buy me a gift");
    System.out.println("now write a command !!!");
    int FirstCommand = input.nextInt();

   switch(FirstCommand)
   {
    case 1:
    case 2:
    case 3:
    case 5: System.out.println(replies[FirstCommand-1]);
    break;
    case 4 : System.out.println(replies[FirstCommand-1]+" "+YourName);
    break;
    default:
        System.out.println("Only the above commands are acceptable !!!!!!!!!!!!!!");
        break;
   }
}
}
shreshta bm
  • 304
  • 4
  • 11