Results 1 to 12 of 12
- 04-14-2009, 09:37 PM #1
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.
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-14-2009 at 11:01 PM. Reason: To specify programming environment
- 04-14-2009, 10:17 PM #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.
does that declare all those variables as private?Java Code:private int iSum = 0; int iNumber; int[] Numbers = new int [iNumber];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, 10:40 PM #3
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
-
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.Java Code:private int iSum = 0; int iNumber; int[] Numbers = new int [iNumber];
- 04-14-2009, 11:36 PM #5
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:D
-
You set the size of the array by initializing it with that size:
If you know the size to begin with, you can do both steps at the same time:Java Code:// the following declares the array int[] myArray; // the following sets its size myArray = new int[100];
Java Code:int[] myArray = new int[100];
- 04-16-2009, 01:37 AM #7
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, 02:58 AM #8
Member
- Join Date
- Mar 2009
- Posts
- 25
- Rep Power
- 0
Do you really need to use an array for this?
- 04-16-2009, 03:12 AM #9
Senior Member
- Join Date
- Mar 2009
- Posts
- 552
- Rep Power
- 5
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, 06:38 PM #10
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?
Thanks for your help peoples:)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(); } }
- 04-16-2009, 10:47 PM #11
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?
ThanksJava 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(); } }
- 04-16-2009, 11:28 PM #12
Senior Member
- Join Date
- Sep 2008
- Posts
- 564
- Rep Power
- 5
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
-
Multi Client Server applications, troubles with File I/O
By zackpudil in forum NetworkingReplies: 3Last Post: 01-29-2010, 06:53 AM -
How to add an integer to a array element and the store that backinto an array.
By Hannguoi in forum New To JavaReplies: 1Last Post: 03-31-2009, 06:40 AM -
String array to byte array?!
By Joe2003 in forum Advanced JavaReplies: 5Last Post: 02-28-2009, 06:09 AM -
StreamCorruptedException and Casting troubles
By Wassa in forum NetworkingReplies: 2Last Post: 02-18-2009, 03:07 PM -
Array Reflection: Multi Array Reflection
By Java Tip in forum java.langReplies: 0Last Post: 04-23-2008, 08:08 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks