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: Need help with character as a varible

  1. #1
    Junior Member
    Join Date
    Jun 2013
    Posts
    2
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Need help with character as a varible

    I'm a college student and haven't programmed in a very long time and have never used Java as a source code. I'm taking a beginner Java class and our first project has me stumped. Where I'm having the problem is getting the user input for a Yes or No question by using a character as a variable (Y or N) and then moving into the If/else section of the code.

    Thank you in advance for any help

    Here is what I've written so far...

    public static void main(String[] args) {
    Scanner keyBoard = new Scanner(System.in);

    //Constants
    final double MONTHLY_PLAN = 39.99; //Cost per month
    final int ALLOWABLE_MINUTES = 500; // number of minutes per month
    final double ADDITIONAL_MINUTE_RATE = .45; //cost of overage minutes
    final double TEXT_MSG_PLAN = 15.00; //Cost per month for text messaging
    final double TEXT_MSG_RATE = .12; //Cost per text message w/o text plan
    final double DATA_PLAN = 20.00; //Cost per month for data
    final int ALLOWABLE_DATA = 300; //In Megabytes, overage costs

    //User Inputs
    int minutesUsed;
    int textMessages;
    int dataUsage;
    char Y,y = 0;
    char N,n = 0;
    System.out.println("Please enter number of minutes used for the month:");
    minutesUsed = keyBoard.nextInt();// user enters minutes used for the month
    System.out.println("Do you have a text messaging plan?");
    System.out.println("Please enter Y or N");
    boolean textPlan = keyBoard.nextBoolean();
    if(); //textPlan = Y
    //Use TEXT_MSG_PLAN for calculations
    else(); //textPlan = N
    //Use TEXT_MSG_RATE for calculations
    System.out.println("How many texts did you send and recieve?");
    textMessages = keyBoard.nextInt();
    System.out.println("Do you have a data plan?");
    System.out.println("Please enter Y or N");
    boolean dataPlan = keyBoard.nextBoolean();
    if () //dataPlan = Y
    //Use DATA_PLAN for calulations
    System.out.println("How many MB of data was used for the month?");
    dataUsage = keyBoard.nextInt();
    else () //dataPlan = N
    //Do not use DATA_PLAN for calculations



    //Calculations

    //Output


  2. #2
    Junior Member
    Join Date
    May 2013
    Posts
    12
    Thanks
    0
    Thanked 6 Times in 2 Posts

    Default Re: Need help with character as a varible

    --
    Last edited by jps; June 4th, 2013 at 10:54 AM. Reason: Spoonfed code removed

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

    Netwraith (June 4th, 2013)

  4. #3
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Need help with character as a varible

    @haiderali Please read The problem with spoonfeeding

    @Netwraith Please use code tags when posting code to the forum. See the announcements page for help
    You did explain what you are trying to do, and provided code to show what you have completed, but there is no question or clear indication where you are stuck or what you need help with. Always be sure to ask some question or state what it is you do not understand, or where exactly you are stuck.

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

    Netwraith (June 4th, 2013)

  6. #4
    Member
    Join Date
    May 2011
    Location
    west palm beach, FL
    Posts
    189
    My Mood
    Tired
    Thanks
    41
    Thanked 11 Times in 10 Posts

    Default Re: Need help with character as a varible

    true and false is not the same as yes or no(Y or N) and read this over to learn your if/else statements a little better and that should help with your problems

    Simple Java Tutorial, IF and Else statements in Java

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

    Netwraith (June 4th, 2013)

  8. #5
    Junior Member
    Join Date
    Jun 2013
    Posts
    2
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Need help with character as a varible

    Thanks for the help, the first set of code that was shown actually put me on the right direction. Gave me the right kick to get me thinking right( it in itself wasn't right but put me on the right track) It's been a long time since I've done any programming (about 17 or so years, so it's taking a bit to dust off those memories)

  9. #6
    Member
    Join Date
    May 2011
    Location
    west palm beach, FL
    Posts
    189
    My Mood
    Tired
    Thanks
    41
    Thanked 11 Times in 10 Posts

    Default Re: Need help with character as a varible

    haha read this forum for a bit and you will learn sooooo much

  10. #7
    Junior Member
    Join Date
    Jun 2013
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need help with character as a varible

    You may simplely use scanner.nextLine() to get a string and then compare it with "Yes" or "Y" and "No" or "N".

Similar Threads

  1. non static varible from static context error
    By chopficaro in forum What's Wrong With My Code?
    Replies: 12
    Last Post: June 5th, 2012, 08:07 PM
  2. [SOLVED] Illegal character
    By Tank314 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: March 12th, 2011, 11:59 AM
  3. Character Array
    By p_dibb in forum Collections and Generics
    Replies: 1
    Last Post: February 16th, 2011, 10:08 AM
  4. Help with Character arrays
    By shanklove in forum What's Wrong With My Code?
    Replies: 6
    Last Post: September 6th, 2010, 01:56 PM
  5. The character '' is an invalid XML character exception getting
    By sumanta in forum What's Wrong With My Code?
    Replies: 5
    Last Post: January 9th, 2010, 12:13 PM