Simple Array

Enter pizza size using one letter then display price

Page 1 of 1

9 Replies - 2001 Views - Last Post: 31 March 2010 - 08:57 AM Rate Topic: -----

#1 Guest_240z240z*


Reputation:

Simple Array

Posted 31 March 2010 - 06:15 AM

Just asking for a tip on the commented line below...

Code works great with integer type array but I need string type or at least when the user enters S...M... for small...or medium (size of pizza) the program displays the price...

Cant seem to make the array sting type and it have it work successfully...
thank you...


import javax.swing.*;
public class PizzaChoice
{
   public static void main(String[] args)
   {
      final int NUMBER_OF_ITEMS = 4;
      int[] validValues = {1, 2, 3, 4}; // IF THIS COULD HAVE S, M, L, X,  BUT IVE BEEN UNLUCKY FOR ATLEAST 30 HRS
		double[] prices = {6.99, 8.99, 12.50, 15.00};
      String strItem;
      double itemOrdered;
      double itemPrice = 0.0;
      boolean validItem = false;
		strItem = JOptionPane.showInputDialog(null,
          " Enter a letter code for the your size of pizza: \n S  = Small \n M = Medium \n L = Large \n X = Extra large ");
            itemOrdered = Integer.parseInt(strItem);
      for(int x = 0; x < NUMBER_OF_ITEMS; ++x)
      {
         if(itemOrdered == validValues[x])
         {
            validItem = true;
            itemPrice = prices[x];
         }
      }
      if(validItem)
         JOptionPane.showMessageDialog(null, "The price for item " +
            itemOrdered + " is $" + itemPrice);
      else
         JOptionPane.showMessageDialog(null,
            "Sorry - invalid item entered");
    }
}



Is This A Good Question/Topic? 0

Replies To: Simple Array

#2 japanir  Icon User is offline

  • jaVanir
  • member icon

Reputation: 1010
  • View blog
  • Posts: 3,025
  • Joined: 20-August 09

Re: Simple Array

Posted 31 March 2010 - 06:33 AM

you can use a char array if it is only the first letter:
char[] pizzaType = {'S', 'M', 'L', 'X'};


then, when getting the input, check only the first char of the String:
String letterCode = //..get the input by JOptionPane
char charCode = letterCode.charAt(0);

Was This Post Helpful? 0
  • +
  • -

#3 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2687
  • View blog
  • Posts: 11,349
  • Joined: 20-September 08

Re: Simple Array

Posted 31 March 2010 - 06:34 AM

You could use

final String[] ITEM_TYPE = {"", "S", "M", "L", "X" };

...

       JOptionPane.showMessageDialog(null, "The price for item " +
            ITEM_TYPE[itemOrdered] + " is $" + itemPrice);


Was This Post Helpful? 0
  • +
  • -

#4 240z240z  Icon User is offline

  • New D.I.C Head

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

Re: Simple Array

Posted 31 March 2010 - 06:54 AM

Thank you!...may be getting close...no errors just need price...kc

import javax.swing.*;
public class PizzaChoice
{
   public static void main(String[] args)
   {
      final int NUMBER_OF_ITEMS = 4;
      char[] validValues = {'S', 'M', 'L', 'X'};
		//String[] validValues = {" ", "S", "M", "L", "X"};
		double[] prices = {6.99, 8.99, 12.50, 15.00};
      String strItem;
      int itemOrdered;
      double itemPrice = 0.0;
      boolean validItem = false;
		strItem = 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 ");
      String letterCode = strItem; //..get the input by JOptionPane 
		char charCode = letterCode.charAt(0);
       
		itemOrdered = Integer.parseInt(strItem);
	 
		//itemOrdered = (strItem);
      for(int x = 0; x < NUMBER_OF_ITEMS; ++x)
      {
         if(itemOrdered == validValues[x])
         {
            validItem = true;
            itemPrice = prices[x];
         }
      }
      if(validItem)
         JOptionPane.showMessageDialog(null, "The price for item " +
            itemOrdered + " is $" + itemPrice);
      else
         JOptionPane.showMessageDialog(null,
            "Sorry - invalid item entered");
    }
}


This post has been edited by 240z240z: 31 March 2010 - 06:57 AM

Was This Post Helpful? 0
  • +
  • -

#5 NoobKnight  Icon User is offline

  • D.I.C Head

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

Re: Simple Array

Posted 31 March 2010 - 06:56 AM

Also if you use String but sure to use your condition correctly itemOrdered.equals(validValues[x])
instead of itemOrdered == validValues[x]
Was This Post Helpful? 0
  • +
  • -

#6 240z240z  Icon User is offline

  • New D.I.C Head

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

Re: Simple Array

Posted 31 March 2010 - 07:13 AM

thank you for the help but cant get past the boolean part of code in order to display price, receiving this error...:

PizzaChoice.java:24: ')' expected
if(itemOrdered.equals(validValues[x])
^
1 error


import javax.swing.*;
public class PizzaChoice
{
   public static void main(String[] args)
   {
      final int NUMBER_OF_ITEMS = 4;
      char[] validValues = {'S', 'M', 'L', 'X'};
		//String[] validValues = {" ", "S", "M", "L", "X"};
		double[] prices = {6.99, 8.99, 12.50, 15.00};
      String strItem;
      String itemOrdered;
      double itemPrice = 0.0;
      boolean validItem = false;
		strItem = 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 ");
      String letterCode = strItem; //..get the input by JOptionPane 
		char charCode = letterCode.charAt(0);
       
		//itemOrdered = Integer.parseInt(strItem);
	 
		//itemOrdered = (strItem);
      for(int x = 0; x < NUMBER_OF_ITEMS; ++x)
      {
         if(itemOrdered.equals(validValues[x])
         {
            validItem = true;
            itemPrice = prices[x];
         }
      }
      if(validItem)
         JOptionPane.showMessageDialog(null, "The price for item " +
            itemOrdered + " is $" + itemPrice);
      else
         JOptionPane.showMessageDialog(null,
            "Sorry - invalid item entered");
    }
}


Was This Post Helpful? 0
  • +
  • -

#7 zim1985  Icon User is offline

  • Grand Inquisitor
  • member icon

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

Re: Simple Array

Posted 31 March 2010 - 07:23 AM

You need another parathesis at the end.

if(itemOrdered.equals(validValues[x])

If you look, the last parenthesis closes the equals() method parameters. You need another one to close the if statement.
Was This Post Helpful? 0
  • +
  • -

#8 NoobKnight  Icon User is offline

  • D.I.C Head

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

Re: Simple Array

Posted 31 March 2010 - 07:28 AM

If you use a char you still need to use ==
Strings you use equals...
Was This Post Helpful? 0
  • +
  • -

#9 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2687
  • View blog
  • Posts: 11,349
  • Joined: 20-September 08

Re: Simple Array

Posted 31 March 2010 - 07:47 AM

This would be a lot easier were you to use the code i posted ;)
Was This Post Helpful? 1
  • +
  • -

#10 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2687
  • View blog
  • Posts: 11,349
  • Joined: 20-September 08

Re: Simple Array

Posted 31 March 2010 - 08:57 AM

It would be even easier like this, though less readable

	for (int x = 0; x < NUMBER_OF_ITEMS; ++x) {
	    if (itemOrdered.equals(validValues[x])) {
		validItem = true;
		itemPrice = prices[x];
		JOptionPane.showMessageDialog(null,
			"The price for item " + " SMLX".charAt(itemOrdered) + " is $" +
			itemPrice);
	    } else {
		JOptionPane.showMessageDialog(null, "Sorry - invalid item entered");
	    }
	}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1