Results 1 to 8 of 8
  1. #1
    circuspeanuts is offline Member
    Join Date
    Apr 2009
    Posts
    14
    Rep Power
    0

    Default Array project for class

    Yes, I have looked it up in my book and online, but no matter how I reformat my array labs, nothing seems to work, so first off, here's the code that I have.


    import javax.swing.*;
    public class Main
    {
    private
    int UsrNum = 0;
    int Average = 0;
    int TotalSum = 0;
    int Count = 0;
    int [] ArrayofIntegers= new int [UsrNum];

    public void SetTotalandSum()
    {
    do
    {
    String UserNum = JOptionPane.showInputDialog("Please enter a number:");
    UsrNum = Integer.parseInt(UserNum);
    if (UsrNum == 0)
    {
    TotalSum = TotalSum + UsrNum;
    Average = TotalSum/ArrayofIntegers.length;
    JOptionPane.showMessageDialog(null, "The total sum of your numbers is " + TotalSum);
    JOptionPane.showMessageDialog(null, "The average of your numbers is " + Average);
    System.exit(0);
    }
    else
    {
    // ArrayofIntegers.(UsrNum);
    }
    }while (UsrNum != 0);
    }
    /**
    * @param args the command line arguments
    */
    public static void main(String[] args) {
    Main oTnA = new Main();
    oTnA.SetTotalandSum();
    }

    }
    I get no errors, but it won't add to the array each number they input, and it won't add the number of the array together when the user inputs 0. I'm not sure what to do here...

  2. #2
    emceenugget is offline Senior Member
    Join Date
    Sep 2008
    Posts
    564
    Rep Power
    6

    Default

    not sure exactly what you're expecting this program to do, but here are some problems you might wanna check out:

    Java Code:
    int UsrNum = 0;
    
    int [] ArrayofIntegers= new int [UsrNum];
    
    Average = TotalSum/ArrayofIntegers.length;
    among a couple other confusing pieces of code...

  3. #3
    circuspeanuts is offline Member
    Join Date
    Apr 2009
    Posts
    14
    Rep Power
    0

    Default

    oh, forgot to put that in, sorry..

    the program is to take an unspecified amount of integers and find the sum and the average of the integers entered when the user enters 0 and then exit the program.

    may I ask what is confusing about
    Java Code:
    int UsrNum = 0;
    
    int [] ArrayofIntegers= new int [UsrNum];
    
    Average = TotalSum/ArrayofIntegers.length;
    this piece of code?

    int [] ArrayofIntegers = new int [UsrNum]

    ^ This is the declaration of my array and sets the value of it to UsrNum, doesn't it?

  4. #4
    emceenugget is offline Senior Member
    Join Date
    Sep 2008
    Posts
    564
    Rep Power
    6

    Default

    no, you're basically making an array with length 0. then dividing a number by that length. given that you only do anything when user input is 0, your program doesn't seem to do anything but divide 0/0.

  5. #5
    circuspeanuts is offline Member
    Join Date
    Apr 2009
    Posts
    14
    Rep Power
    0

    Default

    and thus why I'm confused.....


    well while I was waiting for a reply, I took a break, wandered around the library looking for a java book that I haven't looked in yet, and this came to mind:

    Java Code:
    import javax.swing.*;
    public class Main
    {
    
        private
                int UserIntsChoice;
                int ArrayLength;
                int [] UserArray = new int [ArrayLength];
                int UsrNum;
                int TotalSum;
                int Average;
    
    
        public void SetIntegers()
        {
            String UserChoiceInts = JOptionPane.showInputDialog("How many numbers would you like to use today?");
            UserIntsChoice = Integer.parseInt(UserChoiceInts);
            JOptionPane.showMessageDialog(null, "Thank you. You will be using " + UserIntsChoice + " numbers with this exercise");
    
            do
            {
                String UserNum = JOptionPane.showInputDialog("Please enter a number (Or press 0 to quit):");
                UsrNum = Integer.parseInt(UserNum);
                UserArray[ArrayLength] = UsrNum;
           
                /* if (UsrNum == 0)
                {
                    TotalSum = TotalSum + UsrNum;
                    Average = TotalSum/UserArray.length;
                    JOptionPane.showMessageDialog(null, "The total sum of your numbers is " + TotalSum);
                    JOptionPane.showMessageDialog(null, "The average of your numbers is " + Average);
                    System.exit(0);
                }
                */
                
            }while (UserArray.length < UserIntsChoice);
    
            TotalSum = TotalSum + UsrNum;
            Average = TotalSum/UserArray.length;
            JOptionPane.showMessageDialog(null, "The total sum of your numbers is " + TotalSum);
            JOptionPane.showMessageDialog(null, "The average of your numbers is " + Average);
            System.exit(0);
        }
        public static void main(String[] args) {
            Main oNumbers = new Main();
            oNumbers.SetIntegers();
        }
    
    }
    I figured I would try a different way. Does this way make more sense?

  6. #6
    emceenugget is offline Senior Member
    Join Date
    Sep 2008
    Posts
    564
    Rep Power
    6

    Default

    without even going into what you're trying to do, you're still not initializing your code appropriately. first, you shouldn't be assigning values outside of methods unless they are declared as final. if you want to initialize variables, do it in the constructor or in another method.

    and once again, this does nothing now, as you don't even have a size to initialize the array's length.
    Java Code:
    int ArrayLength;
    int [] UserArray = new int [ArrayLength];

  7. #7
    circuspeanuts is offline Member
    Join Date
    Apr 2009
    Posts
    14
    Rep Power
    0

    Default

    our instructor has us do it this way for some reason, and I CAN'T give my array a certain value. As I had said in my second post...

    the program is to take an unspecified amount of integers and find the sum and the average of the integers entered
    please explain to me what you mean when you say I shouldn't be assigning values. This is the way I've been doing it since I started...

  8. #8
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    7

    Default User input or ArrayList

    There are two ways around the "array number" issue:
    1)You ask the user how many integers he/she is going to input, create the array with that number and then loop to input each element into the array or ...
    2) You use the ArrayList class, which doesn't need to know the amount of array elements ahead of time.

    ArrayList class & methods:
    ArrayList (Java Platform SE 6)

    Examples:
    Java ArrayList example - AnyExample.com
    Java ArrayList Example
    Java: ArrayList

    Much more examples on the web

    Luck,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

Similar Threads

  1. Replies: 4
    Last Post: 05-13-2011, 10:04 PM
  2. Replies: 8
    Last Post: 03-21-2009, 09:34 AM
  3. Help with class project, random number generator.
    By Christopher The Great in forum New To Java
    Replies: 4
    Last Post: 03-14-2009, 02:12 AM
  4. Replies: 22
    Last Post: 01-05-2009, 01:03 AM
  5. 5 Class Project- Last one before Summer!
    By Bascotie in forum New To Java
    Replies: 11
    Last Post: 05-30-2008, 03:08 AM

Posting Permissions

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