-2

I am trying to make the while loop stop asking for user input once the user enters "stop", but i cannot get it to stop asking the user for input.

public static void main(String[] args)
{
    exampletoday llist=new exampletoday();
    Scanner keyboard=new Scanner(System.in);
    System.out.println("Enter the first operator or operand: ");
    String first=keyboard.nextLine();
    llist.addNode(first);
    while(true)
    {
    System.out.println("Enter the next operand or operator or stop to stop: ");
    String next=keyboard.nextLine();
    llist.addNode(next);
    if(next=="stop")
    break;
}
Sarah
  • 1

1 Answers1

-1

Use .equals() to compare Strings.

public static void main(String[] args)
{
    exampletoday llist=new exampletoday();
    Scanner keyboard=new Scanner(System.in);
    System.out.println("Enter the first operator or operand: ");
    String first=keyboard.nextLine();
    llist.addNode(first);
    while(true)
    {
    System.out.println("Enter the next operand or operator or stop to stop: ");
    String next=keyboard.nextLine();
    llist.addNode(next);
    if(next.equals("stop"))
    break;
}
Spectric
  • 30,714
  • 6
  • 20
  • 43