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.


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

Results 1 to 3 of 3

Thread: Array Problem

  1. #1
    Junior Member
    Join Date
    Sep 2009
    Thanked 0 Times in 0 Posts

    Default Array Problem

    Hi everyone. I'm trying to finish up this bit of code for some homework, but I can't quite figure out where I'm going wrong. I can get the code to print out fine, but when it goes to itemPrice = validValue[x], it's not changing the price, and defaulting to 15.50. Can someone tell me where I'm going wrong? I don't need the answer, but a nudge in the right direction would be nice. Thanks!

    import javax.swing.*;
    public class Main {
        public static void main(String[] args) {
            final double NUMBER_OF_PIZZAS = 4;
            String[] validValues = {"S", "M", "L", "X"};
            double[] prices = {6.99, 8.99, 12.50, 15.50};
            String strItem;
            int entry;
            boolean validItem = false;
            double itemPrice = 0.0;
            strItem = JOptionPane.showInputDialog(null, "Enter the pizza size you want.\nS - Small\nM - Medium\nL - Large\nX - Extra Large");
            for (int x = 0; x < NUMBER_OF_PIZZAS; ++x) {
                if (strItem.equalsIgnoreCase(validValues[x])) {
                    validItem = true;
                    itemPrice = prices[x];
            if (validItem) {
                JOptionPane.showMessageDialog(null, "The price of a " + strItem + " pizza is $" + itemPrice);
            } else {
                JOptionPane.showMessageDialog(null, "That's not a valid pizza");

  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Thanked 833 Times in 772 Posts
    Blog Entries

    Default Re: Array Problem

    The code you posted should work as expected, and just to be sure I ran it and it worked as expected.
    Last edited by copeg; January 24th, 2010 at 07:54 PM.

  3. #3
    Junior Member
    Join Date
    Sep 2009
    Thanked 0 Times in 0 Posts

    Default Re: Array Problem

    I found an extra pair of brackets. I think my IDE might not have been compiling it correctly or something. Oh well. Thanks for your help!

Similar Threads

  1. array/string problem
    By RSYR in forum Collections and Generics
    Replies: 1
    Last Post: December 18th, 2009, 10:24 PM
  2. Problem with 2d array
    By Anyone in forum Collections and Generics
    Replies: 2
    Last Post: November 14th, 2009, 09:32 PM
  3. [SOLVED] Creation of objects of Array in Java
    By sadi_shihab in forum Collections and Generics
    Replies: 4
    Last Post: July 9th, 2009, 01:38 PM
  4. [SOLVED] Array loop problem which returns the difference between the value with fixed value
    By uplink600 in forum Loops & Control Statements
    Replies: 5
    Last Post: May 15th, 2009, 04:31 AM
  5. Java program for 2-D Array Maze
    By Peetah05 in forum Collections and Generics
    Replies: 11
    Last Post: May 8th, 2009, 04:30 AM