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

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


    You know? After you posted this every time I create something I think of this post? lol

    Comment

    • r035198x
      MVP
      • Sep 2006
      • 13225

      #17
      Do
      [CODE=java]for(int i =0;i < anArray.length; i++ ) {
      System.out.prin tln(anArray[i]);
      }[/CODE]

      before that string of anArray[i] = 0; to find out for yourself what values the array locations are initialized to.

      Comment

      • r035198x
        MVP
        • Sep 2006
        • 13225

        #18
        Originally posted by MrHuggykins
        You know? After you posted this every time I create something I think of this post? lol
        Now here's a little challenge. Use a for-loop for capturing the numbers from the user so that your code becomes shorter(fewer lines of code).

        Comment

        • MrHuggykins
          New Member
          • Mar 2008
          • 27

          #19
          Originally posted by r035198x
          Do
          [CODE=java]for(int i =0;i < anArray.length; i++ ) {
          System.out.prin tln(anArray[i]);
          }[/CODE]

          before that string of anArray[i] = 0; to find out for yourself what values the array locations are initialized to.
          I don't know what all of this means..
          java.lang.NullP ointerException
          at sun.misc.Launch er$AppClassLoad er.loadClass(La uncher.java:269 )
          at java.lang.Class Loader.loadClas s(ClassLoader.j ava:299)
          at java.lang.Class Loader.loadClas s(ClassLoader.j ava:251)
          at bluej.runtime.E xecServer$3.run (ExecServer.jav a:787)
          All the ints are zero instead of the user entering them and I am really confused, heh. I can't find enough information about arrays >.<

          Comment

          • sukatoa
            Contributor
            • Nov 2007
            • 539

            #20
            Originally posted by MrHuggykins
            I don't know what all of this means..
            java.lang.NullP ointerException
            at sun.misc.Launch er$AppClassLoad er.loadClass(La uncher.java:269 )
            at java.lang.Class Loader.loadClas s(ClassLoader.j ava:299)
            at java.lang.Class Loader.loadClas s(ClassLoader.j ava:251)
            at bluej.runtime.E xecServer$3.run (ExecServer.jav a:787)
            All the ints are zero instead of the user entering them and I am really confused, heh. I can't find enough information about arrays >.<
            Can you post your updated codes?

            sukatoa

            Comment

            • pralu
              New Member
              • Mar 2008
              • 34

              #21
              this is the way u should do it......
              • import java.io.*;
                import javax.swing.*;

                class Number
                {
                public static void main (String[] args) throws IOException
                {
                String inData;
                int num,num2;
                int sum=0;
                int avg=0;

                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);

                sum=num+num2;
                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"); //always executed
                }

                }

              Comment

              • r035198x
                MVP
                • Sep 2006
                • 13225

                #22
                Originally posted by pralu
                this is the way u should do it......


                • import java.io.*;

                  import javax.swing.*;



                  class Number

                  {

                  public static void main (String[] args) throws IOException

                  {

                  String inData;

                  int num,num2;

                  int sum=0;

                  int avg=0;



                  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);



                  sum=num+num2;

                  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"); //always executed

                  }



                  }
                1.) Use code tags when posting code.
                2.) Do not attempt to spoonfeed, it's against site rules.
                3.) Always declare variables closest to where they are used.
                4.) There already is a class called Number in the java.lang package. Always try to make sure that your class names do not clash with standard Java classes.
                5.) The OP wants to get the average of possibly more than two numbers.

                Comment

                • Navdip
                  New Member
                  • Mar 2008
                  • 22

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

                  hi...
                  you should try the following code to fix the problem..
                  your code is givin problem cause u r displaying something without giving any value to them...


                  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);

                  sum=num+num2;
                  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

                  • r035198x
                    MVP
                    • Sep 2006
                    • 13225

                    #24
                    Originally posted by Navdip
                    hi...
                    you should try the following code to fix the problem..
                    your code is givin problem cause u r displaying something without giving any value to them...


                    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);

                    sum=num+num2;
                    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
                    }

                    }
                    Please read items numbered 1,2,3, and 5 in my reply (post #22) above.

                    Comment

                    • MrHuggykins
                      New Member
                      • Mar 2008
                      • 27

                      #25
                      I need to make a program in java that does this with if statement. /*greater than or equal to 10 ifPGM
                      * Display a "happy Message"
                      *
                      * else
                      * if value less than 10 ;
                      * display an error message
                      */

                      Comment

                      • BigDaddyLH
                        Recognized Expert Top Contributor
                        • Dec 2007
                        • 1216

                        #26
                        Originally posted by MrHuggykins
                        I need to make a program in java that does this with if statement. /*greater than or equal to 10 ifPGM
                        * Display a "happy Message"
                        *
                        * else
                        * if value less than 10 ;
                        * display an error message
                        */
                        Can you post your best effort at this and then ask a specific question about the code?

                        Comment

                        • questionit
                          Contributor
                          • Feb 2007
                          • 553

                          #27
                          Originally posted by MrHuggykins
                          I need to make a program in java that does this with if statement. /*greater than or equal to 10 ifPGM
                          * Display a "happy Message"
                          *
                          * else
                          * if value less than 10 ;
                          * display an error message
                          */
                          To do this, you need to know how to use 'if statement' and how to display a message.

                          If you can work on these well, there would be no difficulty in using these properly to accomplish what you are trying to do.

                          The initial declarations and writing main class in Java are just the basics. i hope you can put these all together right now.


                          Regards
                          Qi

                          Comment

                          Working...