Welcome to the Java Programming Forums


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.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 11 of 11

Thread: (Beginner) Program doesnt work

  1. #1
    Junior Member
    Join Date
    Jan 2012
    Location
    Nigeria
    Posts
    12
    My Mood
    Cheerful
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Exclamation (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
    */
    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.
    I AM MONEY B-)


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: (Beginner) Program doesnt work

    What is it doing that's wrong? Is there a compile/runtime error? What message is it giving you?

  3. #3
    Junior Member
    Join Date
    Jan 2012
    Location
    Nigeria
    Posts
    12
    My Mood
    Cheerful
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: (Beginner) Program doesnt work

    Ignore the last printf method in the program. I have corected that.
    I AM MONEY B-)

  4. #4
    Junior Member
    Join Date
    Jan 2012
    Location
    Nigeria
    Posts
    12
    My Mood
    Cheerful
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default 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
    I AM MONEY B-)

  5. #5
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default 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'?

  6. #6
    Junior Member
    Join Date
    Jan 2012
    Location
    Nigeria
    Posts
    12
    My Mood
    Cheerful
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default 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
    Last edited by moneyman021; January 14th, 2012 at 06:40 PM.
    I AM MONEY B-)

  7. #7
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default 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.
    Last edited by helloworld922; January 14th, 2012 at 06:50 PM.

  8. #8
    Junior Member
    Join Date
    Jan 2012
    Location
    Nigeria
    Posts
    12
    My Mood
    Cheerful
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default 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
    I AM MONEY B-)

  9. #9
    Member
    Join Date
    Dec 2011
    Location
    United States
    Posts
    94
    My Mood
    Amused
    Thanks
    5
    Thanked 8 Times in 8 Posts

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

  10. #10
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: (Beginner) Program doesnt work

    Quote Originally Posted by moneyman021 View Post
    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).

    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.

    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");
    	}
    }
    Last edited by helloworld922; January 14th, 2012 at 09:09 PM.

  11. #11
    Junior Member
    Join Date
    Jan 2012
    Location
    Mumbai
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: (Beginner) Program doesnt work

    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

Similar Threads

  1. HI, could someone please tell me why my code doesnt work?
    By joelmeler in forum What's Wrong With My Code?
    Replies: 1
    Last Post: August 3rd, 2011, 01:37 AM
  2. My program doesnt want to do its math point my errors out please
    By Redlight in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 31st, 2010, 01:56 PM
  3. i can run my program but the math doesnt come otu
    By pairenoid in forum What's Wrong With My Code?
    Replies: 0
    Last Post: May 7th, 2010, 01:39 PM
  4. my menu doesnt work can u tell me whats wrong
    By claymore in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 8th, 2010, 04:16 AM
  5. my run program does not work
    By rman27bn in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 16th, 2009, 09:13 AM

Tags for this Thread