1

I'm new to java and I'm working on some homework. I am trying to write an 'If' statement, by with a string and int. What is supposed to happen is the user inputs gender, today's date, and their birthday. Then the program is supposed to tell you if you're a male and you're a certain age, the best rate of you is... But, what I'm having trouble is, how do I write an 'If' statement with int and a string?

the //comments were given by my professor

Scanner kb = new Scanner(System.in);
System.out.println("Welcome to the car renter's rate finder.");       
System.out.print("Please enter the renter's gender (m/f): ");
String gender = kb.nextLine();

System.out.print("Please enter the today's date (mm dd yyyy): ");
     int curMonth = kb.nextInt();
     int curDay = kb.nextInt();
     int curYear = kb.nextInt();
     kb.nextLine();

System.out.print("Please enter the renter's date of birth (mm dd yyyy): ");
     int birthMonth = kb.nextInt();
     int birthDay = kb.nextInt();
     int birthYear = kb.nextInt();
     kb.nextLine();

     int age = 0;

     String rateResult;



  // Get age...
     age = calcAge(curMonth, curDay, curYear, birthMonth, birthDay, birthYear);

  // Get the rental rate...
     //rateResult = calcRateClass(age, gender); 
  // Display the results...
     //displayResults(gender, age, rateResult);
     displayResults(age);
  }
}

private static int calcAge(int curMonth, int curDay, int curYear,int, birthMonth,int birthDay,int birthYear)
{
return curYear - birthYear;
}

private static void displayResults(int age)
{
System.out.printf("Thanks. \n");
System.out.printf("The driver is %d years old. \n",age);
if (age >= 35 && age <= 65)
//Best rate (male drivers, age 33 – 65 and female drivers, age 30 - 62) -- $40.00 per day, $200.00 per week
  {
  System.out.println("$40.00 per day, $200.00 per week.");
  }
4
  • 1
    if (gender.equalsIgnoreCase("M") && (age >= 35 && age <= 65)) {...} Commented Jan 27, 2017 at 22:09
  • @MadProgrammer: for the sake of readability: if (gender.equalsIgnoreCase("M") && (35 <= age && age <= 65)) {...} Commented Jan 27, 2017 at 22:13
  • @MichaelLihs Personally, I like the way it reads now >= && <= visually shows a "between" intention, but that's me and I'm simple minded ;) Commented Jan 27, 2017 at 22:16
  • @MadProgrammer I agree - it's personal style :) in Maths you normally have a < x < b (which you can't have in Java), maybe that's why I like it more... but I see your point :) Commented Jan 27, 2017 at 22:37

2 Answers 2

1

First, make sure you pass gender to your displayResults method

private static void displayResults(String gender, int age) 

Then you can use String#equals to compare the value, but this is a case sensitive comparison, so something like String#equalsIgnoreCase would be a better choice

Once you have that information, you can include gender in your if statement

You could use...

if (gender.equalsIgnoreCase("M") {
    if (age >= 35 && age <= 65) {

This is good if you want to do more then one thing with "M", like if their aged over 65 for example.

If you're only going to do a single action against "M", you could use something like

if (gender.equalsIgnoreCase("M") && (age >= 35 && age <= 65)) {...}

which can reduce the "pyramid of doom" indent issue

I would suggest having a look at The Strings trail and API docs for more details

Sign up to request clarification or add additional context in comments.

Comments

0

To implement this in single if statement

//Best rate (male drivers, age 33 – 65 and female drivers, age 30 - 62) -- $40.00 per day, $200.00 per week

You could use this if:

if( (gender.equalsIgnoreCase('m') && (age >= 33 && age <= 65)) || (gender.equalsIgnoreCase('f') && (age >= 30 && age <=62)) )

And of course you need to pass gender parametr to your displayResults function:

private static void displayResults(String gender, int age) 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.