(Beginner) Program doesnt work
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
Code java:
/*
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.
Re: (Beginner) Program doesnt work
What is it doing that's wrong? Is there a compile/runtime error? What message is it giving you?
Re: (Beginner) Program doesnt work
Ignore the last printf method in the program. I have corected that.
Re: (Beginner) Program doesnt work
Either its not reading the String well or it's not returning the correct char.
It has no compile errors...just an endless do...while loop
Re: (Beginner) Program doesnt work
Try building a logic table for your do-while conditional.
If sentinelChar = 'Y', is it true or false? What if sentinelChar = 'a', or sentinelChar = 'n'?
Re: (Beginner) Program doesnt work
I have managed to debug it successfully...apparently all my || were wrong and were meant to be &&
That leads me to ask; What is the difference between && and || ? And what exactly do they mean because apparently I don't understand what they do
Re: (Beginner) Program doesnt work
&& is logical and. It means what you think it does: Is this true, and is that true? || is logical or. Note that its use in programming (and math) is a little different from its use in English: Is this true, or is that true, or are both true?
In your code, what it was basically saying is sentinelChar not 'y', or is sentinelChar not 'Y', ... this is always true because sentinelChar can't be 'y', 'Y', 'n', and 'N' at the same time.
Re: (Beginner) Program doesnt work
Buh how come it then responded to the && instead of the lgical OR.
Thought that for an OR, if one of the statements is true, the condition is true, but for the AND, if 1 is false, then the whole condition is false
Re: (Beginner) Program doesnt work
You might want to read more on logical AND & OR. The concept you are referring to in this question is short-circuiting where if the first condition is false,the entire thing fails. The evaluation only works for the && logic if both are true otherwise if one is true and the other is false, it fails. Again, review your logical tables to learn more.
Re: (Beginner) Program doesnt work
Quote:
Originally Posted by
moneyman021
Buh how come it then responded to the && instead of the lgical OR.
Thought that for an OR, if one of the statements is true, the condition is true, but for the AND, if 1 is false, then the whole condition is false
Exactly. That's why your do-while loop never exited with the logical or: there was always at least one true expression, so the entire statement evaluated to true. It could never be false. Now examine what happens with &&. If sentinelChar is 'y', 'n', 'Y', or 'N', one of those expressions ends up false end everything is false. If it's none of them, all the expressions end up true, and the do-while loop repeats (which is what you want).
Quote:
You might want to read more on logical AND & OR. The concept you are referring to in this question is short-circuiting where if the first condition is false,the entire thing fails. The evaluation only works for the && logic if both are true otherwise if one is true and the other is false, it fails. Again, review your logical tables to learn more.
Short circuiting can happen with either logical operator and is evaluated from left to right. Short circuiting won't happen with the bitwise operators. Short circuiting won't affect the results of the conditional, though. true | anything is always true and true || anything is always true, regardless of short circuiting. The only thing short circuiting will affect is any side effects that could take place.
Code Java:
public static boolean falseTest()
{
System.out.println("false");
return false;
}
public static boolean trueTest()
{
System.out.println("true");
return true;
}
public static void main(String[] args)
{
// prints out true
if (trueTest() || falseTest())
{
System.out.println("this gets printed");
}
System.out.println();
// prints out false
if (falseTest() && trueTest())
{
System.out.println("you will never see this");
}
System.out.println();
// prints out truefalse
if (trueTest() | falseTest())
{
System.out.println("this gets printed");
}
System.out.println();
// prints out falsetrue
if (falseTest() & trueTest())
{
System.out.println("you will never see this");
}
}
Re: (Beginner) Program doesnt work
Quote:
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' );
Here is the problems... Hope you understand