Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.

# Thread: Help with Java programming

1. ## Help with Java programming

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:

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
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);
}
}

• This sounds like a homework assignment? Is it?

Why are you doing a pre-increment here?

for (int index=0; index < 5; ++index)

You should just stick with post-increments unless you absolutely need to pre-increment.

Your loop is is going to go from 1 to 4 as it stands. Did you want it to go from 1 to 6?

Instead of writing it the way you have it, it is usually better to write it in a more readable way like so:

for(int index = 1; index <=4; index++)

Here it is obvious that the loop will go from 1 to 4.

• just so you know you really only have 3 arrays...friends is one array and numbercode* are the other 2. What are the errors? That would help in debugging.

Jason

• I am taking an independent Java course(non-credit) and I am trying to figure one of the question in the book.

• I need the loop to go through 1 through, the main problem when I run the program to the compiler is that it highlight = stating that a name needs to be included after the equal sign.

int[] numbercode = (3, 2, 4, 5, 6);
int[] numbercode2 = (9, 7, 12, 15, 17);
=

• int[] numbercode = {3, 2, 4, 5, 6};
int[] numbercode2 = {9, 7, 12, 15, 17};

use the "{ }" brackets instead...

Jason

• When I used the bracket as stated, the compiler gave me the following error: "Type contact was not found"
Contact[]friends = new Contact[5];contact contact

//Sortfriends.java
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 = 1; index <=6; 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);
}
}

• where did you define contact...also it would appear that you were using contact as a 3 element array in which you still have the '(' instead of the '{'. But now your problem is greater...

Jason

• are you saying that Contact[]friends = new Contact[5]; is not being declared, I was under the impression that I had declared per the examples I saw in the book

//Sortfriends.java
public class Sortfriends
{
public static void main (String[] args)
{
Contact[]friends = new Contact[5
friends [0] = new Contact {"Secret Route", "Name", "code"};
friends [1] = John{"3xcvbn", "John Doe", "34232"};
friends [2] = Bill {"2129uio", "Bill Rep", "90323"};
friends [3] = Eric {("3898qas", "Eric Quick", "10021"};
friends [4] = Bob {"hjd234", "Bob Bark", "91920"};
int[] numbercode = {3, 2, 4, 5, 6};
int[] numbercode2 = {9, 7, 12, 15, 17};
for(int index = 1; index <=6; 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);
}
}

• Well, I could be wrong but Im not so sure that in the java language there is a variable for Contacts as you are using. You are saying that an array of Contacts with the name friends is a new Contacts array with length 5. Now In Java without the declaration of what Contacts are it wont know. Arrays can be of other arrays, of strings, booleans, integers....Contacts must be defined somewhere in the code. Unless it is then the compiler won't know. What is a Contact? Where is it defined. That was my question..or questions...

Jason

•

#### Posting Permissions

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