Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: height code not working

  1. #1
    Junior Member
    Join Date
    Sep 2013
    Posts
    6
    My Mood
    Yeehaw
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default height code not working

    I am just starting out in Java, I have lots to learn. I get the following error code.
    error:incompatible types
    gender=keyboard.next();
    required: char
    found: String
    Does this mean that I should have gender as a String?

    import java.util.Scanner;

    public class Height
    {
    public static void main (String[] args)
    {
    int inches, feet,mFeet, mInches, fFeet, fInches, cFeet, cInches;
    int mHeight, fHeight, cHeight;
    char gender;

    Scanner keyboard=new Scanner (System.in);

    System.out.println("Enter the child's gender (M or F):");
    gender=keyboard.next();

    if ((gender=="M")||(gender=="F"));
    System.out.println("Error - Invalid gender");

    System.out.println("Enter mothers height (feet):");
    mFeet=keyboard.nextInt();

    System.out.println("Enter the mothers height (inches):");
    mInches=keyboard.nextInt();

    System.out.println("Enter fathers height (feet):");
    fFeet=keyboard.nextInt();

    System.out.println("Enter the fathers height (inches):");
    fInches=keyboard.nextInt();
    //convert input form mother and father to inches and estimate the adult height of the child//
    mHeight=(mFeet*12)+mInches;
    fHeight=(fFeet*12)+fInches;

    if (gender=M)
    //male child formula//
    cHeightmHeight*13/12)+fHeight)/2;
    else
    // female child formula//
    cHeightfHeight*12/13)+mHeight)/2;
    //turn child height into feet and inches//


    System.out.println("The estimated adult height of this child is "
    + cHeight/12+" feet "
    +cHeight%12+" inches.");
    }
    }


  2. #2
    Member Kewish's Avatar
    Join Date
    Apr 2013
    Location
    Australia
    Posts
    116
    Thanks
    10
    Thanked 17 Times in 14 Posts

    Default Re: height code not working

    Welcome ZorraBella,

    Quote Originally Posted by ZorraBella View Post
    Does this mean that I should have gender as a String?
    Not really. It means that you have it as a string. But the program expects a char. It makes sense to store it as a char, so work on making String an 'actual' char value.

    You will find what you need here: Java String API #Methods

    Also please read this Welcome! If it's your first time here, please read this. and post your code in the correct tags to format it.

  3. The Following User Says Thank You to Kewish For This Useful Post:

    ZorraBella (October 28th, 2013)

  4. #3
    Member llowe29's Avatar
    Join Date
    Jul 2013
    Posts
    116
    My Mood
    Tired
    Thanks
    9
    Thanked 5 Times in 5 Posts

    Default Re: height code not working

    use Char (Variable) = gender.charAt(0) TO convert string to char. I hope the mod doesn't percieve this as spoonfeeding.

  5. The Following User Says Thank You to llowe29 For This Useful Post:

    ZorraBella (October 28th, 2013)

  6. #4
    Member
    Join Date
    Sep 2013
    Posts
    70
    Thanks
    1
    Thanked 13 Times in 13 Posts

    Default Re: height code not working

    There are a few errors in your code you will run into from what I can see. Make sure to learn the difference between (==, = and .equals()) Two of them are used to compare values while one is used to assign. Be sure to know when to use which of the them. Another thing you will want to learn is when to use " or ' for what data types and such. One more interesting bug you will experience at one point will be when using the Scanner object when it happens you will baffled and understand why it is interesting.

    Quick thing to note when if statements if you decide to exclude the { } only the code in the next line will be run so be careful when you exclude them.

  7. The Following User Says Thank You to Ubiquitous For This Useful Post:

    ZorraBella (October 28th, 2013)

  8. #5
    Junior Member
    Join Date
    Sep 2013
    Posts
    6
    My Mood
    Yeehaw
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Re: height code not working

    Quote Originally Posted by Kewish View Post
    Welcome ZorraBella,



    Not really. It means that you have it as a string. But the program expects a char. It makes sense to store it as a char, so work on making String an 'actual' char value.

    You will find what you need here: Java String API #Methods

    Also please read this Welcome! If it's your first time here, please read this. and post your code in the correct tags to format it.
    Thank you for your help. I got my code to compile, but I have still working on getting it to exit if the put in the wrong answer to the first question. Instead my if statement lets the code run all the way through.
    System.out.println("Enter the child's gender (M or F):");
           gender=keyboard.next().charAt(0);
     
     
          if ((gender!='M')||(gender!='F'));
               System.out.println("Error - Invalid gender");

  9. #6
    Member Kewish's Avatar
    Join Date
    Apr 2013
    Location
    Australia
    Posts
    116
    Thanks
    10
    Thanked 17 Times in 14 Posts

    Default

    Do you want it to exit or ask the user to try again?

  10. The Following User Says Thank You to Kewish For This Useful Post:

    ZorraBella (October 28th, 2013)

  11. #7
    Member
    Join Date
    Sep 2013
    Posts
    70
    Thanks
    1
    Thanked 13 Times in 13 Posts

    Default Re: height code not working

    You may want to post the revised version of your code. By the way you may want to check your if statement for gender.
    if ((gender!='M')||(gender!='F'));
       System.out.println("Error - Invalid gender");
    As it is now it checks if gender does not equal 'M' OR if gender does not equal 'F' then print out invalid gender. A char cannot be two chars at the same time so it will always display the error message. It is a simple fix just giving you a heads up. So what is it you are trying to accomplish now?

  12. The Following User Says Thank You to Ubiquitous For This Useful Post:

    ZorraBella (October 28th, 2013)

Similar Threads

  1. Height of a node in a binary tree
    By ueg1990 in forum Algorithms & Recursion
    Replies: 2
    Last Post: November 19th, 2012, 01:17 AM
  2. How to Translate working code into code with a Tester Class
    By bankoscarpa in forum What's Wrong With My Code?
    Replies: 6
    Last Post: October 15th, 2012, 02:13 PM
  3. Estimating the adult height of a child
    By ep7network in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 8th, 2012, 08:12 PM
  4. JButton Width & Height
    By Howdy_McGee in forum Java Theory & Questions
    Replies: 4
    Last Post: May 22nd, 2010, 07:12 AM
  5. Getting table height using JSTL
    By jsnx7 in forum JavaServer Pages: JSP & JSTL
    Replies: 1
    Last Post: March 19th, 2009, 12:03 PM