0

Question about Boolean Operators (Java)

I cannot get this code to run without error. I appreciate any help in this matter. import java.util.Scanner; public class Program { public static void main(String[] args) { Scanner input = new Scanner (System.in); System.out.print(" are you over 18?"); int age = input.Int(); System.out.print("are you a female instrested in birth control?"); String gender = input.String(); if ( age >= 18 && gender == "female") { System.out.print("Welcome to our site"); else System.out.println ("Sorry this site may be inapprobiate for you because it is about birth control"); } } }

26th May 2019, 11:35 PM
Markie Alicia
Markie Alicia - avatar
9 Respuestas
+ 1
You can't parse boolean that way(it will work if the string has "true" or "false" text. Also you cannot compare a boolean (gender) with a string, in that case you should compare gender with true (without quotes) or just put the variable name for testing it's content. You can write: Boolean gender = genderString == "female" ? true : false; It's a simple way of setting an object's value depending on a condition. Then: if(age >= 18 && gender)
27th May 2019, 12:44 AM
Andres0b0100
Andres0b0100 - avatar
+ 2
import java.util.Scanner; public class Program { public static void main(String[] args) { Scanner input = new Scanner (System.in); System.out.print(" are you over 18?"); int age = input.NextInt(); System.out.print("are you a female instrested in birth control?"); String gender = input.NextLine(); if ( age >= 18 && gender == "female") { System.out.print("Welcome to our site"); else System.out.println ("Sorry this site may be inapprobiate for you because it is about birth control"); } } }
26th May 2019, 11:54 PM
Markie Alicia
Markie Alicia - avatar
+ 2
Is this better? It still gets errors but I put in the curly braces. import java.util.Scanner; public class Program { public static void main(String[] args) { Scanner input = new Scanner (System.in); System.out.print(" are you over 18?"); int age = input.NextInt(); System.out.print("are you a female instrested in birth control?"); String gender = input.NextLine(); if ( age >= 18 && gender == "female") { System.out.print("Welcome to our site"); } else { System.out.println ("Sorry this site may be inapprobiate for you because it is about birth control");}
27th May 2019, 12:19 AM
Markie Alicia
Markie Alicia - avatar
+ 2
You still are writing "NextInt" instead of nextInt (the n should be lowercase), i think it's the only error. The brackets are well, they're no strictly needed in this case (when the condition only executes one statement) but it's a good practice to always put brackets.
27th May 2019, 12:23 AM
Andres0b0100
Andres0b0100 - avatar
+ 2
What do you think about this one? I get only one error at "==" import java.util.Scanner; public class Program { public static void main(String[] args) { Scanner input = new Scanner (System.in); System.out.print(" are you over 18?"); int age = input.nextInt(); System.out.print("are you a female instrested in birth control?"); String genderString = input.nextLine(); Boolean gender = Boolean.parseBoolean(genderString); if ( age >= 18 && gender == "true") { System.out.print("Welcome to our site"); } else { System.out.println ("Sorry this site may be inapprobiate for you because it is about birth control");} } }
27th May 2019, 12:37 AM
Markie Alicia
Markie Alicia - avatar
+ 1
The correct methods are input.nextInt() and input.nextLine()
26th May 2019, 11:47 PM
Andres0b0100
Andres0b0100 - avatar
+ 1
You have to close the "if" brackets before the "else"
26th May 2019, 11:57 PM
Andres0b0100
Andres0b0100 - avatar
+ 1
Also you may put brackets just after the else keyword and close after the block (in this case it's not needed because you just execute one function), example: if(age>=18 && gender=="female"){ System.out.println("true"); } else { System.out.println("false"); }
26th May 2019, 11:59 PM
Andres0b0100
Andres0b0100 - avatar
0
Hi there. Thank You for your response... I did what you said but I still get an error. Workspace points to the else as an error.
26th May 2019, 11:54 PM
Markie Alicia
Markie Alicia - avatar