Results 1 to 6 of 6
- 03-21-2008, 02:22 AM #1
Member
- Join Date
- Nov 2007
- Posts
- 20
- Rep Power
- 0
Need help with my 1st array program
Hi again everyone,
ive been working through my textbook and i'm just starting off with arrays, however im having a little trouble getting the output how i want it to go.
the objective is to enter 5 intergers into an array and display the 5 intergers plus the sum of them added together in a dialog box, ive managed to do this in a terminal window however when i use dialog boxes i have a little issue with each element of the aray being displayed in a seporate box, i'm guessing ive messed up with my "for" loops or something, could someone tell me what ive done wrong or point me in the right direction please? here's what ive got so far...
thanks in advance for any help, its greatly apriciatedJava Code:import javax.swing.*; public class ArrDone { public static void main(String[] args) { final int NUMELS= 5; String s1; int i; int total = 0; // declare and allocate the array int numbers[] = new int[NUMELS]; // allocate the array for (i = 0; i < NUMELS; i++) // Enter the grades { s1 = JOptionPane.showInputDialog("Enter a number: "); numbers[i] = Integer.parseInt(s1); total = total + numbers[i]; } for (i = 0; i < NUMELS; i++) // Display and total the grades { JOptionPane.showMessageDialog(null,"numbers entered where: " + numbers[i] + "\n" + "the sum of all numbers entered is: " + total,"message", JOptionPane.INFORMATION_MESSAGE); } System.exit(0); } }
- 03-21-2008, 06:55 AM #2
Java Code:String digits = ""; for (i = 0; i < NUMELS; i++) // Display and total the grades { digits += numbers[i]; } JOptionPane.showMessageDialog(null, "numbers entered where: " + digits + "\n" + "the sum of all numbers entered is: " + total, "message", JOptionPane.INFORMATION_MESSAGE);
- 03-21-2008, 09:15 AM #3
Member
- Join Date
- Nov 2007
- Posts
- 20
- Rep Power
- 0
awsome thanks for that, it helps so much having an example of how to work around these little problems,
cheers mate/mate-ette
- 03-22-2008, 03:21 AM #4
Member
- Join Date
- Nov 2007
- Posts
- 20
- Rep Power
- 0
i have one final question reguarding arrays,
on of the questions asks me to "write down apropriate notation for the first second and third elements of the following array int dist[15]"
from what i have managed to read all arrays default their element values to zero unless sepcified or untill assigned, is this an incorrect assumption?
thanks again for any help
- 03-22-2008, 04:26 AM #5
all arrays default their element values to zero unless sepcified or untill assigned
This is true for arrays of primitive data types such as int, float, double, long. Each array element is given a default value; zero for those listed.
For objects (vis-a-vis primitives) such as Point, Rectangle, String the array elements are all null.
Each array element is accessed by the array index which starts at zero and goes up to array.length-1.
int[] n = new int[5];
Each element of this array has been initialized by java with the value zero. The elements are accessed as: n[0], n[1], n[2], n[3] and n[4].
String[] s = new String[3];
The array is allocated. Each element is null. You have to assign a value to each element.
For more on arrays you can try Arrays.
- 03-22-2008, 06:23 AM #6
Member
- Join Date
- Nov 2007
- Posts
- 20
- Rep Power
- 0
Similar Threads
-
Executing a program within a program
By gibsonrocker800 in forum New To JavaReplies: 5Last Post: 05-12-2008, 08:24 AM -
how to right a program that find kth number in two sorted array?
By fireball2008 in forum New To JavaReplies: 8Last Post: 04-22-2008, 03:21 AM -
Array program help
By adelgado0723 in forum New To JavaReplies: 2Last Post: 04-16-2008, 01:19 PM -
How to execute an External Program through Java program
By Java Tip in forum java.ioReplies: 0Last Post: 04-04-2008, 02:40 PM -
How to execute an External Program through Java program
By JavaBean in forum Java TipReplies: 0Last Post: 10-04-2007, 09:33 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks