0

Java beginner here. With the following code my else function automatically prints. So even before answering the question 'Do you want to buy the house with your partner? Type Yes / No' it's already throwing the else function out there.

//How much is the gross salary
    boolean partner;
    String partnerQuestion;
    double partnerSalary =0;
    System.out.print("Do you want to buy the house with your partner? Type Yes / No \n");
        while(true){
            partnerQuestion = sc.nextLine().trim().toLowerCase();
            if(partnerQuestion.equals("yes")){
                //how high is the loan                
                System.out.print("What is your partners´ anually gross salary? \n");
                partnerSalary = sc.nextDouble();
                partner = true;
                break;
            } 
            else if(partnerQuestion.equals("no")){
                partner = false;
                break;
            }  
            else{
                System.out.print("I did not understand you. Please try again.");
            }
        }

To give a good picture my if / else inside this while loop, does work.

boolean timeTF;
    double term;
    System.out.print("For how many years should the mortgage run? Choose between 10/15/20/25/30 \n");
        while(true){
            term = sc.nextDouble();
            if(term != 10 && term != 15 && term != 20 && term != 25 && term != 30){
                System.out.print("Please type 10/15/20/25/30, other values are not valid \n");
                timeTF = true;   
            } 
            else{  
                timeTF = false;
                break;
            }
        }

So my question is, what am I missing? Thanks in advance.

5
  • What is "sc"? A Scanner object? Commented Jan 16, 2017 at 22:04
  • Side note: you can use System.out.println to automatically append \n at the end of the string instead of doing it manually with System.out.print Commented Jan 16, 2017 at 22:04
  • 1
    Could this be your problem? stackoverflow.com/questions/23450524/… Commented Jan 16, 2017 at 22:04
  • Cannot recreate. You need to provide the instantiation of your sc object. Commented Jan 16, 2017 at 22:09
  • Props to Keiwan for finding the duplicate! Thanks buddy. Commented Jan 16, 2017 at 22:24

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.