Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 03-19-2008, 10:45 PM
Member
 
Join Date: Mar 2008
Posts: 1
MrHuggykins is on a distinguished road
I need help fixing my code.. or non code?
import java.io.*;
import javax.swing.*;

class Numbert



{public static void main (String[] args) throws IOException
{ String inData;
int num ,num2;
int sum= num+num2;
int avg= sum/2;

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 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 need to let the user enter any number they want and get the sum and averages displayed for them. I'm in college and this is the first Java assignment my professor has given us. Any help is appreciated, I really want to get familiar with this language.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 03-19-2008, 11:12 PM
Member
 
Join Date: Jan 2008
Posts: 12
rhm54 is on a distinguished road
It should look something like this:

Code:
public class numbert { public static void main(String[] args) { String inData; int num ,num2; int sum; int avg; 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); 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 } }
The problem was you were trying to intialize sum and avg with the variables num and num2 before you had values there.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
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
Pls some one to help mi wit this code _nik_ New To Java 3 02-10-2008 03:02 AM
I need help on my code jason27131 New To Java 4 07-28-2007 05:23 AM
tic tac toe code zoe New To Java 1 07-23-2007 05:36 PM
Need help with my code. stormviper New To Java 0 07-12-2007 04:18 PM
Generating Code Automatically Using Custom code Template In Eclipse JavaForums Eclipse 1 04-26-2007 04:52 PM


All times are GMT +3. The time now is 10:33 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org