473,396 Members | 1,743 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

I am less than a beginner at Java and I need to figure out..

I need to display the sum and the average without initializing num and num2. 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 varables values. Any help?


--------------------------------------------------
Expand|Select|Wrap|Line Numbers
  1. import java.io.*;
  2. import javax.swing.*;
  3.  
  4. class Numbert
  5.  
  6.  
  7.  
  8.  {public static void main (String[] args) throws IOException
  9.      { String   inData;
  10.       int num,num2;
  11.       int sum;
  12.       int avg;
  13.  
  14.       inData = JOptionPane.showInputDialog (null, "Enter an Integer Value. NOW!") ;
  15.       num = Integer.parseInt ( inData); // convert inData to int
  16.  
  17.   inData = JOptionPane.showInputDialog (null, "Enter another Integer Value. Ugh.."); 
  18.       num2 = Integer.parseInt ( inData);              
  19.  
  20.    System.out.println ("Sum of both numbers:" +sum);
  21.       System.out.println ("The average is:" +avg); 
  22.  
  23.       System.out.println ("Good-bye for now, suckahfish!");  //always executed 
  24.     }
  25.  
  26. }
Mar 17 '08 #1
26 1985
Ganon11
3,652 Expert 2GB
You don't have any code for calculating the sum or the average. What do you mean, you have to display them without initializing num and num2? num and num2 must get a value somehow...
Mar 17 '08 #2
JosAH
11,448 Expert 8TB
Besides other errors you can make those variables static class variables so the
JVM will initialize them to zero for you.

kind regards,

Jos
Mar 18 '08 #3
I'm really new to java and I don't exactly know how to make the variables static classes. Or do anything else to be honest. I did a tutorial and it didn't help.
Mar 19 '08 #4
You don't have any code for calculating the sum or the average. What do you mean, you have to display them without initializing num and num2? num and num2 must get a value somehow...
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.
Mar 19 '08 #5
JosAH
11,448 Expert 8TB
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.
Ok here's a spoiler: what keeps you from doing the following after num and num2
have reveived their values:

Expand|Select|Wrap|Line Numbers
  1. sum= num+num2;
  2. avg= sum/2;
  3.  
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 ;-)

kind regards,

Jos
Mar 19 '08 #6
Ok here's a spoiler: what keeps you from doing the following after num and num2
have reveived their values:

Expand|Select|Wrap|Line Numbers
  1. sum= num+num2;
  2. avg= sum/2;
  3.  
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 ;-)

kind regards,

Jos
I got that far but when I compiled it it said that num wasn't initialized.. And I don't know how to fix that. So I erased it and posted what I had.
Mar 19 '08 #7
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
}

}
Mar 19 '08 #8
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. Schweet.
\
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 for now, suckahfish!"); //always executed
}

}
Mar 19 '08 #9
Okay. My professor introduced arrays last class and now I have to use an array to represent 20 values and display the average.
Mar 20 '08 #10
Ganon11
3,652 Expert 2GB
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 like your professor showed you. Then figure out how to find the sum and average of any amount of numbers, not just 20. (That will be more impressive!)
Mar 21 '08 #11
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 like your professor showed you. Then figure out how to find the sum and average of any amount of numbers, not just 20. (That will be more impressive!)

Yeah. I can do any number with the fixed code above. I'll see what I can do.

--Edit: Thanks for all the help/replies guys. Helps a lot.
Mar 22 '08 #12
Heh, my teacher won't re-post the code that he gave us the last day of class before spring break it was something like intarr=newarr[]; or something.. Gah1
Mar 24 '08 #13
nomad
664 Expert 512MB
Heh, my teacher won't re-post the code that he gave us the last day of class before spring break it was something like intarr=newarr[]; or something.. Gah1
Take a look at this link
http://java.sun.com/docs/books/tutor...ts/arrays.html

nomad
Mar 24 '08 #14
Take a look at this link
http://java.sun.com/docs/books/tutor...ts/arrays.html

nomad
I don't know if I did it the long way or whatever but I got it to work.

