Results 1 to 15 of 15
Thread: Help me please
- 10-25-2010, 11:19 PM #1
Member
- Join Date
- Oct 2010
- Posts
- 51
- Rep Power
- 0
Help me please
Hi. I'm writting a program that processes listening figures for the radio station. Listening figures are recorded separately during morning and afternoon.
There are 3 shows in the morning and 3 shows in the afternoon.
so this program I'm trying to write will asks in turn for the figures for each morning shows and then prints the total. It does then the same for the afternoon shows. Finally it prints the overall total.
So I used for loops.
But I'm getting an errorJava Code:import javax.swing.*; // import the swing library for I/O class whatever { public static void main (String[] param) { radio(); System.exit(0); } // END main public static void radio() { for (int i=7; i<=11; i=i+2) { String radioshow;String morning; float morning1; String afternoon; float afternoon1; float totalfig; radioshow = JOptionPane.showMessageDialog(null, " The total number of Gleeful listeners this morning was" + totalfig + "million"); morning = JOptionPane.showInputDialog("What were the morning listening figures for the" + i + "am show?"); morning1 = Float.parseFloat(morning); afternoon = JOptionPane.showInputDialog("What were the afternoon listening figures for the" + i + "am show?"); afternoon1 = Float.parseFloat(afternoon); totalfig = morning1 + afternoon1; } } // END radio } // END class whatever
Is the program I have written correct?Java Code:whatever.java:41: incompatible types found : void required: java.lang.String radioshow = JOptionPane.showMessageDialog(null, " The total number of Gleeful listeners this morning was" + totalfig + "million"); ^ 1 error
- 10-25-2010, 11:22 PM #2
You are trying to assign a variable "radioshow" to a Message Dialog. It's a message dialog, not an input dialog.
Sincerely, Joshua Green
Please REP if I help :)
-
I second Josh's response and recommend that you check out the JOptionPane API to find another method there that might work better for you.
- 10-25-2010, 11:34 PM #4
Member
- Join Date
- Oct 2010
- Posts
- 51
- Rep Power
- 0
what do you mean? i did message dialog not input dialogYou are trying to assign a variable "radioshow" to a Message Dialog. It's a message dialog, not an input dialog.
__________________
Sincerely,
Josh Green
- 10-25-2010, 11:42 PM #5
A message dialog just outputs something to the screen. You can't store anything into a variable because there is nothing to store.
Sincerely, Joshua Green
Please REP if I help :)
- 10-26-2010, 12:25 AM #6
Member
- Join Date
- Oct 2010
- Posts
- 51
- Rep Power
- 0
Ok so I modified it a bit.
but now how do I make sure the program to get a total number of morning listeners and a total number of afternoon listeners?
so that it will ask user type in answer for every question and in the end there will be a message dialog that appears with the total for morning listeners. the same goes to afternoon.
Java Code:import javax.swing.*; // import the swing library for I/O class whatever { public static void main (String[] param) { radio(); System.exit(0); } // END main public static void radio() { for (int i=7; i<=11; i=i+2) { String morningshow; float morning1; float morning2; morningshow = JOptionPane.showInputDialog("What were the morning listening figures for the " + i + " am show?"); morning1 = Float.parseFloat(morningshow); morning2 = morning1; JOptionPane.showMessageDialog(null, " The total number of Gleeful listeners this morning was " + morning2 + " million"); } for (int i=1; i<=10; i=i+3) { String afternoonshow; float afternoon1; float afternoon2; afternoonshow = JOptionPane.showInputDialog("What were the afternoon listening figures for the " + i + " pm show?"); afternoon1 = Float.parseFloat(afternoonshow); afternoon2 = afternoon1; JOptionPane.showMessageDialog(null, " The total number of Gleeful listeners this afternoon was " + afternoon2 + " million"); } } // END radio } // END class whatever
- 10-26-2010, 12:27 AM #7
Member
- Join Date
- Oct 2010
- Posts
- 51
- Rep Power
- 0
please help me this time
- 10-26-2010, 12:37 AM #8
Well, first of all, morning2 and afternoon2 are useless variables. You don't need them. Why make morning2 = morning1 if you can just use the variable morning1?
Sincerely, Joshua Green
Please REP if I help :)
- 10-26-2010, 12:39 AM #9
Member
- Join Date
- Oct 2010
- Posts
- 51
- Rep Power
- 0
TRUE.
I have no idea what was going on with my head. deleted them.
- 10-26-2010, 12:43 AM #10
Okay, and as for the totals. All you need is a new variable, something like..."float sum = 0". And then just add to that variable every time the user makes an input. A running total.
Sincerely, Joshua Green
Please REP if I help :)
- 10-26-2010, 01:02 AM #11
Member
- Join Date
- Oct 2010
- Posts
- 51
- Rep Power
- 0
what do you mean by this
I know this. but I don't know how do I get to make a total of all the inputs.And then just add to that variable every time the user makes an input. A running total.
Java Code:float sum = 0; sum = sum + i
- 10-26-2010, 01:03 AM #12
The variable "i" is not your input variable. But the concept is correct.
Sincerely, Joshua Green
Please REP if I help :)
- 10-26-2010, 01:16 AM #13
Member
- Join Date
- Oct 2010
- Posts
- 51
- Rep Power
- 0
I did this. And I'm getting it one by one. For example " what were the morning listening figures for 7am show?" then I get " The total number of Gleeful listeners this morning was " + whatever I put before + " million" this repeats again for the 9am, 11am.
I do I total up everything and the total only appears once.
Java Code:String morningshow; float morning1; float sum = 0; morningshow = JOptionPane.showInputDialog("What were the morning listening figures for the " + i + " am show?"); morning1 = Float.parseFloat(morningshow); sum = sum + morning1; JOptionPane.showMessageDialog(null, " The total number of Gleeful listeners this morning was " + sum + " million");
- 10-26-2010, 01:48 AM #14
The adding of the sum should be inside your loops that you had before.
Sincerely, Joshua Green
Please REP if I help :)
-


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks