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

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • MrHuggykins
    New Member
    • Mar 2008
    • 27

    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?


    --------------------------------------------------
    [CODE=java]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.sho wInputDialog (null, "Enter an Integer Value. NOW!") ;
    num = Integer.parseIn t ( inData); // convert inData to int

    inData = JOptionPane.sho wInputDialog (null, "Enter another Integer Value. Ugh..");
    num2 = Integer.parseIn t ( inData);

    System.out.prin tln ("Sum of both numbers:" +sum);
    System.out.prin tln ("The average is:" +avg);

    System.out.prin tln ("Good-bye for now, suckahfish!"); //always executed
    }

    }[/CODE]
    Last edited by Ganon11; Mar 17 '08, 11:11 PM. Reason: Please use the [CODE] tags provided.
  • Ganon11
    Recognized Expert Specialist
    • Oct 2006
    • 3651

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

    Comment

    • JosAH
      Recognized Expert MVP
      • Mar 2007
      • 11453

      #3
      Besides other errors you can make those variables static class variables so the
      JVM will initialize them to zero for you.

      kind regards,

      Jos

      Comment

      • MrHuggykins
        New Member
        • Mar 2008
        • 27

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

        Comment

        • MrHuggykins
          New Member
          • Mar 2008
          • 27

          #5
          Originally posted by Ganon11
          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.

          Comment

          • JosAH
            Recognized Expert MVP
            • Mar 2007
            • 11453

            #6
            Originally posted by MrHuggykins
            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:

            [code=java]
            sum= num+num2;
            avg= sum/2;
            [/code]

            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

            Comment

            • MrHuggykins
              New Member
              • Mar 2008
              • 27

              #7
              Originally posted by JosAH
              Ok here's a spoiler: what keeps you from doing the following after num and num2
              have reveived their values:

              [code=java]
              sum= num+num2;
              avg= sum/2;
              [/code]

              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.

              Comment

              • MrHuggykins
                New Member
                • Mar 2008
                • 27

                #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.sho wInputDialog (null, "Enter an Integer Value.") ;
                num = Integer.parseIn t ( inData); // convert inData to int

                inData = JOptionPane.sho wInputDialog (null, "Enter another Integer Value.");
                num2 = Integer.parseIn t ( inData);

                System.out.prin tln ("Sum of both numbers:" +sum);
                System.out.prin tln ("The average is:" +avg);

                System.out.prin tln ("Good-bye for now, suckahfish!"); //always executed
                }

                }

                Comment

                • MrHuggykins
                  New Member
                  • Mar 2008
                  • 27

                  #9
                  Originally posted by MrHuggykins
                  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.sho wInputDialog (null, "Enter an Integer Value.") ;
                  num = Integer.parseIn t ( inData); // convert inData to int

                  inData = JOptionPane.sho wInputDialog (null, "Enter another Integer Value.");
                  num2 = Integer.parseIn t ( inData);

                  System.out.prin tln ("Sum of both numbers:" +sum);
                  System.out.prin tln ("The average is:" +avg);

                  System.out.prin tln ("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.sho wInputDialog (null, "Enter an Integer Value.") ;
                  num = Integer.parseIn t ( inData); // convert inData to int

                  inData = JOptionPane.sho wInputDialog (null, "Enter another Integer Value.");
                  num2 = Integer.parseIn t ( inData);

                  int sum= num+num2;
                  int avg= sum/2;

                  System.out.prin tln ("Sum of both numbers:" +sum);
                  System.out.prin tln ("The average is:" +avg);

                  System.out.prin tln ("Good-bye for now, suckahfish!"); //always executed
                  }

                  }

                  Comment

                  • MrHuggykins
                    New Member
                    • Mar 2008
                    • 27

                    #10
                    Okay. My professor introduced arrays last class and now I have to use an array to represent 20 values and display the average.

                    Comment

                    • Ganon11
                      Recognized Expert Specialist
                      • Oct 2006
                      • 3651

                      #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!)

                      Comment

                      • MrHuggykins
                        New Member
                        • Mar 2008
                        • 27

                        #12
                        Originally posted by Ganon11
                        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.

                        Comment

                        • MrHuggykins
                          New Member
                          • Mar 2008
                          • 27

                          #13
                          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

                          Comment

                          • nomad
                            Recognized Expert Contributor
                            • Mar 2007
                            • 664

                            #14
                            Originally posted by MrHuggykins
                            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


                            nomad

                            Comment

                            • MrHuggykins
                              New Member
                              • Mar 2008
                              • 27

                              #15
                              Originally posted by nomad
                              Take a look at this link


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

                              Code:
                              import java.io.*;
                              import javax.swing.*;
                              
                                  class ArrT
                              {
                              
                              public static void main (String[] args) throws IOException {
                                      String inData;
                                      int[] anArray;
                                      
                                      anArray = new int[20];
                                      
                                      anArray[0] = 0;
                                      anArray[1] = 0;
                                      anArray[2] = 0;
                                      anArray[3] = 0;
                                      anArray[4] = 0;
                                      anArray[5] = 0;
                                      anArray[6] = 0;
                                      anArray[7] = 0;
                                      anArray[8] = 0;
                                      anArray[9] = 0;
                                      anArray[10] = 0;
                                      anArray[11] = 0;
                                      anArray[12] = 0;
                                      anArray[13] = 0;
                                      anArray[14] = 0;
                                      anArray[15] = 0;
                                      anArray[16] = 0;
                                      anArray[17] = 0;
                                      anArray[18] = 0;
                                      anArray[19] = 0;          
                                                        
                                      inData = JOptionPane.showInputDialog (null, "Enter an Integer Value.") ;
                                      anArray[0] = Integer.parseInt ( inData); // convert inData to int
                                      
                                      inData = JOptionPane.showInputDialog (null, "Enter another Integer Value.");
                                      anArray[1] = Integer.parseInt ( inData);
                                      
                                      inData = JOptionPane.showInputDialog (null, "Enter another Integer Value.");
                                      anArray[2] = Integer.parseInt ( inData);
                                      inData = JOptionPane.showInputDialog (null, "Enter another Integer Value.");
                                      anArray[3] = Integer.parseInt ( inData);
                                      inData = JOptionPane.showInputDialog (null, "Enter another Integer Value.");
                                      anArray[4] = Integer.parseInt ( inData);
                                      inData = JOptionPane.showInputDialog (null, "Enter another Integer Value.");
                                      anArray[5] = Integer.parseInt ( inData);
                                      inData = JOptionPane.showInputDialog (null, "Enter another Integer Value.");
                                      anArray[6] = Integer.parseInt ( inData);
                                      inData = JOptionPane.showInputDialog (null, "Enter another Integer Value.");
                                      anArray[7] = Integer.parseInt ( inData);
                                      inData = JOptionPane.showInputDialog (null, "Enter another Integer Value.");
                                      anArray[8] = Integer.parseInt ( inData);
                                      inData = JOptionPane.showInputDialog (null, "Enter another Integer Value.");
                                      anArray[9] = Integer.parseInt ( inData);
                                      inData = JOptionPane.showInputDialog (null, "Enter another Integer Value.");
                                      anArray[10] = Integer.parseInt ( inData);
                                      inData = JOptionPane.showInputDialog (null, "Enter another Integer Value.");
                                      anArray[11] = Integer.parseInt ( inData);
                                      inData = JOptionPane.showInputDialog (null, "YOUSUCK! ENTER ANOHTER ONE.");
                                      anArray[12] = Integer.parseInt ( inData);
                                      inData = JOptionPane.showInputDialog (null, "Enter another Integer Value.");
                                      anArray[13] = Integer.parseInt ( inData);
                                      inData = JOptionPane.showInputDialog (null, "Enter another Integer Value.");
                                      anArray[14] = Integer.parseInt ( inData);
                                      inData = JOptionPane.showInputDialog (null, "This isn't over...");
                                      anArray[15] = Integer.parseInt ( inData);
                                      inData = JOptionPane.showInputDialog (null, "Enter another Integer Value.");
                                      anArray[16] = Integer.parseInt ( inData);
                                      inData = JOptionPane.showInputDialog (null, "Suck it blue!");
                                      anArray[17] = Integer.parseInt ( inData);
                                      inData = JOptionPane.showInputDialog (null, "Enter another Integer Value.");
                                      anArray[18] = Integer.parseInt ( inData);
                                      inData = JOptionPane.showInputDialog (null, "Enter another Integer Value.");
                                      anArray[19] = Integer.parseInt ( inData);
                                      
                                      
                                     int sum = anArray[0] + anArray[1] + anArray[2] + anArray[3] + anArray[4] + anArray[5] + anArray[6]
                                     + anArray[7] +  anArray[8] + anArray[9] + anArray[10]  + anArray[11] + anArray[12] + anArray[13] + 
                                     anArray[14] + anArray[15] + anArray[16]  +anArray[17]  + anArray[18] + anArray[19];
                                     int avg = sum/20;
                                      
                                      System.out.println ("Sum of your sorry list of numbers is:" +sum);
                                      System.out.println ("The average is:" +avg);
                                      
                                      System.out.println ("Good-bye for now, suckahfish!"); //always executed
                              }
                              
                              }

                              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.

                              Comment

                              Working...