Expand|Select|Wrap|Line Numbers
  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. }
  90.  

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. I feel really proud, heh.
Mar 25 '08 #15
You don't have any code for calculating the sum or the average. What do you mean, you have to display them without initializing num and num2? num and num2 must get a value somehow...


You know? After you posted this every time I create something I think of this post? lol
Mar 25 '08 #16
r035198x
13,262 8TB
Do
Expand|Select|Wrap|Line Numbers
  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.
Mar 25 '08 #17
r035198x
13,262 8TB
You know? After you posted this every time I create something I think of this post? lol
Now here's a little challenge. Use a for-loop for capturing the numbers from the user so that your code becomes shorter(fewer lines of code).
Mar 25 '08 #18
Do
Expand|Select|Wrap|Line Numbers
  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.
I don't know what all of this means..
java.lang.NullPointerException
at sun.misc.Launcher$AppClassLoader.loadClass(Launche r.java:269)
at java.lang.ClassLoader.loadClass(ClassLoader.java:2 99)
at java.lang.ClassLoader.loadClass(ClassLoader.java:2 51)
at bluej.runtime.ExecServer$3.run(ExecServer.java:787 )
All the ints are zero instead of the user entering them and I am really confused, heh. I can't find enough information about arrays >.<
Mar 25 '08 #19
sukatoa
539 512MB
I don't know what all of this means..
java.lang.NullPointerException
at sun.misc.Launcher$AppClassLoader.loadClass(Launche r.java:269)
at java.lang.ClassLoader.loadClass(ClassLoader.java:2 99)
at java.lang.ClassLoader.loadClass(ClassLoader.java:2 51)
at bluej.runtime.ExecServer$3.run(ExecServer.java:787 )
All the ints are zero instead of the user entering them and I am really confused, heh. I can't find enough information about arrays >.<
Can you post your updated codes?

sukatoa
Mar 26 '08 #20
pralu
34
this is the way u should do it......
  • import java.io.*;
    import javax.swing.*;

    class Number
    {
    public static void main (String[] args) throws IOException
    {
    String inData;
    int num,num2;
    int sum=0;
    int avg=0;

    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. Ugh..");
    num2 = Integer.parseInt ( inData);

    sum=num+num2;
    avg=sum/2;
    System.out.println ("Sum of both numbers:" +sum);
    System.out.println ("The average is:" +avg);

    System.out.println ("Good-bye for now"); //always executed
    }

    }
Mar 26 '08 #21
r035198x
13,262 8TB
this is the way u should do it......


  • import java.io.*;

    import javax.swing.*;



    class Number

    {

    public static void main (String[] args) throws IOException

    {

    String inData;

    int num,num2;

    int sum=0;

    int avg=0;



    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. Ugh..");

    num2 = Integer.parseInt ( inData);



    sum=num+num2;

    avg=sum/2;

    System.out.println ("Sum of both numbers:" +sum);

    System.out.println ("The average is:" +avg);



    System.out.println ("Good-bye for now"); //always executed

    }



    }
1.) Use code tags when posting code.
2.) Do not attempt to spoonfeed, it's against site rules.
3.) Always declare variables closest to where they are used.
4.) There already is a class called Number in the java.lang package. Always try to make sure that your class names do not clash with standard Java classes.
5.) The OP wants to get the average of possibly more than two numbers.
Mar 26 '08 #22
Navdip
22
I need to display the sum and the average without initializing num and num2. 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 varables values. Any help?


--------------------------------------------------
Expand|Select|Wrap|Line Numbers
  1. import java.io.*;
  2. import javax.swing.*;
  3.  
  4. class Numbert
  5.  
  6.  
  7.  
  8.  {public static void main (String[] args) throws IOException
  9.      { String   inData;
  10.       int num,num2;
  11.       int sum;
  12.       int avg;
  13.  
  14.       inData = JOptionPane.showInputDialog (null, "Enter an Integer Value. NOW!") ;
  15.       num = Integer.parseInt ( inData); // convert inData to int
  16.  
  17.   inData = JOptionPane.showInputDialog (null, "Enter another Integer Value. Ugh.."); 
  18.       num2 = Integer.parseInt ( inData);              
  19.  
  20.    System.out.println ("Sum of both numbers:" +sum);
  21.       System.out.println ("The average is:" +avg); 
  22.  
  23.       System.out.println ("Good-bye for now, suckahfish!");  //always executed 
  24.     }
  25.  
  26. }

