Dialog box questions

Page 1 of 1

6 Replies - 996 Views - Last Post: 12 March 2010 - 04:18 PM Rate Topic: -----

#1 dazedandconfused  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 06-November 09

Dialog box questions

Posted 15 December 2009 - 10:55 PM

I have 2 questions about this code.

The first question is, "why does the input dialog box show a number 3?
Can I control what the dialog box says when it first displays and before the user does anything.

My second question is, "when this code outputs based on the choice, it generates one dialog box for each item, how can I go about making it instead display all the list of items in just one dialog box?

Or alternately should I just do everything in console? the input and the output?

I am open to any advice or suggestions since I don't know squat yet :0


	public class Item
   {
   	//setting up the name and price variables
	  private String aName = "";
	  private double aPrice=0.0;
   	
   	 //setting up the constructor 
	   public Item(String name, double price)
	  {
		 aName = name;
		 aPrice = price;
	  }
   	
   	 //get method retuns name
	   public String getaName()
	  {
		 return aName;
	  }
   
   	 //get method returns price
	   public double getaPrice()
	  {
		 return aPrice;
	  }
   
   	 //set method sets name
	   public void setaName(String name)
	  {
		 aName = name;
	  }
   	
   	 //set method sets price 
	   public void setiPrice (double price)
	  {
		 aPrice = price;
	  }
   }






   import java.util.*;
   import javax.swing.*;
   import java.text.DecimalFormat;
	
	public class CoffeeDriver
   { 
	   public static void main(String[]args)
	  {
		  //string array for item names
		 char choice;
		 String[] name = {"Donut", "Coffee", "Bagel", "Milk", "Water"};
		  //double array for item prices
		 double[] price = {.75, 1.00, 1.25, 1.50, 2.00};
		  
		  //asking the customer to choose whether we sort by price or name
		 choice = JOptionPane.showInputDialog(null,"Welcome to Wings Coffee Shop.\nWe have a great list of items on our menu.\nWould you like to see these items sorted by name or by price? (n/p)",
			JOptionPane.QUESTION_MESSAGE).charAt(0); //why does this have a 3 in it by default? I am lost on this one :0
			  
		  //converting choice letter to lower case		  
		 choice=Character.toLowerCase(choice); 
		 switch(choice)
		 {
			case 'n': 
			   sortName (name, price);
			   break;
			case 'p':
			   sortPrice(name,price);
			   break;
		 	//in case they do not enter the letter n or p	
			default:
			   JOptionPane.showMessageDialog(null,"Incorrect, Please enter n for name or p for price");
			   break;
		 } 
	  }
   	 //method to sort by prices
	   public static void sortPrice(String[] name, double[] price)
	  {
		 DecimalFormat money = new DecimalFormat("$0.00");
		 Arrays.sort(price); 
		 for(int count=0;count<=4;++count)
		 	//displaying the list when sorted by price
			JOptionPane.showMessageDialog(null, name[count]+ " " + money.format(price[count]));
			//This shows each item in a seperate dialog box, how can i make the entire list in just one dialog box?
	  }
   	
   	 //method to sort by name
	   public static void sortName(String[] name,double[] price)
	  {
		 DecimalFormat money = new DecimalFormat("$0.00");
		 Arrays.sort(name);
	  
		 for(int count=0;count<=4; ++count)
		 	//displaying the list when sorted by name
			JOptionPane.showMessageDialog(null, name[count]+ " " + money.format(price[count]));
			 //This shows each item in a seperate dialog box, how can i make the entire list in just one dialog box?
	  }
   
   }



This post has been edited by dazedandconfused: 15 December 2009 - 10:59 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Dialog box questions

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8347
  • View blog
  • Posts: 31,913
  • Joined: 06-March 08

Re: Dialog box questions

Posted 16 December 2009 - 05:33 PM

Your are using this version of JOptionPane


