Results 1 to 2 of 2
Thread: Help with array in java
- 08-06-2007, 03:42 AM #1
Member
- Join Date
- Jul 2007
- Posts
- 39
- Rep Power
- 0
Help with array in java
Hi, I am trying to code the following information, however I am getting some error when I try to add array 5, than add array 6 and add both array 5 and 6 together to get the total sum
Using separate arrays, code the following information:
Sortfriends.javaJava Code:Array #1] 34232, John Doe, 3xcvbn Array #2] 90323, Bill Reb, 2129uio Array #3] 10021, Eric Quick, 3898qas Array #4] 91920, Bob Bark, hjd2314 Array #5] 3, 2, 4, 5, 6 Array #6] 9, 7, 12, 15, 17 Display on the screen the following output: Secret Route----Name-----Code 3xcvbn........John Doe......34232 2129uio......Bill Reb.........90323 3898qas.....Eric Quick.....10021 hjd2314......Bob Bark......91920 Output the SUM of array5 (The value that is returned should be obtained by adding up all the numbers in the array) Output the SUM of array6 (The value that is returned should be obtained by adding up all the numbers in the array) Output the SUM of array5 + array6
Thanks.Java Code:public class Sortfriends { public static void main (String[] args) { Contact[]friends = new Contact[5]; friends [0] = new Contact ("Secret Route", "Name", "code"); friends [1] = new Contact ("3xcvbn", "John Doe", "34232"); friends [2] = new Contact ("2129uio", "Bill Rep", "90323"); friends [3] = new Contact ("3898qas", "Eric Quick", "10021"); friends [4] = new Contact ("hjd234", "Bob Bark", "91920"); int[] numbercode = (3, 2, 4, 5, 6); int[] numbercode2 = (9, 7, 12, 15, 17); for (int index=0; index < 5; ++index) System.out.println (friends[index]); int sum1 = 0; int sum2 = 0; for (int i = 0; i < numbercode.length; i++) sum1 += numbercode[i]; System.out.println (sum1); for (int i = 0; i < numbercode2.length; i++) sum2 += numbercode2[i]; System.out.println (sum2); System.out.println (sum1 + sum2); } }
- 08-06-2007, 04:03 AM #2
Use curley braces for array instantiation, not parentheses:Java Code:C:\jexp>javac v.java v.java:6: ')' expected int[] numbercode = (3, 2, 4, 5, 6); ^ v.java:7: ')' expected int[] numbercode2 = (9, 7, 12, 15, 17); ^
Java Code:int[] numbercode = { 3, 2, 4, 5, 6 }; int[] numbercode2 = { 9, 7, 12, 15, 17 };
Similar Threads
-
Java Array of Structure
By PAffiliates in forum New To JavaReplies: 1Last Post: 01-28-2008, 06:08 AM -
how to pass array in java?
By sivasayanth in forum New To JavaReplies: 3Last Post: 01-13-2008, 04:33 PM -
how to convert a Java array to a java stack?
By pompeez in forum New To JavaReplies: 2Last Post: 08-13-2007, 02:41 PM -
Help with string and array in java
By zoe in forum New To JavaReplies: 1Last Post: 08-07-2007, 06:12 AM -
Help with array and files in java
By fernando in forum New To JavaReplies: 1Last Post: 07-31-2007, 07:52 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks