Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 04-14-2009, 10:37 PM
Member
 
Join Date: Sep 2008
Posts: 29
Rep Power: 0
Reiyn is on a distinguished road
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.

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
Bookmark Post in Technorati
Reply With Quote
  #2 (permalink)  
Old 04-14-2009, 11:17 PM
xcallmejudasx's Avatar
Senior Member
 
Join Date: Oct 2008
Location: Houston, TX & Flint, MI
Posts: 585
Rep Power: 2
xcallmejudasx is on a distinguished road
Send a message via AIM to xcallmejudasx
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.
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.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 04-14-2009, 11:40 PM
Member
 
Join Date: Sep 2008
Posts: 29
Rep Power: 0
Reiyn is on a distinguished road
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
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 04-15-2009, 12:13 AM
Fubarable's Avatar
Moderator
 
Join Date: Jun 2008
Posts: 6,506
Rep Power: 8
Fubarable is on a distinguished road
Default
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.
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 04-15-2009, 12:36 AM
Member
 
Join Date: Sep 2008
Posts: 29
Rep Power: 0
Reiyn is on a distinguished road
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
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 04-15-2009, 12:43 AM
Fubarable's Avatar
Moderator
 
Join Date: Jun 2008
Posts: 6,506
Rep Power: 8
Fubarable is on a distinguished road
Default
You set the size of the array by initializing it with that size:
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:
Code:
int[] myArray = new int[100];
Bookmark Post in Technorati
Reply With Quote
  #7 (permalink)  
Old 04-16-2009, 02:37 AM
Member
 
Join Date: Sep 2008
Posts: 29
Rep Power: 0
Reiyn is on a distinguished road
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
Bookmark Post in Technorati
Reply With Quote
  #8 (permalink)  
Old 04-16-2009, 03:58 AM
Member
 
Join Date: Mar 2009
Posts: 25
Rep Power: 0
hawaiian robots is on a distinguished road
Default
Do you really need to use an array for this?
Bookmark Post in Technorati
Reply With Quote
  #9 (permalink)  
Old 04-16-2009, 04:12 AM
Senior Member
 
Join Date: Mar 2009
Posts: 392
Rep Power: 2
Singing Boyo is on a distinguished road
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!
Bookmark Post in Technorati
Reply With Quote
  #10 (permalink)  
Old 04-16-2009, 07:38 PM
Member
 
Join Date: Sep 2008
Posts: 29
Rep Power: 0
Reiyn is on a distinguished road
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?

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
Bookmark Post in Technorati
Reply With Quote
  #11 (permalink)  
Old 04-16-2009, 11:47 PM
Member
 
Join Date: Sep 2008
Posts: 29
Rep Power: 0
Reiyn is on a distinguished road
Default
Here is the finished assignment, do you see any way i could do it with fewer lines?
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
Bookmark Post in Technorati
Reply With Quote
  #12 (permalink)  
Old 04-17-2009, 12:28 AM
Senior Member
 
Join Date: Sep 2008
Posts: 564
Rep Power: 2
emceenugget is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Multi Client Server applications, troubles with File I/O zackpudil Networking 3 01-29-2010 07:53 AM
How to add an integer to a array element and the store that backinto an array. Hannguoi New To Java 1 03-31-2009 07:40 AM
String array to byte array?! Joe2003 Advanced Java 5 02-28-2009 07:09 AM
StreamCorruptedException and Casting troubles Wassa Networking 2 02-18-2009 04:07 PM
Array Reflection: Multi Array Reflection Java Tip java.lang 0 04-23-2008 09:08 PM


All times are GMT +2. The time now is 05:39 PM.



VBulletin, Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2009, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org