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

Thread: My code is skipping my for loop altogether and ignoring my array input.

  1. #1
    Junior Member
    Join Date
    Sep 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default My code is skipping my for loop altogether and ignoring my array input.

    I am trying to create a code that allows users to input an item that potentially matches an array item. If it matches the array, it should output the price of the item from a parallel array. If the input item doesn't match the array, the output should be an error message. When I run it, it only returns the error message. Please see code below:



    import javax.swing.*;

    public class JumpinJive {
    static String addIn;
    static String QUIT = "xxx";
    static final int NUM_ITEMS = 5;
    static String addIns[] = {"Cream", "Cinnamon", "Chocolate", "Amaretto", "Whiskey"};
    static double addInPrices[] = {.89, .25, .59, 1.50, 1.75};
    static boolean foundIt;
    static int x; //loop control variable
    static double orderTotal = 2.00 ; //all orders start with a $2.00 charge

    public static void main(String[] args) throws Exception {
    // TODO Auto-generated method stub
    addIn = JOptionPane.showInputDialog("Enter coffee add-in or" + QUIT + "to quit: ");
    while (addIn != QUIT){
    findAddin();
    }
    System.exit(0);
    }



    private static void findAddin() {
    // TODO Auto-generated method stub
    foundIt = false;
    for (x = 0; x < NUM_ITEMS; x++){

    if (addIn == addIns[x])
    {
    foundIt = true;
    orderTotal += addInPrices[x];
    System.out.println("The price for your " + addIns[x] + " coffee is : $ "+ orderTotal );
    }


    else if (foundIt == false)
    {
    System.out.println("Sorry, this add-in is not available. Please try again.");
    addIn = JOptionPane.showInputDialog("Enter nextcoffee add-in or " + QUIT + " to quit: ");
    }
    else{

    addIn = JOptionPane.showInputDialog("Enter next coffee add-in or " + QUIT + " to quit: ");
    }
    }


    }
    }


  2. #2
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: My code is skipping my for loop altogether and ignoring my array input.

    Welcome to the forum.
    See the Announcements page for the use of code tags and other useful forum tips.

    Use .equals to compare Strings

  3. #3
    Junior Member
    Join Date
    Sep 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: My code is skipping my for loop altogether and ignoring my array input.

    Sorry. I will post with the proper format next time.

    Is . equals the only way that Strings can be compared?

  4. #4
    Member Kewish's Avatar
    Join Date
    Apr 2013
    Location
    Australia
    Posts
    116
    Thanks
    10
    Thanked 17 Times in 14 Posts

    Default Re: My code is skipping my for loop altogether and ignoring my array input.

    No it is not the only way, but it is the easiest and it compares the contents of the string rather than if they are the same 'object' like an == operator would do.

    Have a read through the String API Methods

  5. #5
    Member
    Join Date
    May 2013
    Posts
    106
    My Mood
    Amused
    Thanks
    16
    Thanked 9 Times in 9 Posts

    Default Re: My code is skipping my for loop altogether and ignoring my array input.

    Quote Originally Posted by PearE View Post
    Sorry. I will post with the proper format next time.

    Is . equals the only way that Strings can be compared?
    No, but it's a lot more reliable than using ==

    I think that == sometimes works with Strings but not always so you shouldn't count on it.

    You can also use .compareTo() and .compareToIgnoreCase. Both of those method return 0 if the two strings are identical. As the name implies, .compareToIgnoreCase is not case-sensitive.

Similar Threads

  1. Code is ignoring the read.
    By javapol in forum What's Wrong With My Code?
    Replies: 8
    Last Post: February 27th, 2013, 01:52 PM
  2. Skipping parts of if loop (Coin toss Program)
    By ChicoTheMan94 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 5th, 2012, 03:52 PM
  3. [SOLVED] My program skipping a for loop
    By Dr.Code in forum What's Wrong With My Code?
    Replies: 5
    Last Post: September 16th, 2012, 09:05 AM
  4. Scanner skipping input
    By hblakek in forum File I/O & Other I/O Streams
    Replies: 4
    Last Post: February 21st, 2012, 01:44 PM
  5. Skipping Input Problem
    By Matty Alan in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 15th, 2011, 07:45 AM