Scott P
Scott P

Reputation: 29

user input string and int, if statements

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.");
  }

Upvotes: 1

Views: 577

Answers (2)

kozielt
kozielt

Reputation: 36

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) 

Upvotes: 0

MadProgrammer
MadProgrammer

Reputation: 347204

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

Upvotes: 1

Related Questions