• Post Reply Bookmark Topic Watch Topic
  • New Topic

Trying to get the sum of pairs that equal to a prime number  RSS feed

 
shifa farheen
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying to get four input from the user and try to find pairs that equal to a prime number just by using user defined methods. i din reach the prime step yet. But before that i ran into a problem. So jus to check the sum I TRIED printing it out in the method called sumPair6 but it prints zero, so the sum is returning 0 for everything. don't know what mistake am i doing. I go t rid of the initial 0 too but still doesn't not work




 
fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How do you know it's returning 0? You don't seem to save those values anywhere...
 
shifa farheen
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
fred rosenberger wrote:How do you know it's returning 0? You don't seem to save those values anywhere...

I ran this program and when i put the input. just to check what is the sum, I printed out and it printed 0
Enter positive integer :1
Enter positive integer :2
Enter positive integer :3
Enter positive integer :4

0
 
Norm Radder
Rancher
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
it printed 0 

Which line in the program printed the 0?
 
shifa farheen
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The print statement under method sumPair6 .
 
Norm Radder
Rancher
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What are the values of the variables used to compute the printed value?
 
shifa farheen
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i JSUT TREID 1 2 3 4
 
Carey Brown
Saloon Keeper
Posts: 3310
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I see you've initialized sum1 to zero but I don't see that you're setting it anywhere.
 
Norm Radder
Rancher
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What are the values of the variables in line 100 used to compute the printed value?
 
Carey Brown
Saloon Keeper
Posts: 3310
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

You are passing in the value contained in the static variable "sum1". However, your parameter, also named "sum1" is a local copy so that when you assign a value to it on line 69 you are changing only the local "sum1" not the static "sum1".

On the other hand you return the local "sum1" on line 70 but further up in the program where you call this method you are not doing anything with the returned value.
 
Norm Radder
Rancher
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try doing some debugging to see What are the values of the variables in line 100 used to compute the printed value.
Add a print statement that prints their values so you can see what they are.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
shifa farheen wrote:The print statement under method sumPair6 .

Right. you print the value of sum1 INSIDE the method. That doesn't tell you anything about what the method RETURNS.

Especially becuase you print sum1, but return sum6....
 
Carey Brown
Saloon Keeper
Posts: 3310
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Cross posted to
http://stackoverflow.com/questions/43129823/trying-to-get-the-sum-of-pairs-that-equal-to-a-prime-number
http://itknowledgeexchange.techtarget.com/itanswers/trying-to-get-the-sum-of-pairs-that-equal-to-a-prime-number/
 
Knute Snortum
Sheriff
Posts: 4274
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
shifa farheen,

It's okay to crosspost at CodeRanch, just be upfront about it.  That way you don't waste people's time if your question has already been answered in some other forum.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!