showInputDialog
public static String showInputDialog(Component parentComponent,
Object message,
Object initialSelectionValue)Shows a question-message dialog requesting input from the user and parented to parentComponent. The input value will be initialized to initialSelectionValue. The dialog is displayed on top of the Component's frame, and is usually positioned below the Component.

Parameters:
parentComponent - the parent Component for the dialog
message - the Object to display
initialSelectionValue - the value used to initialize the input field
Since:
1.4


took me a while to figure out that is horrible code

		 //asking the customer to choose whether we sort by price or name
		 choice = JOptionPane.showInputDialog(null,
				 "Welcome to Wings Coffee Shop.\nWe have a great list of items on our menu.\nWould you like to see these items sorted by name or by price? (n/p)",
			JOptionPane.QUESTION_MESSAGE).charAt(0); //why does this have a 3 in it by default? I am lost on this one :0


you should have done
		 //asking the customer to choose whether we sort by price or name
		 String str = JOptionPane.showInputDialog(null,
				 "Welcome to Wings Coffee Shop.\nWe have a great list of items on our menu.\nWould you like to see these items sorted by name or by price? (n/p)",
			JOptionPane.QUESTION_MESSAGE); //why does this have a 3 in it by default? I am lost on this one :0
		  choice = str.charAt(0);


now as far as your first question is concern the version of showInputDialog() you used uses as 3rd parameter:
initialSelectionValue.... you used: JOptionPane.QUESTION_MESSAGE which I presume values 3 :)
Was This Post Helpful? 1
  • +
  • -

#3 dazedandconfused  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 06-November 09

Re: Dialog box questions

Posted 17 December 2009 - 11:03 AM

Merci PBL!! :^:

I changed the 3 and made it an n so that is great. :^:

I would have never figured that problem out without your helpful feedback.
Was This Post Helpful? 0
  • +
  • -

#4 Locke  Icon User is offline

  • Sarcasm Extraordinaire!
  • member icon

Reputation: 521
  • View blog
  • Posts: 5,596
  • Joined: 20-March 08

Re: Dialog box questions

Posted 17 December 2009 - 11:45 AM

(if pbl didn't fix this already, keep reading, not sure he did. I don't have much time (I have a final exam in about 20 minutes), so I didn't read the entire thread.)

To put all your output in one dialog box, you'll have to build a string containing all of the output data that you'll want to use. Then simply output that string in one JOptionPane.showMessageDialog call.

:)

This post has been edited by Locke: 17 December 2009 - 11:49 AM

Was This Post Helpful? 0
  • +
  • -

#5 Guest_onlandagain*


Reputation:

Re: Dialog box questions

Posted 12 March 2010 - 12:40 AM

Also I don't see a title for that JOptionPane which will allow for the specific Question version of the JOptionPane so for example:

String str = JOptionPane.showInputDialog(null,
    "Welcome to Wings Coffee Shop.\nWe have a great list of items on our menu.\nWould you like to see these items sorted by name or by price? (n/p)",
    "Please Enter N or P", JOptionPane.QUESTION_MESSAGE); // will get rid of the 3 - worked for me
    choice = str.charAt(0); 



Was This Post Helpful? -2

#6 m-e-g-a-z  Icon User is offline

  • Winning
  • member icon


Reputation: 497
  • View blog
  • Posts: 1,453
  • Joined: 19-October 09

Re: Dialog box questions

Posted 12 March 2010 - 05:14 AM

Why bring up a post in march 2008?
Was This Post Helpful? 1
  • +
  • -

#7 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2876
  • View blog
  • Posts: 11,051
  • Joined: 15-July 08

Re: Dialog box questions

Posted 12 March 2010 - 04:18 PM

View Postm-e-g-a-z, on 12 March 2010 - 07:14 AM, said:

Why bring up a post in march 2008?


Yes. it's an old post...but not March 2008, it was December 2009...

Thanks for spotting this.
Topic Closed.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1