This is a discussion on newbie question in java within the Java Programming forums, part of the Software Development category; hi there, I need to display the sum and the average without initializing passing inputs. I can't figure it ...
| |||||||
| Register | FAQ | Members List | Calendar | Mark Forums Read |
| |||
| hi there, I need to display the sum and the average without initializing passing inputs. I can't figure it out and it's killing me. I can't seem to get the sum or average displayed without assigning the variables values. Code:
import java.io.*;
import javax.swing.*;
class Number
{
public static void main (String[] args) throws IOException
{
String inData;
int num,num2;
int sum;
int avg;
inData = JOptionPane.showInputDialog (null, "Enter an Integer Value. NOW!") ;
num = Integer.parseInt ( inData); // convert inData to int
inData = JOptionPane.showInputDialog (null, "Enter another Integer Value..");
num2 = Integer.parseInt ( inData);
System.out.println ("Sum of both numbers:" +sum);
System.out.println ("The average is:" +avg);
System.out.println ("Good-bye!"); //always executed
}
} Last edited by latchu : 03-26-2008 at 12:43 AM. |
| Sponsored Links |
| |||
| Quote:
They need to get their value from the user entering a number and after they enter the two numbers I need to have the program display the sum and the average. |
| |||
| Quote:
k here's a way i found: what keeps you from doing the following after num and num2 have received their values: Code:
sum= num+num2;
avg= sum/2; It's a spoiler so you have to figure out how it works yourself. And you have to show us your working code. You don't have to perform the secret dance though ;-)
__________________ cheers Aman |
| |||
| Code: import java.io.*;
import javax.swing.*;
class Numbert
{
int num , num2;
int sum= num+num2;
int avg= sum/2;
{public static void main (String[] args) throws IOException
{ String inData;
inData = JOptionPane.showInputDialog (null, "Enter an Integer Value.") ;
num = Integer.parseInt ( inData); // convert inData to int
inData = JOptionPane.showInputDialog (null, "Enter another Integer Value.");
num2 = Integer.parseInt ( inData);
System.out.println ("Sum of both numbers:" +sum);
System.out.println ("The average is:" +avg);
System.out.println ("Good-bye for now, suckahfish!"); //always executed
}
} |
| |||
| I figured it out.. I was defining the sum/avg before num and num2 could be assigned values by the user. import java.io.*; import javax.swing.*; class Numbert { int num , num2; {public static void main (String[] args) throws IOException { String inData; inData = JOptionPane.showInputDialog (null, "Enter an Integer Value.") ; num = Integer.parseInt ( inData); // convert inData to int inData = JOptionPane.showInputDialog (null, "Enter another Integer Value."); num2 = Integer.parseInt ( inData); int sum= num+num2; int avg= sum/2; System.out.println ("Sum of both numbers:" +sum); System.out.println ("The average is:" +avg); System.out.println ("Good-bye); //always executed } } |
| |||
| OK, so what do you have so far to accomplish this task? You've got this working for 2 numbers. Instead of making 20 variables named num, num2, num3, num4,...etc., use an array. Then figure out how to find the sum and average of any amount of numbers, not just 20. (That will be more impressive!) |
| |||
| Take a look at this link http://java.sun.com/docs/books/tuto...lts/arrays.html
__________________ cheers Aman |
| |||
| I don't know if I did it the long way or whatever but I got it to work. Code: 1.
import java.io.*;
2.
import javax.swing.*;
3.
4.
class ArrT
5.
{
6.
7.
public static void main (String[] args) throws IOException {
8.
String inData;
9.
int[] anArray;
10.
11.
anArray = new int[20];
12.
13.
anArray[0] = 0;
14.
anArray[1] = 0;
15.
anArray[2] = 0;
16.
anArray[3] = 0;
17.
anArray[4] = 0;
18.
anArray[5] = 0;
19.
anArray[6] = 0;
20.
anArray[7] = 0;
21.
anArray[8] = 0;
22.
anArray[9] = 0;
23.
anArray[10] = 0;
24.
anArray[11] = 0;
25.
anArray[12] = 0;
26.
anArray[13] = 0;
27.
anArray[14] = 0;
28.
anArray[15] = 0;
29.
anArray[16] = 0;
30.
anArray[17] = 0;
31.
anArray[18] = 0;
32.
anArray[19] = 0;
33.
34.
inData = JOptionPane.showInputDialog (null, "Enter an Integer Value.") ;
35.
anArray[0] = Integer.parseInt ( inData); // convert inData to int
36.
37.
inData = JOptionPane.showInputDialog (null, "Enter another Integer Value.");
38.
anArray[1] = Integer.parseInt ( inData);
39.
40.
inData = JOptionPane.showInputDialog (null, "Enter another Integer Value.");
41.
anArray[2] = Integer.parseInt ( inData);
42.
inData = JOptionPane.showInputDialog (null, "Enter another Integer Value.");
43.
anArray[3] = Integer.parseInt ( inData);
44.
inData = JOptionPane.showInputDialog (null, "Enter another Integer Value.");
45.
anArray[4] = Integer.parseInt ( inData);
46.
inData = JOptionPane.showInputDialog (null, "Enter another Integer Value.");
47.
anArray[5] = Integer.parseInt ( inData);
48.
inData = JOptionPane.showInputDialog (null, "Enter another Integer Value.");
49.
anArray[6] = Integer.parseInt ( inData);
50.
inData = JOptionPane.showInputDialog (null, "Enter another Integer Value.");
51.
anArray[7] = Integer.parseInt ( inData);
52.
inData = JOptionPane.showInputDialog (null, "Enter another Integer Value.");
53.
anArray[8] = Integer.parseInt ( inData);
54.
inData = JOptionPane.showInputDialog (null, "Enter another Integer Value.");
55.
anArray[9] = Integer.parseInt ( inData);
56.
inData = JOptionPane.showInputDialog (null, "Enter another Integer Value.");
57.
anArray[10] = Integer.parseInt ( inData);
58.
inData = JOptionPane.showInputDialog (null, "Enter another Integer Value.");
59.
anArray[11] = Integer.parseInt ( inData);
60.
inData = JOptionPane.showInputDialog (null, "YOUSUCK! ENTER ANOHTER ONE.");
61.
anArray[12] = Integer.parseInt ( inData);
62.
inData = JOptionPane.showInputDialog (null, "Enter another Integer Value.");
63.
anArray[13] = Integer.parseInt ( inData);
64.
inData = JOptionPane.showInputDialog (null, "Enter another Integer Value.");
65.
anArray[14] = Integer.parseInt ( inData);
66.
inData = JOptionPane.showInputDialog (null, "This isn't over...");
67.
anArray[15] = Integer.parseInt ( inData);
68.
inData = JOptionPane.showInputDialog (null, "Enter another Integer Value.");
69.
anArray[16] = Integer.parseInt ( inData);
70.
inData = JOptionPane.showInputDialog (null, "Suck it blue!");
71.
anArray[17] = Integer.parseInt ( inData);
72.
inData = JOptionPane.showInputDialog (null, "Enter another Integer Value.");
73.
anArray[18] = Integer.parseInt ( inData);
74.
inData = JOptionPane.showInputDialog (null, "Enter another Integer Value.");
75.
anArray[19] = Integer.parseInt ( inData);
76.
77.
78.
int sum = anArray[0] + anArray[1] + anArray[2] + anArray[3] + anArray[4] + anArray[5] + anArray[6]
79.
+ anArray[7] + anArray[8] + anArray[9] + anArray[10] + anArray[11] + anArray[12] + anArray[13] +
80.
anArray[14] + anArray[15] + anArray[16] +anArray[17] + anArray[18] + anArray[19];
81.
int avg = sum/20;
82.
83.
System.out.println ("Sum of your sorry list of numbers is:" +sum);
84.
System.out.println ("The average is:" +avg);
85.
86.
System.out.println ("Good-bye for now, suckahfish!"); //always executed
87.
}
88.
89.
} Edit: I did have a question though. Because I'm having the user input their own value for each of the 20 numbers does it matter that I initialized each of the arrays at 0? Any information here is appreciated. Thanks for keeping up with me. |
| |||
| Do Code: 1.
for(int i =0;i < anArray.length;i++ ) {
2.
System.out.println(anArray[i]);
3.
} before that string of anArray[i] = 0; to find out for yourself what values the array locations are initialized to.
__________________ cheers Aman |
![]() |
| Thread Tools | |
| Display Modes | |
| |
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Newbie here :) | angel_dust | Introductions | 2 | 01-17-2008 09:28 AM |
| question | iyyappan | Flash Actionscript Programming | 3 | 12-06-2007 12:45 AM |
| new newbie here...*wink* | una_noche | Introductions | 0 | 06-26-2007 09:39 PM |
| Where does that leave the newbie wanting | montyauto | Advertising Sales and Affiliate Programs | 2 | 03-25-2007 12:38 PM |
| Another newbie arrived! | DWebHostingServ | Introductions | 2 | 03-15-2007 10:34 AM |