The question was : Write a program that processes an input.txt file that contains data regarding ticket type followed by mileage covered and reports how many frequent-flier miles the person earns.
- 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.
- 0 frequent flyer miles are earned on a discounted flight.
For example, given the data in input.txt below, your method must return 15600 (2*5000 + 1500 + 100 + 2*2000).
Input.txt:
firstclass 5000 coach 1500 coach
100 firstclass 2000 discount 300
My code gives me a problem with the parseint method. Any help would be appreciated :)
//InInteger class
import java.lang.NumberFormatException;
public class IsInteger {
public static boolean IsaInteger (String s)throws NumberFormatException
{
try
{
Integer.parseInt(s);//converts the string into an integer
return true;
}
catch (NumberFormatException e)
{
return false;
}
}
}
//main class
import java.io.*;
import java.util.StringTokenizer;
public class LA5ex2 {
public static void main(String[] args) throws FileNotFoundException {
BufferedReader input= new BufferedReader (new InputStreamReader (new FileInputStream("C:/Users/user/workspace/LA5ex2/input.txt")));
String str;
int TotalMiles=0;
try {
int mileage,lines=0;
String check,copy=null;
String word=null;
boolean isString=false;
while ((str = input.readLine()) != null)
{
lines++;
StringTokenizer token = new StringTokenizer(str);
while (token.hasMoreTokens())
{
if ((lines>1) && (isString))
{
//do nothing
}
else
{word= token.nextToken();
copy=word;}
if (token.hasMoreTokens())
mileage= Integer.parseInt(token.nextToken());
else
{
if (!(IsInteger.IsaInteger(word)))
{
copy=word;
isString=true;
}
break;
}
if (copy.equals("firstclass"))
TotalMiles+= (2*mileage);
else if (copy.equals("coach"))
TotalMiles+= (1*mileage);
else if (copy.equals("discount"))
TotalMiles+= (0*mileage);
}
}
System.out.println("Frequent-flier miles the person earns: "+ TotalMiles);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}