//Date: 3/16/2010

import javax.swing.*;

class PizzaChoice 
{
	public static void main(String[] args) throws Exception
	{
		char[] whatSize = { 'S', 'M', 'L', 'X'};
		char size;
		double[] sizePrice = {6.99, 8.99, 12.50, 15.00};
		int s;
		
		System.out.println("What size do you want?");
		size = Character.toUpperCase((char)System.in.read());
		
		for (s = 0; s < whatSize.length; ++s)
		{
			if (size == whatSize[s])
			System.out.println("Your pizza price is $" + sizePrice[s]);
		}
	}

}

That top section is for the command prompt, below is what I conjured up for a dialog input box and message box, but it doesn't work to the finish!!

//Date: 3/16/2010

import javax.swing.*;

class PizzaChoice 
{
	public static void main(String[] args) throws Exception
	{
		String[] whatSize = { "S", "M", "L", "X"};
		String size;
		double[] sizePrice = {6.99, 8.99, 12.50, 15.00};
		int s;
		
		size = JOptionPane.showInputDialog(null, "What size do you want?");
		
		for (s = 0; s < whatSize.length; ++s)
		{
			if (size == whatSize[s])
			JOptionPane.showMessageDialog(null, "Your pizza price is $" + sizePrice[s]);
		}
	}

}

Your explanation makes no sense. Do you want to know how to use a JOptionPane?

JOptionPane.showMessageDialog(null, "Here is some text!");

Your explanation makes no sense. Do you want to know how to use a JOptionPane?

JOptionPane.showMessageDialog(null, "Here is some text!");

Sorry, my first post.
Just wondering to get Arrays to work correctly in a dialog box, with the message Dialog giving the right answer for pizza size price, instead popping up 4 times, showing each price, obviously not getting the pizza size the input asked for with JUST the pizza price ONCE in the message Dialog box. Make sense?

Not really man. Not trying to be rude but punctuation and clear explanations go a long way. I have no idea what 'pizza size' or anything else you mentioned has to do with your problem.

Sorry man. Again, I've never asked someone else for help with java before.

From the book, the exercise states: Write a program using dialog boxes that prompts the user to make a choice for a pizza size - S, M, L, or X - and then displays the price as $6.99, $8.99, $12.50, $or $15.00 accordingly.

So I put the pizza size in an array, and the pizza price in an array. Just trying to create dialog boxes for an input of pizza size, then a message dialog stating what the price of the size I chose, then the program closes.

Thanks for you reply, but I'm still stuck. I'm new at arrays. I just finished a chapter in the book introducing me to arrays. It's an exercise question, the 2nd exercise question. The whole chapter talked about arrays, and this exercise is asking to use arrays (I'm assuming) with just adding JOptionPane dialog boxes for this instead of the basic command line (System.out.println).

The book so far has only touched the starting code for dialog boxes as well

size = JOptionPane.showInputDialog(null, "What size do you want?");

Just simple stuff like that. I don't need to use a customized box, just a simple JOptionPane.showInputDialog box asking me for size of pizza.
Then

JOptionPane.showMessageDialog(null, "Your pizza price is $" + sizePrice[s]);

a JOptionPane.showMessagDialog box telling me what I get for the size I chose, THAT'S IT.

Please help!!! Again, I just need help on showing two simple dialog boxes, i was just introduced to dialog boxes and arrays, keep in mind.

Thanks.

What size do you want?
x
Your pizza price is $15.0

Above, I chose x (which automatically puts it upperCase with the toUpperCase code, X, and then it tells me that X (X-large) is $15.00.

I wanna be able to do that using dialog boxes, but when I run it using boxes, I input what size, then the Message box comes up telling me I'm getting the pizza for $6.99, I click OK, then another box comes up, $8.99, click OK, another box pops up, $12.50, click OK, then another final windows pops up, $15.00......

I just want it to tell me the price of the one I chose, not all of them, which apparently, is because that's what I told the for loop to do,

String[] whatSize = { "S", "M", "L", "X"};
String size;
double[] sizePrice = {6.99, 8.99, 12.50, 15.00};
int s;

size = JOptionPane.showInputDialog(null, "What size do you want?");

for (s = 0; s < whatSize.length; ++s)
		{
			if (size == whatSize[s])
			System.out.println("Your pizza price is $" + sizePrice[s]);

but I don't want to do that, I don't know the code to only show the correct price and then stop.

It seems either I'm wording it wrong in the for loop, s < whatSize.length (maybe?) or the

if (size == whatSize[s])
     System.out.println("Your pizza price is $" + sizePrice[s]);

is wrong, especially the if statement.

Well, you could have them do something like this:

String choice = JOptionPane.showMessageDialog(null, "Enter 1 for Small, 2 for Medium, 3 for Large, 4 for X");
int userChoice = Integer.parseInt(choice);
System.out.println("Your pizza price is " + sizePrice[userChoice-1]);

Honestly though, the other Dialog that I showed you before would be a much more reasonable dialog in this case, because it forces the user to pick from a list of choices, and the user can't pick anything that you don't want them to pick. The other dialog that I showed you returns an int which would represent '0' for small, '1' for medium, etc. So if you use that dialog, you could directly use sizePrice, whereas if you use the dialog that I showed you above, you have to do a bunch of extra work to make sure the user actually entered something between 1-4.

By the way, the code you posted above would work, except you cannot use '==' to compare two Objects for equality. Using '==' only works to compare primitive types (int, double, etc) for equality. To compare Objects for equality, you have to use a method - the equals() method. The equals() method is supposed to return true if the Objects are equal and false otherwise. So change this:


if (size == whatSize[s])
//Should be
if (size.equals(whatSize[s]))

Thanks sooo much. For 2 days I was looking for the right code....size.equals instead of size ==. Thank you BestJewSinceJC
I will look at the other stuff you sent me as well.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.