Important Notice:

Practice-It will be discontinued as of November 1st, 2024. After this date, the website will remain online for a transitional period, but login will be restricted to University of Washington NetID authentication. This marks the next phase towards the platform's full retirement. Thank you for your use and support of the application over the years.

If you are looking for an alternative, a similar tool, CodeStepByStep, was developed independently by the original author of Practice-It, and is available at codestepbystep.com**

logo Practice-It logo

frequentFlier

Language/Type: Java file processing Scanner
Author: Jessica Miller (on 2010/12/28)

Write a static method named frequentFlier that accepts as its parameter a Scanner for an input file. The data in the Scanner represents all of the flights a person has taken. Your method should compute and return an integer representing the total frequent flyer miles the person has earned. The input consists of pairs of tokens where each pair begins with the type of ticket that the person bought ("coach", "firstclass", or "discount", case-sensitively) and is followed by the number of miles of the flight. The rules for awarding frequent flier miles are:

  • 1 frequent flyer mile is earned for each mile traveled in coach.
  • 2 frequent flyer miles are earned for each mile traveled in first class.
  • Zero frequent flyer miles are earned on a discounted flight.

For example, if a Scanner named input1 refers to an input file containing the following text:

coach 1500   firstclass 2000   discount 900   coach 3500

In this example, the person earns 1500 frequent flyer miles for the first flight, 4000 frequent flyer miles for the second flight, 0 frequent flyer miles for the third flight, and 3500 frequent flyer miles for the fourth flight. Therefore the call of frequentFlier(input1) should return 9000 (a total of 1500 + 2*2000 + 3500).

The input might span multiple lines and might have different spacing between tokens. You are to process all tokens on all lines together. For example, if a Scanner named input2 refers to an input file containing the following text:

firstclass        5000  coach         1500         coach
100 firstclass

2000    discount 300

Then the call of frequentFlier(input2) should return 15600 (a total of 2*5000 + 1500 + 100 + 2*2000).

You may assume that the input file exists and follows the format above; that the file has an even number of tokens and contains at least 1 pair of tokens; that every other token is an integer; and that the other tokens are valid ticket types.

Type your solution here:


This is a method problem. Write a Java method as described. Do not write a complete program or class; just the method(s) above.

You must log in before you can solve this problem.


Log In

If you do not understand how to solve a problem or why your solution doesn't work, please contact your TA or instructor.
If something seems wrong with the site (errors, slow performance, incorrect problems/tests, etc.), please

Is there a problem? Contact a site administrator.