Results 1 to 2 of 2
  1. #1
    coco is offline Member
    Join Date
    Jul 2007
    Posts
    39
    Rep Power
    0

    Default 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:

    Java 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
    Sortfriends.java
    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);
    }
    }
    Thanks.

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    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);
                                  ^
    Use curley braces for array instantiation, not parentheses:
    Java Code:
    int[] numbercode = { 3, 2, 4, 5, 6 };
    int[] numbercode2 = { 9, 7, 12, 15, 17 };

Similar Threads

  1. Java Array of Structure
    By PAffiliates in forum New To Java
    Replies: 1
    Last Post: 01-28-2008, 06:08 AM
  2. how to pass array in java?
    By sivasayanth in forum New To Java
    Replies: 3
    Last Post: 01-13-2008, 04:33 PM
  3. how to convert a Java array to a java stack?
    By pompeez in forum New To Java
    Replies: 2
    Last Post: 08-13-2007, 02:41 PM
  4. Help with string and array in java
    By zoe in forum New To Java
    Replies: 1
    Last Post: 08-07-2007, 06:12 AM
  5. Help with array and files in java
    By fernando in forum New To Java
    Replies: 1
    Last Post: 07-31-2007, 07:52 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •