Results 1 to 3 of 3
- 11-22-2008, 04:26 AM #1
Member
- Join Date
- Nov 2008
- Posts
- 32
- Rep Power
- 0
[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:
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.Tvs.java:31: cannot find symbol
symbol : variable output
location: class Tvs
output.Str = "You chose model " + model + "TV with these features:" + "\n"
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 04:31 AM. Reason: forgot to mark thread as a question
- 11-22-2008, 05:21 AM #2
The output variable was not initialized ,you didn't declare it at all.
instead of this
type thisJava 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";
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";
- 11-22-2008, 05:30 AM #3
Member
- Join Date
- Nov 2008
- Posts
- 32
- Rep Power
- 0
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
-
[SOLVED] JOptionPaneMessage dialog question
By blueyan in forum New To JavaReplies: 2Last Post: 09-28-2008, 07:50 AM -
Help! Need to randomly assign boxes
By newb101 in forum New To JavaReplies: 1Last Post: 09-16-2008, 10:57 PM -
Need help - using algorithm statements
By javanewbie in forum New To JavaReplies: 2Last Post: 07-23-2008, 11:20 AM -
avoiding if statements
By valoyivd in forum New To JavaReplies: 1Last Post: 04-02-2008, 09:08 AM -
Help with if else statements
By zoe in forum New To JavaReplies: 1Last Post: 07-24-2007, 07:56 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks