IT Community - Software Programming, Web Development and Technical Support

newbie question in java

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 ...


Go Back   IT Community - Software Programming, Web Development and Technical Support > Software Development > Java Programming

Register FAQ Members List Calendar Mark Forums Read

Reply
 
Thread Tools Display Modes
  #1  
Old 03-26-2008, 12:40 AM
latchu latchu is offline
D-Web Trainee
 
Join Date: Sep 2007
Posts: 21
latchu is on a distinguished road
Default newbie question in java

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.
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2  
Old 03-26-2008, 12:47 AM
leoraja8 leoraja8 is offline
D-Web Sr.Programmer
 
Join Date: May 2007
Posts: 190
leoraja8 is on a distinguished road
Default Re: newbie question in java

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 must get a value somehow...
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3  
Old 03-26-2008, 12:49 AM
amansundar amansundar is offline
D-Web Analyst
 
Join Date: May 2007
Posts: 319
amansundar is on a distinguished road
Default Re: newbie question in java

Besides other errors you can make those variables static class variables so the
JVM will initialize them to zero for you.
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4  
Old 03-26-2008, 12:51 AM
latchu latchu is offline
D-Web Trainee
 
Join Date: Sep 2007
Posts: 21
latchu is on a distinguished road
Default Re: newbie question in java

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.
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5  
Old 03-26-2008, 12:52 AM
latchu latchu is offline
D-Web Trainee
 
Join Date: Sep 2007
Posts: 21
latchu is on a distinguished road
Default Re: newbie question in java

Quote:
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 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.
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #6  
Old 03-26-2008, 12:55 AM
amansundar amansundar is offline
D-Web Analyst
 
Join Date: May 2007
Posts: 319
amansundar is on a distinguished road
Default Re: newbie question in java

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.

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
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #7  
Old 03-26-2008, 12:57 AM
latchu latchu is offline
D-Web Trainee
 
Join Date: Sep 2007
Posts: 21
latchu is on a distinguished road
Default Re: newbie question in java

I got that far but when I compiled.. am getting num wasn't initialized.. And I don't know how to fix that. So I erased it and posted what I had.
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #8  
Old 03-26-2008, 12:59 AM
latchu latchu is offline
D-Web Trainee
 
Join Date: Sep 2007
Posts: 21
latchu is on a distinguished road
Default Re: newbie question in java

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
}

}
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #9  
Old 03-26-2008, 01:00 AM
latchu latchu is offline
D-Web Trainee
 
Join Date: Sep 2007
Posts: 21
latchu is on a distinguished road
Default Re: newbie question in java

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
}

}
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #10  
Old 03-26-2008, 01:02 AM
leoraja8 leoraja8 is offline
D-Web Sr.Programmer
 
Join Date: May 2007
Posts: 190
leoraja8 is on a distinguished road
Default Re: newbie question in java

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!)
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Hi Newbie here! shifanyroma Introductions 2 08-14-2009 09:43 PM
newbie pramod Introductions 2 07-08-2009 11:00 PM
Newbie here :) angel_dust Introductions 3 07-05-2009 02:13 AM
Newbie! taramichael Introductions 2 07-05-2009 01:22 AM


All times are GMT -7. The time now is 04:46 PM.


Copyright ©2004 - 2007, DiscussWeb. All Rights Reserved.
Our Partners
One Way Moving Companies | Stamford Dentist | Euro Millions Lottery | Home Loans| Furniture

SEO by vBSEO 3.0.0