Results 1 to 2 of 2
  1. #1
    MrHuggykins is offline Member
    Join Date
    Mar 2008
    Posts
    1
    Rep Power
    0

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

  2. #2
    rhm54 is offline Member
    Join Date
    Jan 2008
    Posts
    12
    Rep Power
    0

    Default

    It should look something like this:

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

Similar Threads

  1. Need help with my code.
    By stormviper in forum New To Java
    Replies: 8
    Last Post: 11-18-2013, 04:58 PM
  2. Pls some one to help mi wit this code
    By _nik_ in forum New To Java
    Replies: 3
    Last Post: 02-10-2008, 03:02 AM
  3. I need help on my code
    By jason27131 in forum New To Java
    Replies: 4
    Last Post: 07-28-2007, 05:23 AM
  4. tic tac toe code
    By zoe in forum New To Java
    Replies: 1
    Last Post: 07-23-2007, 05:36 PM
  5. Replies: 1
    Last Post: 04-26-2007, 04:52 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •