
04-14-2009, 10:37 PM
|
|
Member
|
|
Join Date: Sep 2008
Posts: 29
Rep Power: 0
|
|
[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
|
|

04-14-2009, 11:17 PM
|
 |
Senior Member
|
|
Join Date: Oct 2008
Location: Houston, TX & Flint, MI
Posts: 585
Rep Power: 2
|
|
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.
|
|

04-14-2009, 11:40 PM
|
|
Member
|
|
Join Date: Sep 2008
Posts: 29
Rep Power: 0
|
|
|
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
|
|

04-15-2009, 12:13 AM
|
 |
Moderator
|
|
Join Date: Jun 2008
Posts: 6,506
Rep Power: 8
|
|
|
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.
|
|

04-15-2009, 12:36 AM
|
|
Member
|
|
Join Date: Sep 2008
Posts: 29
Rep Power: 0
|
|
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
|
|

04-15-2009, 12:43 AM
|
 |
Moderator
|
|
Join Date: Jun 2008
Posts: 6,506
Rep Power: 8
|
|
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]; |
|
|

04-16-2009, 02:37 AM
|
|
Member
|
|
Join Date: Sep 2008
Posts: 29
Rep Power: 0
|
|
|
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
|
|

04-16-2009, 03:58 AM
|
|
Member
|
|
Join Date: Mar 2009
Posts: 25
Rep Power: 0
|
|
|
Do you really need to use an array for this?
|
|

04-16-2009, 04:12 AM
|
|
Senior Member
|
|
Join Date: Mar 2009
Posts: 392
Rep Power: 2
|
|
|
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!
|
|

04-16-2009, 07:38 PM
|
|
Member
|
|
Join Date: Sep 2008
Posts: 29
Rep Power: 0
|
|
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
|
|

04-16-2009, 11:47 PM
|
|
Member
|
|
Join Date: Sep 2008
Posts: 29
Rep Power: 0
|
|
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
|
|

04-17-2009, 12:28 AM
|
|
Senior Member
|
|
Join Date: Sep 2008
Posts: 564
Rep Power: 2
|
|
|
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.
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT +2. The time now is 05:39 PM.
|
|