0

I have an issue with the do-while code that I tried to run. I want it to repeat the process of adding the sum of two numbers together produced by the user, but the code will not run for some reason. I am new to java programming and loops. I just want to figure out why the code will not run and repeat the process of entering the numbers and adding the sum. I am not looking to compare the strings, my main goal is to make the while loop repeat the process of adding the two numbers produced by the user if they respond with Y. Thank you!

   import java.util.Scanner;
 public class Try {

public static void main(String[] args) {
    int first;
    int second;
    int sum; 
    String repeat;
    Scanner kbd= new Scanner(System.in);
    do { 
    System.out.print("Enter the first number: "); 
    first = kbd.nextInt(); 

    System.out.print("Enter the second number: "); 
    second = kbd.nextInt(); 

    sum = first + second; 
    System.out.println("The sum is:" +sum ); 
    System.out.print("Do you want to do this again? Y or N? "); 
    repeat= kbd.nextLine();
    } while (repeat=="y" || repeat=="Y");


    }


}
Mooooo
  • 1
  • 1
  • Besides the marked dupe, also check [Scanner is skipping nextLine() after using next() or nextFoo()?](https://stackoverflow.com/questions/13102045/scanner-is-skipping-nextline-after-using-next-or-nextfoo) – Ivar Apr 08 '18 at 11:09

1 Answers1

0

Change the data type of repeat variable to char.

Replace this repeat= kbd.nextLine(); line to repeat = kbd.next().charAt(0);

Replace this while (repeat=="y" || repeat=="Y"); to while (repeat == 'y' || repeat == 'Y');

Pshemo
  • 113,402
  • 22
  • 170
  • 242
Letsintegreat
  • 2,764
  • 4
  • 16
  • 35