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

Thread: parallel array newbie

  1. #1
    Member
    Join Date
    Oct 2011
    Posts
    35
    Thanks
    8
    Thanked 2 Times in 2 Posts

    Default parallel array newbie

    This is the code, in this code when i try to compile it tell me that price might not have been initialized. I went through the code and cant find whats wrong . can someone tell me why the price variable might not be initialized and also tell me how to fix it.






    // BillyGoat.java - This program looks up and prints the names and prices of fast-

    food items.
    // Input: Interactive.
    // Output: Name and price of fast-food item or error message if fast-food item is

    not found.

    import javax.swing.*;

    public class BillyGoat
    {
    public static void main(String args[]) throws Exception
    {
    double price;
    int custNum; // Customer number.
    String custString; // String version of customer number.
    String itemName; // Name of item ordered.
    final int NUM_ITEMS = 3; // Named constant
    // Initialized array of food items.
    String foodItems[] = {"Cheeseburger", "Pepsi", "Chips"};
    // Initialized array of food prices.
    double foodPrices[] = {2.49, 1.00, .59};
    boolean foundIt = false; // Flag variable.
    int sub; // Loop control variable.
    String QUIT="finish";
    // Get user input.
    custString = JOptionPane.showInputDialog("Enter customer number: ");
    custNum = Integer.parseInt(custString);
    itemName = JOptionPane.showInputDialog("Enter item name: ");


    while(itemName.compareTo("finish") !=0)
    {

    sub=0;
    foundIt=false;
    while(sub<3)
    {
    if(itemName==foodItems[sub])
    {
    foundIt = true;
    price=foodPrices[sub];
    }
    x++;
    }
    if(foundIt==true)
    {
    System.out.println("the amount of:" + itemName + "is" + price);
    }
    else{
    System.out.println(" we do not carry this please choose another

    selection");

    }
    custString = JOptionPane.showInputDialog("Enter customer number: ");
    custNum = Integer.parseInt(custString);
    itemName = JOptionPane.showInputDialog("Enter item name or finish to

    quit ");

    }


    System.exit(0);
    } // End of main() method.
    } // End of BillyGoat class.


  2. #2
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: parallel array newbie

    String s;
    System.out.println(s);
    What should be displayed? The variable s has not been given a value. You have the same problem in your code.
    Improving the world one idiot at a time!

  3. #3
    Member
    Join Date
    Oct 2011
    Posts
    35
    Thanks
    8
    Thanked 2 Times in 2 Posts

    Default Re: parallel array newbie

    yes but doesnt price = foodPrices[x] give it a value. cause x is going to be either 0 1 2.

  4. #4
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: parallel array newbie

    What if the if statement is false? What value will price have then?
    Improving the world one idiot at a time!

  5. #5
    Member
    Join Date
    Oct 2011
    Posts
    35
    Thanks
    8
    Thanked 2 Times in 2 Posts

    Default Re: parallel array newbie

    ohhhhhhh. Thanks for that. So i recompiled it and it wont work properly it doesnt recognize that i have Cheeseburger, Pepsi and chips in the array when i enter in Cheeseburger it gives me the else statement we do not carry that when it is suppose to say the price for cheeseburger is and the price . How do I fix this?

  6. #6
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: parallel array newbie

    Post new code. When you do wrap it in highlight tags. Read the linkto BB code near the bottom of the page.
    Improving the world one idiot at a time!

Similar Threads

  1. Pick random objects from array/arraylist. Newbie.
    By Sputnik in forum Collections and Generics
    Replies: 3
    Last Post: October 29th, 2010, 11:59 AM
  2. problem searching for a String element in a parallel array
    By david185000 in forum What's Wrong With My Code?
    Replies: 10
    Last Post: July 27th, 2010, 01:24 PM
  3. problem for writing to parallel port LPT1 on windows by RXTX library
    By sahar_m in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: April 20th, 2010, 04:31 AM
  4. theory about serial / parallel port & java
    By wolfgar in forum Java Theory & Questions
    Replies: 5
    Last Post: January 4th, 2010, 10:08 PM
  5. Newbie...
    By jchan in forum Member Introductions
    Replies: 2
    Last Post: September 28th, 2009, 02:39 AM