Im wondering why the while loop is not evaluating the expression correctly. It seems that the expression always evaluates to false. Im wondering if i did not use the scanner object correctly?
import java.util.Scanner;
public class GasMileage
{
public static void main(String [] args)
{
int milesDriven =0;
int gallonUsed =0;
float mpg = 0;
int tripNum = 1;
int totalMilesDriven =0;
String flag = "yes";
Scanner input = new Scanner(System.in);
do
{
//Program Header
System.out.println("Trip " + tripNum);
//Promp user to Enter Data
System.out.println("What are the miles driven for trip " + tripNum + "?");
milesDriven = input.nextInt();
System.out.println("What are gallons used for the trip?");
gallonUsed = input.nextInt();
//Calcualte the mpg of the trip
mpg = (float)milesDriven / gallonUsed;
System.out.println("The Miles per Gallon obtained in this trip is " + mpg);
//Keep track of total miles drivesn
totalMilesDriven += milesDriven;
System.out.println("Total Miles driven is " + totalMilesDriven + "\n");
//Determine if iteration should continue
System.out.println("Will there be another trip? Enter yes or no");
flag = input.next();
tripNum++;
System.out.println(flag);
}
while( flag == "yes" );
System.out.println("Exiting the program");
} }