What IS wrong with my code...I cant figure it out
Its meant to read a String in the do..while loop and get the char at the 0 position
/* Drivers are concerned with the mileage their automobiles get. One driver has kept track of several tankfuls of gasoline by recording the miles driven and gallons used for each tankful. Develop a Java application that will input the miles driven and gallons used (both as integers) for each tankful. The program should calculate and display the miles per gallon obtained for each tankful and print the combined miles per gallon obtained for all tankfuls up to this point. All averaging calculations should produce floating-point results. Use class Scanner and sentinel-controlled repetition to obtain the data from the user. This is not an assignment. Just an exercise in the textbook am using to learn java */ import java.util.Scanner; public class Mileage { public static void main(String[]args) { // initializing variables and Scanner object Scanner scan = new Scanner(System.in); int milesDriven; int gallonsUsed; int totalMiles = 0; int totalGallons = 0; double MPG, totalMPG = 0.0, averageMPG; int tankful = 1; // sentinel chosen to be a String...allows for a more user friendly interface String sentinelString = ""; // initialized to an empty String char sentinelChar = '0'; // initialized to 0 System.out.println("\nGood day.\n"); while (sentinelChar != 'N' || sentinelChar != 'n') { System.out.print("Please enter the number of miles driven on Tankful "+tankful+" : "); milesDriven = scan.nextInt(); System.out.print("Please enter the number of gallons consumed on Tankful "+tankful+" : "); gallonsUsed = scan.nextInt(); MPG = (double)milesDriven/gallonsUsed; totalMiles += milesDriven; totalGallons += gallonsUsed; System.out.printf("\nThe miles per gallon on Tankful %d is %.2f.",tankful,MPG); totalMPG += MPG; tankful++; // This portion of my code is not workin and I dont know why. Has to do with the Scanner...i guess do { System.out.print("\nDo you want to enter for more Tankfuls? (Y/N) : "); sentinelString = scan.next(); scan.nextLine(); // the other scan is to remove any extra Strings sentinelChar = sentinelString.charAt(0); if (sentinelChar != 'Y' || sentinelChar != 'N' || sentinelChar != 'y' || sentinelChar != 'n' ) { System.out.println("That entry is invalid."); } } while(sentinelChar != 'Y' || sentinelChar != 'N' || sentinelChar != 'y' || sentinelChar != 'n' ); } System.out.printf("\nThe miles per gallon for all %d Tankfuls is %.2f"); } }
Thanks guys.