hi...
you should try the following code to fix the problem..
your code is givin problem cause u r displaying something without giving any value to them...


import java.io.*;
import javax.swing.*;

class Numbert



{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. Ugh..");
num2 = Integer.parseInt ( inData);

sum=num+num2;
avg=sum/2;
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
}

}
Mar 26 '08 #23
r035198x
13,262 8TB
hi...
you should try the following code to fix the problem..
your code is givin problem cause u r displaying something without giving any value to them...


import java.io.*;
import javax.swing.*;

class Numbert



{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. Ugh..");
num2 = Integer.parseInt ( inData);

sum=num+num2;
avg=sum/2;
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
}

}
Please read items numbered 1,2,3, and 5 in my reply (post #22) above.
Mar 26 '08 #24
I need to make a program in java that does this with if statement. /*greater than or equal to 10 ifPGM
* Display a "happy Message"
*
* else
* if value less than 10 ;
* display an error message
*/
Apr 28 '08 #25
BigDaddyLH
1,216 Expert 1GB
I need to make a program in java that does this with if statement. /*greater than or equal to 10 ifPGM
* Display a "happy Message"
*
* else
* if value less than 10 ;
* display an error message
*/
Can you post your best effort at this and then ask a specific question about the code?
Apr 28 '08 #26
questionit
553 512MB
I need to make a program in java that does this with if statement. /*greater than or equal to 10 ifPGM
* Display a "happy Message"
*
* else
* if value less than 10 ;
* display an error message
*/
To do this, you need to know how to use 'if statement' and how to display a message.

If you can work on these well, there would be no difficulty in using these properly to accomplish what you are trying to do.

The initial declarations and writing main class in Java are just the basics. i hope you can put these all together right now.


Regards
Qi
Apr 29 '08 #27

Sign in to post your reply or Sign up for a free account.

Similar topics

8
by: jcnews | last post by:
I am using Windows XP and am learning how to program in Java. Is there any way to get my fonts to be anti-aliased in both editors and in my programs? The non anti-aliased fonts don't look very...
44
by: lester | last post by:
a pre-beginner's question: what is the pros and cons of .net, compared to ++ I am wondering what can I get if I continue to learn C# after I have learned C --> C++ --> C# ?? I think there...
6
by: Alex | last post by:
Hello I am intersting in developing and my background is VBA used in Excel and a brief intro to Java. I am interested in learning beyond VB and feel that C++ would be a very good language to...
12
by: Joshua Rulz | last post by:
Hi, i want to learn to program im quite skilled with computers and want to learn c++. is there anyone who can teach me or tell me a good website to learn it? all replies will be appreciated.
15
by: RAM | last post by:
Hello, I graduated computer science faculty and decided to became a programmer. Please help me to make a decision: Java or Microsoft .NET? What is the future of Java? Thanks! /RAM/
5
by: Jacky Luk | last post by:
import java.awt.Graphics; public class printtest extends java.applet.Applet { public void init() { } public void paint (Graphics g) {
10
by: See_Red_Run | last post by:
Hi, I am trying to figure out how to get started with PHP/MySQL. Everything I've read so far says to start with PHP first. I was expecting something like Visual Basic Express or some other type...
4
by: Johs | last post by:
I am looking for a good C++ book for beginners. I have some experience with C and a lot of experience with Java. I am currently reading Bjarne Stroustrups C++ Programming Language but it starts off...
1
by: robfrancis09 | last post by:
I'm a beginner web site developer and have a problem with my site appearing on IE6 or less. I used MS Expression Web to design the site using ASP. The domain is angusfuneralhome.com and...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.