user2526522
user2526522

Reputation: 1

Can't compare char

  boolean isGender = gender == "M";

this statement returns an error "incomparable types" the rest of the code before that is

import java.util.Scanner;
public class BMR
{
  public static void main(String[] args)
  {
  //Scanner
  Scanner in = new Scanner(System.in);

  //Get info
  System.out.println("Enter your name: ");
  String name = in.nextLine();
  System.out.println("Gender (M or F): ");
  String genderString = in.next();
  char gender = genderString.charAt(0);
  System.out.println("Enter your age: ");
  int age = in.nextInt();
  System.out.println("Height in inches: ");
  int height = in.nextInt();
  System.out.println("Weight in pounds: ");
  int weight = in.nextInt();
  System.out.println();

  //calculate BMR
  boolean isGender = gender == "M";

no idea why this doesn't work

Upvotes: 0

Views: 1294

Answers (2)

sunysen
sunysen

Reputation: 2351

try

boolean isGender = gender == 'M';

http://docs.oracle.com/javase/7/docs/api/java/lang/Character.html

Upvotes: 0

MadProgrammer
MadProgrammer

Reputation: 347214

"M" is not a character, it's String

Based on your code, you should be using gender == 'M'

As a side note, if gender was a String, you should be using gender.equals("M") or if don't care about the case, you could use gender.equalsIgnoreCase("M") instead.

Upvotes: 5

Related Questions