Go Back   CodingForums.com > :: Server side development > Java and JSP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 06-18-2003, 05:51 PM   PM User | #1
Bandog
New to the CF scene

 
Join Date: Jun 2003
Location: Tampa, FL
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Bandog is an unknown quantity at this point
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);
}
}
Bandog is offline   Reply With Quote
Old 06-18-2003, 07:31 PM   PM User | #2
Spookster
Supreme Overlord


 
Spookster's Avatar
 
Join Date: May 2002
Location: Marion, IA USA
Posts: 6,220
Thanks: 4
Thanked 80 Times in 79 Posts
Spookster will become famous soon enough
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.
__________________
Spookster
CodingForums Supreme Overlord
All Hail Spookster
Who gave you that Ugging infraction? Yeah that's right it was me!
Spookster is offline   Reply With Quote
Old 06-18-2003, 07:37 PM   PM User | #3
Jason
Regular Coder

 
Join Date: Feb 2003
Location: California
Posts: 925
Thanks: 0
Thanked 0 Times in 0 Posts
Jason is an unknown quantity at this point
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
Jason is offline   Reply With Quote
Old 06-18-2003, 07:40 PM   PM User | #4
Bandog
New to the CF scene

 
Join Date: Jun 2003
Location: Tampa, FL
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Bandog is an unknown quantity at this point
I am taking an independent Java course(non-credit) and I am trying to figure one of the question in the book.
Bandog is offline   Reply With Quote
Old 06-18-2003, 07:45 PM   PM User | #5
Bandog
New to the CF scene

 
Join Date: Jun 2003
Location: Tampa, FL
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Bandog is an unknown quantity at this point
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);
=
Bandog is offline   Reply With Quote
Old 06-18-2003, 07:55 PM   PM User | #6
Jason
Regular Coder

 
Join Date: Feb 2003
Location: California
Posts: 925
Thanks: 0
Thanked 0 Times in 0 Posts
Jason is an unknown quantity at this point
int[] numbercode = {3, 2, 4, 5, 6};
int[] numbercode2 = {9, 7, 12, 15, 17};

use the "{ }" brackets instead...


Jason
Jason is offline   Reply With Quote
Old 06-18-2003, 10:57 PM   PM User | #7
Bandog
New to the CF scene

 
Join Date: Jun 2003
Location: Tampa, FL
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Bandog is an unknown quantity at this point
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);
}
}
Bandog is offline   Reply With Quote
Old 06-18-2003, 11:40 PM   PM User | #8
Jason
Regular Coder

 
Join Date: Feb 2003
Location: California
Posts: 925
Thanks: 0
Thanked 0 Times in 0 Posts
Jason is an unknown quantity at this point
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
Jason is offline   Reply With Quote
Old 06-19-2003, 12:09 AM   PM User | #9
Bandog
New to the CF scene

 
Join Date: Jun 2003
Location: Tampa, FL
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Bandog is an unknown quantity at this point
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);
}
}
Bandog is offline   Reply With Quote
Old 06-19-2003, 10:11 PM   PM User | #10
Jason
Regular Coder

 
Join Date: Feb 2003
Location: California
Posts: 925
Thanks: 0
Thanked 0 Times in 0 Posts
Jason is an unknown quantity at this point
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
Jason is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 07:22 PM.


Advertisement
Log in to turn off these ads.