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
  #1 (permalink)  
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
Sponsored Links
  #2 (permalink)  
Old 03-26-2008, 12:47 AM
leoraja8 leoraja8 is offline
D-Web Sr.Programmer
 
Join Date: May 2007
Posts: 194
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 (permalink)  
Old 03-26-2008, 12:49 AM
amansundar amansundar is offline
D-Web Analyst
 
Join Date: May 2007
Posts: 320
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 (permalink)  
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 (permalink)  
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 (permalink)  
Old 03-26-2008, 12:55 AM
amansundar amansundar is offline
D-Web Analyst
 
Join Date: May 2007
Posts: 320
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 (permalink)  
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 (permalink)  
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 (permalink)  
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 (permalink)  
Old 03-26-2008, 01:02 AM
leoraja8 leoraja8 is offline
D-Web Sr.Programmer
 
Join Date: May 2007
Posts: 194
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
  #11 (permalink)  
Old 03-26-2008, 01:05 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

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

Thanks for all the help/replies guys.this community brings out me a lot.
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #12 (permalink)  
Old 03-28-2008, 03:17 AM
amansundar amansundar is offline
D-Web Analyst
 
Join Date: May 2007
Posts: 320
amansundar is on a distinguished road
Default Re: newbie question in java

Take a look at this link
http://java.sun.com/docs/books/tuto...lts/arrays.html
__________________
cheers
Aman
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #13 (permalink)  
Old 03-28-2008, 03:21 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 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.
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #14 (permalink)  
Old 03-28-2008, 03:23 AM
amansundar amansundar is offline
D-Web Analyst
 
Join Date: May 2007
Posts: 320
amansundar is on a distinguished road
Default Re: newbie question in java

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
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 On
Pingbacks are On
Refbacks are On


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


All times are GMT -7. The time now is 02:43 PM.


Copyright ©2004 - 2007, DiscussWeb. All Rights Reserved.

SEO by vBSEO 3.0.0