0

I am writing a program for class for recursion. That part works perfectly. But I also need to repeat the program until the user chooses to quit.

My program prompts to repeat the program but doesn't wait for a response. Instead it quits right away.

I added a comment in the code below I believe where the problem lies.

import java.util.*;
public class Recursion{

public static int sumOfInt(int x)
{           
  int sum =0;
  if (x <= 1)
     return x;

  else
     sum += x % 10; 
  x /= 10; 
  return sum + sumOfInt(x);
}

public static void main (String [ ] arg){


  Scanner sc = new Scanner(System.in);
  int userInt = 0;
  boolean userInput = false;
  boolean endLoop = false;
  String userAgain = "";


 do{
  do{
     try{
        System.out.print("Please enter an integer number: ");
        userInt = sc.nextInt();
        userInput = true;


     }
     catch(InputMismatchException ime) {
        System.out.println("You did not enter an integer number");
        sc.nextLine(); 
     }

   }while(userInput == false);

   System.out.println("The integer you entered is " + userInt);
   System.out.println("The sum of the digits in the integer you entered is " + sumOfInt(userInt));
        System.out.println();
        System.out.println("Enter yes to try again");

        userAgain = sc.nextLine();
        // I don't understand why the above line is not waiting for a user response before quitting.

         if(userAgain == "yes"){
           endLoop = false;
         }else{
           endLoop = true;
         }



   }while(endLoop == false);

 }
 }
CookieMcQ
  • 1
  • 2
  • See this question: [Scanner is skipping nextLine() after using next(), nextInt() or other nextFoo() method](http://stackoverflow.com/questions/13102045/scanner-is-skipping-nextline-after-using-next-nextint-or-other-nextfoo). – MikaelF Jan 30 '17 at 21:56
  • I answered before someone linked that other question. But, fine, I deleted my answer. – D M Jan 30 '17 at 22:00
  • Beside duplicate question, take a look at [How do I compare strings in Java?](http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – Pshemo Jan 30 '17 at 22:08

1 Answers1

2

Add the following line of code before userAgain = sc.nextLine():

sc.nextLine();

and it should work. The call to sc.nextInt() doesn't consume the newline character, \n, and so the call to sc.nextLine() you have consumes the \n from the call to nextInt and continues thinking that the "Enter" key has been pressed, before the user actually gets to enter anything. To be the most clear, you may want to include this call to nextLine() after your call to readInt(), so that others who read your code (or you, later!) know why you included this additional call. You should add it in one place or the other, not both.

In reference to the String comparison you're doing, use the .equals method: https://www.leepoint.net/data/expressions/22compareobjects.html

if(object1 == object2)

checks to see if two references are to the same object in memory, and does not compare by value, whereas

if(object1.equals(object2))

check to see that the values of the two objects are logically equivalent.

Following my advice in the context of your program, you'd want to check the userAgain value as such:

if(userAgain.equals("yes")){
endLoop = false;
} else {
endLoop = true;
}
  • 2
    You should also include the red herring that the other answers were aimed at, he should not use "==" for a string comparison. – Alex Jan 30 '17 at 21:55
  • 1
    Yeah. Otherwise that's going to be the next question. – D M Jan 30 '17 at 21:56