Pizza size array

Display price if correct letter input is received

Page 1 of 1

4 Replies - 1551 Views - Last Post: 31 March 2010 - 07:46 AM Rate Topic: -----

#1 240z240z  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 17
  • Joined: 15-March 10

Pizza size array

Posted 31 March 2010 - 07:33 AM

Hello again afgter many hours I've abandoned the other code...im dyin here...any tips on getting this program to display price after correct letter code is entered for pizza size...your help is most appreciated...

This program runs but displays what looks to be memory addresses ...so close here
VR kent

import javax.swing.*;
public class SearchList2
{
   public static void main(String[] args)
   {
      String[] pizzaSize = {"S", "M", "L", "X"};
		double [] prices = {6.99, 8.99, 12.50, 15.00};
      String pizza;
      int x;
      boolean pizzaWasFound = false;
      pizza = JOptionPane.showInputDialog(null,
         " Enter a letter code for the size of your pizza: \n S  = Small \n M = Medium \n L = Large \n X = Extra   large ");
      for(x = 0; x < pizzaSize.length; ++x)
         if(pizza.equals(pizzaSize[x]))
           pizzaWasFound = true;
      if(pizzaWasFound)
           JOptionPane.showMessageDialog(null, "this pizza will cost you " + prices);
      else
           JOptionPane.showMessageDialog(null, pizza +
              " was not found in the list");
   }
}



Is This A Good Question/Topic? 0
  • +

Replies To: Pizza size array

#2 NoobKnight  Icon User is offline

  • D.I.C Head

Reputation: 51
  • View blog
  • Posts: 238
  • Joined: 14-July 09

Re: Pizza size array

Posted 31 March 2010 - 07:37 AM

You need to save the offset of the array, so you know which one to display. When you set the boolean value to true you need to save the index too.
So when you display prices you should display prices[savedIndex]);
Was This Post Helpful? 0
  • +
  • -

#3 XRaizeX  Icon User is offline

  • New D.I.C Head

Reputation: 5
  • View blog
  • Posts: 20
  • Joined: 20-March 10

Re: Pizza size array

Posted 31 March 2010 - 07:37 AM

maybe this will work

JOptionPane.showMessageDialog(null, "this pizza will cost you " + pizzaSize + " " + prices); 


You were asking just how to put the letter for size next to the price ?
Was This Post Helpful? 0
  • +
  • -

#4 zim1985  Icon User is offline

  • Grand Inquisitor
  • member icon

Reputation: 74
  • View blog
  • Posts: 557
  • Joined: 19-February 10

Re: Pizza size array

Posted 31 March 2010 - 07:38 AM

Incorporate something like:
double price = 0;
String size = "":
if(input.equals(pizzaSize[0])
{
    price = prices[0];
    size = pizzaSize[0];
}
// and so on for the other 
// sizes of pizza.
System.out.println("The price for a " + size + " pizza is " + price);


Just to be said, you can't use a switch statement because you are working with Strings, not ints.

This post has been edited by zim1985: 31 March 2010 - 07:39 AM

Was This Post Helpful? 0
  • +
  • -

#5 NoobKnight  Icon User is offline

  • D.I.C Head

Reputation: 51
  • View blog
  • Posts: 238
  • Joined: 14-July 09

Re: Pizza size array

Posted 31 March 2010 - 07:46 AM

Also you might want to uppercase your pizza field. That way you don't care if the user enters a lower case value.
pizza = JOptionPane.showInputDialog(null,
" Enter a letter code for the size of your pizza: \n S  = Small \n M = Medium \n L = Large \n X = Extra   large ");
pizza = pizza.toUpperCase();

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1