-1

I am trying to replicate this code that I did in the console in GUI.

if (age <18 && feelings.toUpperCase().equals("SAD")){
    System.out.println(phrase.sadQuotesBefore18[rand.nextInt(2)]);
} else if (age >= 18 && feelings.toUpperCase().equals("SAD") ) {
    System.out.println(phrase.sadQuotesAfter18[rand.nextInt(3)]);
} else if (age <18 && feelings.toUpperCase().equals("ANGRY")){
    System.out.println(phrase.angryQuotesBefore18[rand.nextInt(2)]);
} else if (age >= 18 && feelings.toUpperCase().equals("ANGRY")){
    System.out.println(phrase.angryQuotesAfter18[rand.nextInt(8)]);
} else if (age < 18  && feelings.toUpperCase().equals("HAPPY")){
    System.out.println(phrase.happyQuotesBefore18[rand.nextInt(2)]);
} else if (age >= 18 && feelings.toUpperCase().equals("HAPPY")){
    System.out.println(phrase.happyQuotesAfter18[rand.nextInt(4)]);
} else {
    System.out.println("You have entered and unknown combination, please try again");
}    

As shown below. I realise in the code below I cannot get the else if and else statements as shown above to work. Only the if statement as shown below works. I am new to this so any assistance would be appreciated.

submitButton.setOnAction(new EventHandler<ActionEvent>() {
    @Override
    public void handle(ActionEvent event) {
        if (agebox.getText().equals("<=18") && feelings.equals("Sad")) {

        }
        showAlert(AlertType.CONFIRMATION, gridPane.getScene().getWindow(), "Your Quote!", phrase.sadQuotesBefore18[rand.nextInt(3)]);
    }
}
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • The if statement in your second chunk of code does nothing. The curly braces have no content. The alert will always be shown. Try to move the second curly brace after the show alert. You can then use the "else if () { do something }" pattern to do the rest of the tests. – Michael McKay Jun 03 '18 at 12:35
  • @MichaelMcKay you mean move the curly brace that is between the IF and SHOWalert and place it after the show alert? – NewtoProgramming Jun 03 '18 at 12:40
  • 1
    What is `ageBox`? If it's a text field, are you really expecting the user to type `<=18` into it? In the original code you posted, `age` is a numeric type of some kind. – James_D Jun 03 '18 at 12:48
  • @James_D yes that is the text box. I was getting errors so I entered that to see what will work. It is suppose to check if the user enters anything equal or less than 18 to give certain response versus if they enter an age over 18 as shown on the first code. – NewtoProgramming Jun 03 '18 at 12:52
  • 1
    So you should first [convert the text to an integer](https://stackoverflow.com/questions/5585779/how-do-i-convert-a-string-to-an-int-in-java), and then you can basically use exactly the same structure as you had before. – James_D Jun 03 '18 at 12:54
  • @James_D thanks let me go try that. – NewtoProgramming Jun 03 '18 at 13:00
  • There is no info what `feelings` even is or if it's assigned at all. Also storing the data in 2 `Map`s and using `chooseQuote(map.get(feelings.toUpperCase()));` `final Random random = new Random(); private String chooseQuote(String[] quotes) { return quotes == null || quotes.length == 0 ? "You have entered and unknown combination, please try again" : quotes[random.nextInt(quotes.length)];}` would be preferable to using a `if-else-if` like this... – fabian Jun 03 '18 at 18:45

1 Answers1

0

not sure what is your problem, but at least you can categorize your if statements:

submitButton.setOnAction(new EventHandler<ActionEvent>() {
    @Override
    public void handle(ActionEvent event) {
        String quoteStr="";
        if (agebox.getText().equals("<18")) {
            if (feelings.equals("SAD")) {
               quoteStr = phrase.sadQuotesBefore18[rand.nextInt(3)];
            } else if (feelings.equals("ANGRY")) {
               quoteStr = phrase.angryQuotesBefore18[rand.nextInt(2)];
            } else if (feelings.equals("HAPPY")) {
               quoteStr = phrase.angryQuotesBefore18[rand.nextInt(2)];
            }
        }
        else if (agebox.getText().equals(">=18")) {
            if (feelings.equals("SAD")) {
               quoteStr = phrase.sadQuotesAfter18[rand.nextInt(3)];
            } else if (feelings.equals("ANGRY")) {
               quoteStr = phrase.angryQuotesAfter18[rand.nextInt(8)];
            } else if (feelings.equals("HAPPY")) {
               quoteStr = phrase.angryQuotesAfter18[rand.nextInt(4)];
            }
        }

        showAlert(AlertType.CONFIRMATION, gridPane.getScene().getWindow(), "Your Quote!", quoteStr);
    }
}
mostafa.S
  • 1,452
  • 4
  • 16
  • 27