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 3 of 3

Thread: Program Skips the final if statement.

  1. #1
    Junior Member
    Join Date
    Apr 2020
    Posts
    8
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Program Skips the final if statement.

    So the program skips the final if statement, which is causing the program to set the coffee to the base price instead of prompting for another try if the value isn't in the array.


     
    import java.util.Scanner;
     
    public class JumpinJive
    {
       public static void main(String args[]) throws Exception
       {
          // Declare variables.
          String addIn;        // Add-in ordered by customer.
          final int NUM_ITEMS = 5; // Named constant
          // Initialized array of add-ins.
          String addIns[] = {"Cream", "Cinnamon", "Chocolate", "Amaretto", "Whiskey"}; 
          // Initialized array of add-in prices.
          double addInPrices[] = {.89, .25, .59, 1.50, 1.75};
          boolean foundIt = false; 
              // Loop control variable.
          double orderTotal = 2.00; // All orders start with a 2.00 charge
     
          // Get user input.
          Scanner input = new Scanner(System.in);
          System.out.print("Enter coffee add-in or XXX to quit: ");
          addIn = input.nextLine();
          // Write the rest of the program here.
          if(addIn != "XXX"){
            for(int x=0; x<NUM_ITEMS; x++){
              if(addIn.equals(addIns[x])){
                  System.out.println(addInPrices[x]);
                  foundIt = true;
                  orderTotal = orderTotal + addInPrices[x];
                  System.out.print("Enter coffee add-in or XXX to quit: ");
                  addIn = input.nextLine();
              }
            }
              if(foundIt = false){
                System.out.println("Sorry, we do not carry that.");
                System.out.println("Enter coffee add-in or XXX to quit: ");
                addIn = input.nextLine();
            }
          }
          System.out.println("Your order total is $" + orderTotal);
       } // End of main() method.
    } // End of JumpinJive class.
    Last edited by Imeerie; April 10th, 2020 at 07:04 PM. Reason: Solved

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Program Skips the final if statement.

     if(foundIt = false){
    The = operator is for assignment, Use the == operator for comparison.

    It is better to code if(!foundIt)
    then to try to compare its value with an == operator
    If you don't understand my answer, don't ignore it, ask a question.

  3. The Following User Says Thank You to Norm For This Useful Post:

    Imeerie (April 10th, 2020)

  4. #3
    Junior Member
    Join Date
    Apr 2020
    Posts
    8
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Program Skips the final if statement.

    Thanks once again! worked like a charm.

Similar Threads

  1. Input error. Program skips several lines of code during execution.
    By TMahoney1979 in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: September 21st, 2018, 10:52 AM
  2. My program skips an if statement I want to include in my loop--don't know why
    By ciscocoder in forum What's Wrong With My Code?
    Replies: 5
    Last Post: November 3rd, 2013, 09:53 AM
  3. Sqrt Program, skips over method
    By Lashickk in forum What's Wrong With My Code?
    Replies: 1
    Last Post: July 21st, 2013, 01:06 PM
  4. "Program execution skips the "if" statement"!
    By antony1925 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: May 7th, 2012, 07:15 AM
  5. [SOLVED] New at Java... my if, else if, else program doesn't seem to work, skips to else.Help!
    By KevinE in forum What's Wrong With My Code?
    Replies: 4
    Last Post: November 1st, 2010, 03:51 PM

Tags for this Thread