Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 03-21-2008, 03:22 AM
Member
 
Join Date: Nov 2007
Posts: 20
Phobos0001 is on a distinguished road
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...

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); } }
thanks in advance for any help, its greatly apriciated
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 03-21-2008, 07:55 AM
Senior Member
 
Join Date: Jul 2007
Posts: 1,189
hardwired is on a distinguished road
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);
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 03-21-2008, 10:15 AM
Member
 
Join Date: Nov 2007
Posts: 20
Phobos0001 is on a distinguished road
awsome thanks for that, it helps so much having an example of how to work around these little problems,

cheers mate/mate-ette
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 03-22-2008, 04:21 AM
Member
 
Join Date: Nov 2007
Posts: 20
Phobos0001 is on a distinguished road
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
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 03-22-2008, 05:26 AM
Senior Member
 
Join Date: Jul 2007
Posts: 1,189
hardwired is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 03-22-2008, 07:23 AM
Member
 
Join Date: Nov 2007
Posts: 20
Phobos0001 is on a distinguished road
sweet as, thanks for clearing that up for ma hardwired, that was very concice

ive bookmarked the array link you provided me, lots of good info condensed down in there, thanks a ton!
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


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

vB 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
Executing a program within a program gibsonrocker800 New To Java 5 05-12-2008 09:24 AM
how to right a program that find kth number in two sorted array? fireball2008 New To Java 8 04-22-2008 04:21 AM
Array program help adelgado0723 New To Java 2 04-16-2008 02:19 PM
How to execute an External Program through Java program Java Tip java.io 0 04-04-2008 03:40 PM
How to execute an External Program through Java program JavaBean Java Tips 0 10-04-2007 10:33 PM


All times are GMT +3. The time now is 02:25 PM.


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