Results 1 to 3 of 3
  1. #1
    hungdukie is offline Member
    Join Date
    Nov 2008
    Posts
    32
    Rep Power
    0

    Question [SOLVED] Using dialog boxes and switch statements question

    I'm a newb to Java and doing my best to learn on my own, without much help but the book I have is tough to figure out. If anyone could help me out I would surely appreciate it. I am doing a Lab exercise where I am expected to write a program to display a dialog box to the user asking what their choice of three models of televisions will be. They are to enter the model number: 100, 200, or 300. I am to write a switch structure that will, based on their selection, display in a dialog box their choice along with the feature set and price of the model they chose. I have started my code but I checked after the first case to see how it would compile and I am getting the following message:
    Tvs.java:31: cannot find symbol
    symbol : variable output
    location: class Tvs
    output.Str = "You chose model " + model + "TV with these features:" + "\n"
    I am not sure why I am getting this message unless I am not declaring a variable that I'm supposed to, but I really don't know how I should declare one for the output string.
    Here is my code:
    Java Code:
    import java.util.*;
    import java.io.*;
    import javax.swing.JOptionPane;
    
    public class Tvs
    {
    public static void main(String[] args)
    {
    
    String inputStr;
    String outputStr;
    int model;
    inputStr = JOptionPane.showInputDialog
    			  ("This program asks the user to enter a television model number." +"\n"	
    			  + "The description of the model chosen will be displayed" + "\n"
    			  + "\n"
    			  + "\n"
    			  + "Please enter the model chosen" + "\n"
    			  + "Model 100 comes with remote control, timer," + "\n"
    			  + "and stereo sound and costs $1000" + "\n"
    			  + "Model 200 come with all features of model 100" + "\n"
    			  + "and picture-in-picture, and costs $1200" + "\n"
    			  + "Model 300 comes with all features of model 200 and" + "\n"
    			  + "HDTV, flat screen, 16x9 aspect ratio and costs $2400" + "\n");
    
    
    			  
    			  switch (model)
    			  {
    			  case 100:
    			  	output.Str = "You chose model " + model + "TV with these features:" + "\n"
    				+ "Remote control, timer, and stereo sound" + "\n"
    				+ "Your price will be $1000.00" + "\n";
    JOptionPane.showMessageDialog(null, outputStr,
    					   "Television Selection",
    						JOptionPane.INFORMATION_MESSAGE);
    			   case 200: 					
    				} 
    			  }
    			  }
    Last edited by hungdukie; 11-22-2008 at 05:31 AM. Reason: forgot to mark thread as a question

  2. #2
    serjant's Avatar
    serjant is offline Senior Member
    Join Date
    Jun 2008
    Location
    Ukraine,Zaporozhye
    Posts
    487
    Rep Power
    12

    Default

    The output variable was not initialized ,you didn't declare it at all.
    instead of this
    Java Code:
    output.Str = "You chose model " + model + "TV with these features:" + "\n"
    				+ "Remote control, timer, and stereo sound" + "\n"
    				+ "Your price will be $1000.00" + "\n";
    type this

    Java Code:
    outputStr = "You chose model " + model + "TV with these features:" + "\n"
    				+ "Remote control, timer, and stereo sound" + "\n"
    				+ "Your price will be $1000.00" + "\n";

  3. #3
    hungdukie is offline Member
    Join Date
    Nov 2008
    Posts
    32
    Rep Power
    0

    Default Declare all variables!

    Yes, I figured out that that was the problem. I declared and initialized the variable and voila! It compiles and runs like ti is supposed to. If I did something incorrectly I would appreciate any feedback you all might have.
    Thanks.
    I will post my code in the hopes that it may help someone else:
    Java Code:
    import java.util.*;
    import java.io.*;
    import javax.swing.JOptionPane;
    
    public class Tvs
    {
    public static void main(String[] args)
    {
    
    String inputStr;
    String outputStr;
    int model;
    inputStr = JOptionPane.showInputDialog
    			  ("This program asks the user to enter a television model number." +"\n"	
    			  + "The description of the model chosen will be displayed" + "\n"
    			  + "\n"
    			  + "\n"
    			  + "Please enter the model chosen" + "\n"
    			  + "Model 100 comes with remote control, timer," + "\n"
    			  + "and stereo sound and costs $1000" + "\n"
    			  + "Model 200 come with all features of model 100" + "\n"
    			  + "and picture-in-picture, and costs $1200" + "\n"
    			  + "Model 300 comes with all features of model 200 and" + "\n"
    			  + "HDTV, flat screen, 16x9 aspect ratio and costs $2400" + "\n");
    			  model = Integer.parseInt(inputStr);
    
    
    
    
    			  
    			  switch (model)
    			  {
    			  case 100:
    			  	outputStr = "You chose model " + model + " TV with these features:" + "\n"
    				+ "Remote control, timer, and stereo sound" + "\n"
    				+ "Your price will be $1000.00" + "\n";
    JOptionPane.showMessageDialog(null, outputStr,
    					   "Television Selection",
    						JOptionPane.INFORMATION_MESSAGE);
    						break;
    			   case 200:
    			  	outputStr = "You chose model " + model + " TV with these features:" + "\n"
    				+ "Remote control, timer, stereo sound and picture-in-picture" + "\n"
    				+ "Your price will be $1200.00" + "\n";
    JOptionPane.showMessageDialog(null, outputStr,
    					   "Television Selection",
    						JOptionPane.INFORMATION_MESSAGE);
    						break;
    				case 300:
    				 outputStr = "You chose model " + model + " TV with these features:" + "\n"
    				+ "HDTV, flat screen, 16x9 aspect ratio" + "\n"
    				+ "picture-in-picture,"  + "\n"
    				+ "remote control, timer, and stereo sound" + "\n"
    				+ "Your price will be $2400.00" + "\n";
    JOptionPane.showMessageDialog(null, outputStr,
    					   "Television Selection",
    						JOptionPane.INFORMATION_MESSAGE);
    						break;				
     					
    				} 
    			  }
    			  }

Similar Threads

  1. [SOLVED] JOptionPaneMessage dialog question
    By blueyan in forum New To Java
    Replies: 2
    Last Post: 09-28-2008, 08:50 AM
  2. Help! Need to randomly assign boxes
    By newb101 in forum New To Java
    Replies: 1
    Last Post: 09-16-2008, 11:57 PM
  3. Need help - using algorithm statements
    By javanewbie in forum New To Java
    Replies: 2
    Last Post: 07-23-2008, 12:20 PM
  4. avoiding if statements
    By valoyivd in forum New To Java
    Replies: 1
    Last Post: 04-02-2008, 10:08 AM
  5. Help with if else statements
    By zoe in forum New To Java
    Replies: 1
    Last Post: 07-24-2007, 08:56 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •