The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

(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

/*
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
*/importjava.util.Scanner;publicclass Mileage
{publicstaticvoid 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 interfaceString sentinelString ="";// initialized to an empty Stringchar sentinelChar ='0';// initialized to 0System.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 guessdo{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");}}

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

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

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).

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.

publicstaticboolean falseTest(){System.out.println("false");returnfalse;}publicstaticboolean trueTest(){System.out.println("true");returntrue;}publicstaticvoid main(String[] args){// prints out trueif(trueTest()|| falseTest()){System.out.println("this gets printed");}System.out.println();// prints out falseif(falseTest()&& trueTest()){System.out.println("you will never see this");}System.out.println();// prints out truefalseif(trueTest()| falseTest()){System.out.println("this gets printed");}System.out.println();// prints out falsetrueif(falseTest()& trueTest()){System.out.println("you will never see this");}}