3

I am writing a program that asks the user to input his name, address and phone number. When the data is entered the program shall print the data and ask the user to verify the data by entering YES or NO. This process shall be repeated until the user is satisfied and answers YES to the question.

Now in my case I may have put a if-else statement inside the while loop in an inappropriate way. That's why it's not working as it is expected to be. How can I solve this?

Also I only have tried the promt asking to enter the users name. But if I want to add more prompt with different question in the similiar way then how could I do that?

Code:

package userinfo;    
import java.util.Scanner;

public class UserInfo {

    public static void main(String[] args) {    
        String name;
        String yes = "YES";
        String no = "NO";    
        Scanner userInput = new Scanner(System.in);

        System.out.println("Enter your name:");
        name = userInput.next();
        System.out.println("Please varify your name by typing YES or NO");

        while (true) {
            String input = userInput.next();
            if (input == yes) {
                System.out.println("Your name is: " + name);
            }
            if (input == no) {
                System.out.println("Enter your name again");

            } else {
                System.out.println("Invalid input! Enter value again:");
                break;
            }    
        }    
    }    
}
M. A. Kishawy
  • 5,001
  • 11
  • 47
  • 72
Jabir Al Fatah
  • 33
  • 1
  • 1
  • 5
  • 2
    First thing of all, change `==` to `.equals`, programmers will cry if they see it (I cried while I was writing this comment). – Maroun Nov 24 '14 at 19:33
  • 2
    Second, use `equalsIgnoreCase`. You don't want to force users to use a specific letter case when entering a yes/no response. – ajb Nov 24 '14 at 19:34

4 Answers4

3

You should use it like this:

if (input.equals(yes)) {
  System.out.println("Your name is: " + name);
}
else if (input.equals(no)) {
 System.out.println("Enter your name again");
}

Because the statement:

input==yes;

only checks if the references are equal or not

And to input more values from the user you can do it like this:

    System.out.println("Enter your name:");     //This you already did
    name = userInput.next
    System.out.println("Enter your surname:");
    String surname = userInput.next();

full code:(Asking for multiple prompts)

package userinfo;

import java.util.Scanner;

public class UserInfo {

public static void main(String[] args) {

    String name;
    int teleNum;
    String inputTeleNum;
    String yes = "YES";
    String no = "NO";

    Scanner userInput = new Scanner(System.in);

    while (true) {

        System.out.println("Enter your name:");
        name = userInput.next();

        System.out.println("Please varify your name by typing YES or NO");

        String input = userInput.next();

        if (input.equals(yes)) {
            System.out.println("Your name is: " + name);
        } else if (input.equals(no)) {
            System.out.println("Enter your name again");

        }

        while (true) {

            System.out.println("Enter your telephone number:");
            teleNum = userInput.nextInt();

            System.out.println("Please varify your telephone number by typing YES or NO");

            inputTeleNum = userInput.next();

            if (inputTeleNum.equals(yes)) {
                System.out.println("Your telephone num is: " + teleNum);
            } else if (inputTeleNum.equals(no)) {
                System.out.println("Enter your tele number again: ");

            }

        }

    }

}

}
Sarthak Mittal
  • 5,794
  • 2
  • 24
  • 44
  • I would suggest swapping the argument/object, so it looks like: `yes.equals(input)`. I don't think `Scanner` will ever return null, but when I'm comparing user-entered data to a constant, I always put the constant first to avoid NPE. – Craig Otis Nov 24 '14 at 19:37
  • 1
    That is not what the OP is asking... he wants to know about adding another question in the while loop – Ascalonian Nov 24 '14 at 19:37
  • @Craig yes you are right but these are just informal coding conventions that everyone follow differently :) – Sarthak Mittal Nov 24 '14 at 19:41
  • @Ascalonian i think we should not spoon feed the users by writing the exact code that's why didn't wrote the full code, other wise he won't be able to learn doing things himself, moreover the code he is asking is nearly similar to the one he has already written :) – Sarthak Mittal Nov 24 '14 at 19:43
  • 1
    @SarthakMittal But there's a difference between not writing the full code to help OP learn on his own, and posting an answer that doesn't really answer the question. – Craig Otis Nov 24 '14 at 19:52
  • @Craig you are right again, but then comes the matter of judgment, i judged that with the code he already had he would easily write the code he wants if given the proper hint, i just went with my judgment and tried to give that hint to him, what's your opinion on that? :) – Sarthak Mittal Nov 24 '14 at 19:56
  • Sarthak Mittal, Your answer helped. I accepeted it though it doesn't solve my second issue entirely. The remaining problem is with the multiple prompt. If the user varifies by NO, the program is asking the two questions at the same time without giving the user chance to enter his value. I am gonna edit your post and put my current code there so you know what's exactly going on. – Jabir Al Fatah Nov 25 '14 at 00:26
  • @Jabir it's good to know that i could be of some help, just didn't posted the second answer of yours directly because i thought that you could do it on your own, since it was nearly similar to the code you had already written :) – Sarthak Mittal Nov 25 '14 at 06:01
  • @SarthakMittal, I posted my comment before you did all your edits and put a lot of code in. I did it on your original post. – Ascalonian Nov 25 '14 at 13:17
  • @Ascalonian yes bro i know, the OP wanted to me to update the code that's why i did that :) – Sarthak Mittal Nov 25 '14 at 14:19
2

The break statement should go inside

if(input.equals(yes)) 

Not in the else{}.

Maroun
  • 94,125
  • 30
  • 188
  • 241
1

Since JDK 7 it's possible to use a String in a switch statement:

public String getTypeOfDayWithSwitchStatement(String dayOfWeekArg) {
     String typeOfDay;
     switch (dayOfWeekArg) {
         case "Monday":
             typeOfDay = "Start of work week";
             break;
         case "Tuesday":
         case "Wednesday":
         case "Thursday":
             typeOfDay = "Midweek";
             break;
         case "Friday":
             typeOfDay = "End of work week";
             break;
         case "Saturday":
         case "Sunday":
             typeOfDay = "Weekend";
             break;
         default:
             throw new IllegalArgumentException("Invalid day of the week: " + dayOfWeekArg);
     }
     return typeOfDay;
}

Maybe this is applicable in your situation. You loose the options of #equalsIgnoreCase() though.

cringe
  • 13,401
  • 15
  • 69
  • 102
0

To compare String use the methods equals() or equalsIgnoreCase(). this will resolve your issue.

BDRSuite
  • 1,594
  • 1
  • 10
  • 15