Results 1 to 12 of 12
  1. #1
    Reiyn is offline Member
    Join Date
    Sep 2008
    Posts
    29
    Rep Power
    0

    Default [SOLVED] Array troubles, yes I searched...

    This is just a simple lab to take an unknown number of integers and add, average, and also return the number of integers in the array...I get no errors, so I know the reason it won't run is my placement of something ...help!
    Please don't write it out for me, I just need to know where to look in my code, thanks.
    We use NetBeans 5.5, btw.

    Java Code:
    import javax.swing.*;
    public class Main 
    {
        private
                
            int iSum = 0;
            int iNumber;
            int[] Numbers = new int [iNumber];
             
    public void SetMaths()
        {   
            for (int i = 0; i < Numbers.length; i++)
                {
                    String input = JOptionPane.showInputDialog(null, "Enter an integer");
                    int iNumber = Integer.parseInt(input);
                    JOptionPane.showMessageDialog(null, "You entered " + iNumber);
                                   
                if (Numbers.length >= 2)
                {
                    iSum = iSum += iNumber;
                    double dAverage = iSum / Numbers.length;
                    String output = ("You entered " + Numbers.length + "integers.\n The sum is " + iSum + " \n The average is  " + dAverage + "\n" );
                    JOptionPane.showMessageDialog(null, output);
                    System.out.println(output);
                }
                else
                {
                     continue;
                }
           }
    }
            
    
        
        public Main() 
        {
        }
        
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) 
        {
            Main oNumber = new Main();
            oNumber.SetMaths();       
        }    
    }
    Last edited by Reiyn; 04-15-2009 at 12:01 AM. Reason: To specify programming environment

  2. #2
    xcallmejudasx's Avatar
    xcallmejudasx is offline Senior Member
    Join Date
    Oct 2008
    Location
    Houston, TX & Flint, MI
    Posts
    609
    Rep Power
    7

    Default

    I don't think you need to create a new Main object. just call SetMaths from within main.

    Also I'm a little confused with your variable declarations.
    Java Code:
        private
                
            int iSum = 0;
            int iNumber;
            int[] Numbers = new int [iNumber];
    does that declare all those variables as private?
    Liberty has never come from the government.
    Liberty has always come from the subjects of government.
    The history of liberty is the history of resistance.
    The history of liberty is a history of the limitation of governmental power, not the increase of it.

  3. #3
    Reiyn is offline Member
    Join Date
    Sep 2008
    Posts
    29
    Rep Power
    0

    Default

    I'm guessing it would make them private, as in c++. I changed that so each is declared private, the array declaration is not. We are required to use private variables in class,... and our instructor wants us to use object oriented methods so that is why it's written to call the method.
    Do I need to set separate methods to create the array and do the maths maybe? Doesn't seem that should be necessary but I am a no0b lol

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Java Code:
        private
                
            int iSum = 0;
            int iNumber;
            int[] Numbers = new int [iNumber];
    this just calls iSum as private and the others as package access. You need to use a private keyword in front of every variable declared in the class that you want to make private. Also, iNumber is set to a default value of 0, and so your Numbers array will have no elements.

  5. #5
    Reiyn is offline Member
    Join Date
    Sep 2008
    Posts
    29
    Rep Power
    0

    Default

    ahh yes I forgot to tell you the array has to take an unknown number of integers.
    I have changed the declarations of my variables to each independently declared private, the array is not, because I wasn't sure about doing that (if an array should or can be private).
    Every example in our book uses a method to set the size of the array...can I use the for loop variable i to do that? If i do, how do I write the continuation (middle) statement of the for loop?
    Thanks, i will check back in a while, time to make dinner, and a little break may clear my head too:D

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    You set the size of the array by initializing it with that size:
    Java Code:
    // the following declares the array
    int[] myArray;
    
    // the following sets its size
    myArray = new int[100];
    If you know the size to begin with, you can do both steps at the same time:
    Java Code:
    int[] myArray = new int[100];

  7. #7
    Reiyn is offline Member
    Join Date
    Sep 2008
    Posts
    29
    Rep Power
    0

    Default

    ok, that may be causing a problem here, since I wrote it int[] Numbers = new int [iNumber] which is the intake variable in my loop lmao...
    thanks

  8. #8
    Join Date
    Mar 2009
    Posts
    25
    Rep Power
    0

    Default

    Do you really need to use an array for this?

  9. #9
    Singing Boyo is offline Senior Member
    Join Date
    Mar 2009
    Posts
    552
    Rep Power
    6

    Default

    you never actually set the number of ints in your array. If you are required to use arrays, you need to do that. However, if you do NOT need to use arrays, you may want to use an ArrayList<Integer> (You cannot use an ArrayList<int>, as Generics does not allow primitives, it only allows wrapper classes)
    ArrayLists are expandable and allow easy addition, removal, and retrieval of objects. If you used an ArrayList, you would probably want a confirmation window asking the user if they wish to continue.
    If the above doesn't make sense to you, ignore it, but remember it - might be useful!
    And if you just randomly taught yourself to program, well... you're just like me!

  10. #10
    Reiyn is offline Member
    Join Date
    Sep 2008
    Posts
    29
    Rep Power
    0

    Default

    I think this is working correctly so far, I also need to determine the number of even and odd integers added to the array, so I will work on that next. Take a look, can I clean this up at all?

    Java Code:
    import javax.swing.*;
    public class Main 
    {
            private float fSum = 0;
          
            
    public void SetArray()
        {
                {
                    String size = JOptionPane.showInputDialog (null, "How many numbers will you enter?");
                    int[] Numbers = new int [Integer.parseInt(size)];
                    JOptionPane.showMessageDialog(null,"Your array will have " + Numbers.length + " integers in it");
                    
                for (int i = 0; i < Numbers.length; i++)
                {
                    String input = JOptionPane.showInputDialog(null, "Enter a number.");
                    int iNumberToAdd = Integer.parseInt(input);
                    JOptionPane.showMessageDialog(null, "You entered " + iNumberToAdd);
                    Numbers[i] = iNumberToAdd;
                    fSum = fSum += Numbers[i];    
                }
                {
                    float fAverage = fSum / Numbers.length;
                    String output = ("The sum is " + fSum + " \n The average is  " + fAverage + "\n" );
                    JOptionPane.showMessageDialog(null, output);
                    System.out.println(output);
                
                }
                }
        }
    
        
        public Main() 
        {
        }
    
        public static void main(String[] args) 
        {
            Main oNumber = new Main();
            oNumber.SetArray(); 
        }    
    }
    Thanks for your help peoples:)

  11. #11
    Reiyn is offline Member
    Join Date
    Sep 2008
    Posts
    29
    Rep Power
    0

    Default

    Here is the finished assignment, do you see any way i could do it with fewer lines?
    Java Code:
    import javax.swing.*;
    public class Main 
    {
            private float fSum = 0;
            private int NumberOdd;
            private int NumberEven;
            private int NumberPositive;
            private int NumberNegative;
            
    public void SetArray()
        {
                {
                    String size = JOptionPane.showInputDialog (null, "How many numbers will you enter?");
                    int[] Numbers = new int [Integer.parseInt(size)];
                    JOptionPane.showMessageDialog(null,"Your array will have " + Numbers.length + " integers in it");
                    
                for (int i = 0; i < Numbers.length; i++)
                {
                    String input = JOptionPane.showInputDialog(null, "Enter a number.");
                    int iNumberToAdd = Integer.parseInt(input);
                    JOptionPane.showMessageDialog(null, "You entered " + iNumberToAdd);
                    Numbers[i] = iNumberToAdd;
                    fSum = fSum += Numbers[i];  
                    if (Numbers[i] % 2 ==0)
                    {
                        NumberEven ++;
                    }
                    else
                    {
                        NumberOdd ++;
                    }
                    if (Numbers[i] > 0)
                    {
                        NumberPositive ++;
                    }
                    else
                    {
                        NumberNegative ++;
                    }
                }
                {
                    float fAverage = fSum / Numbers.length;
                    String output = ("The sum is " + fSum + " \n The average is  " + fAverage + "\n There are " + NumberEven + " even numbers and " + NumberOdd + " odd numbers \n There are " + NumberPositive + " positive numbers and " + NumberNegative + " negative numbers." );
                    JOptionPane.showMessageDialog(null, output);
                    System.out.println(output);
                }
            }
        }
    
        public Main() 
        {
        }
    
        public static void main(String[] args) 
        {
            Main oNumber = new Main();
            oNumber.SetArray(); 
        }    
    }
    Thanks

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

    Default

    fewer lines? or fewer operations? because technically you can make it all a single line of code.

    anyways, i suppose you can bypass using the "iNumberToAdd" variable and get rid of the default constructor. you might also wanna check for if user input for array size == 0. one last thing, you seem to have excessive bracers in a couple places. doesn't mess with execution, but just not pretty in general.

Similar Threads

  1. Replies: 3
    Last Post: 01-29-2010, 07:53 AM
  2. Replies: 1
    Last Post: 03-31-2009, 07:40 AM
  3. String array to byte array?!
    By Joe2003 in forum Advanced Java
    Replies: 5
    Last Post: 02-28-2009, 07:09 AM
  4. StreamCorruptedException and Casting troubles
    By Wassa in forum Networking
    Replies: 2
    Last Post: 02-18-2009, 04:07 PM
  5. Array Reflection: Multi Array Reflection
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-23-2008, 09:08 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
  •