Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 adding arrays
Index -> Programming, Java -> Java Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
cool dude




PostPosted: Sat Jun 10, 2006 9:31 pm   Post subject: adding arrays

i'm trying to add arrays and the only problem i'm having is that on line 17 it says i need a semicolon. i have one but it still shows the same error. here's the code:

code:

public class problem2 {
public static void main(String[] args) {
   long total;
   long[] sum = new long[999999];
   total = 0;
       
   for(long i = 1; i <= 999999; I++){
      if(i = 1){
         sum[1] = 1;
      }
      if(i = 2) {
         sum[2] = 2;
      }
      else{
         sum[i] = sum[i - 2] + sum[i - 1];
         If (sum[i] % 2 = 0){
            total = total + sum[i];
         }
      }
   }
   System.out.println(total);
}
}
Sponsor
Sponsor
Sponsor
sponsor
Andy




PostPosted: Sat Jun 10, 2006 9:59 pm   Post subject: (No subject)

i hope you realize that in java, = is for assignment, and == is for comparison. unlike C++, i = 1 is a statement not an expression, thus if(i = 1) is invalid
cool dude




PostPosted: Sat Jun 10, 2006 10:10 pm   Post subject: (No subject)

i constantly forget that, because othere languages i know don't do that. thanks for pointing that out. Smile anyways, that doesn't solve my problem with the semicolon error on line 17?
wtd




PostPosted: Sat Jun 10, 2006 10:11 pm   Post subject: (No subject)

Also, Java is case-sensitive.
HellblazerX




PostPosted: Sun Jun 11, 2006 9:19 am   Post subject: (No subject)

Also, in your for loop, the variable i should be int, and not long, because arrays only use int as an index pointer. If you stick with long, then you'll have to typecast everytime you call an element. And int does go to 999999, if thats why you decided to use long.

Also, in your for loop:
code:
for(long i = 1; i <= 999999; I++){

The condition should i < 999999 not <=, because arrays start at 0, so they only go up to 999998. Also, there are some problems with your if statements:
code:
if(i = 1){
         sum[1] = 1;
      }
      if(i = 2) {
         sum[2] = 2;
      }
      else{
         sum[i] = sum[i - 2] + sum[i - 1];
         If (sum[i] % 2 = 0){
            total = total + sum[i];
         }
      }

This should all be one if statement, so the if (i == 2) should else if (i == 2). Otherwise, what'll happen is when you go through the first iteration, where i is 1, you'll go through both statements, and you'll come across an error, because you'll be calling sum [-1].
cool dude




PostPosted: Sun Jun 11, 2006 9:41 am   Post subject: (No subject)

thanks although everyone already pointed out those mistakes. just so ppl won't keep pointing out the same mistakes i'll repost my code and i still have the same error, which nobody seems to be telling me about.

code:

public class Problem2 {
        public static void main(String[] args) {
                long total;
                long[] sum = new long[999999];
               
                total = 0;
               
                for(int i = 1; i < 1000000; i++){
                        if(i == 1){
                                sum[1] = 1;
                        }
                        if(i == 2) {
                                sum[2] = 2;
                        }
                        else{
                                sum[i] = sum[i - 2] + sum[i - 1];
                                If (sum[i] % 2 == 0){
                                        total = total + sum[i];
                                }
                        }
                }
                System.out.println(total);
        }
}
wtd




PostPosted: Sun Jun 11, 2006 9:48 am   Post subject: (No subject)

I told you exactly why you're getting the error.

What is on line 17?
HellblazerX




PostPosted: Sun Jun 11, 2006 10:03 am   Post subject: (No subject)

wtd edit: Let cool dude figure it out.

code:
for(int i = 1; i < 1000000; i++){

This doesn't change anything. It's the same thing as saying i <= 999999. Arrays goes from 0 to 1 less than its size, and since your array is of size 999999, it goes from 0 to 999998. You'll still get an ArrayOutBounds error. Also, this line:
code:
if(i == 2) {

You have to change it to this:
code:
else if(i == 2) {

Otherwise, you'll run into another ArrayOutBound error, because you'll be calling with negative numbers.
Sponsor
Sponsor
Sponsor
sponsor
Cervantes




PostPosted: Sun Jun 11, 2006 10:15 am   Post subject: (No subject)

HellblazerX: Do you think wtd had a reason for not specifically stating the error? He's certainly capable of doing so. It's to let the author of the thread figure it out for himself.
cool dude




PostPosted: Sun Jun 11, 2006 6:52 pm   Post subject: (No subject)

wtd wrote:
I told you exactly why you're getting the error.

What is on line 17?


when u say what is on line 17 is that a question or a hint. as well all u told me was that java is case sensitive so i'm guessing that means i used a capital letter when it was supposed to be lower case or vice versa correct? well in that case i'm blind because i honestly don't see where i did that Confused but why would it be telling me that i need a semicolon?
cool dude




PostPosted: Sun Jun 11, 2006 6:57 pm   Post subject: (No subject)

i feel really embarassed right now Embarassed i see wat u mean by case sensitive. i'm an idiot. thanx for pointing it out!
wtd




PostPosted: Sun Jun 11, 2006 6:59 pm   Post subject: (No subject)

Feeling like an idiot is the first step on the road to not feeling like an idiot.
HellblazerX




PostPosted: Mon Jun 12, 2006 5:34 pm   Post subject: (No subject)

Cervantes wrote:
HellblazerX: Do you think wtd had a reason for not specifically stating the error? He's certainly capable of doing so. It's to let the author of the thread figure it out for himself.

Sry, I didn't realize this Embarassed
wtd




PostPosted: Mon Jun 12, 2006 6:26 pm   Post subject: (No subject)

Live and learn. Smile
cool dude




PostPosted: Mon Jun 12, 2006 6:57 pm   Post subject: (No subject)

wtd wrote:
Live and learn. Smile


hahaha today is wtd day of philosophy and quotes lol. we should start a thread with all of wtd philosophy and quotes. wise words for a wise men!
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 15 Posts ]
Jump to:   


Style:  
Search: