• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Knute Snortum
  • Paul Clapham
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Frits Walraven
Bartenders:
  • Ganesh Patekar
  • Tim Holloway
  • salvin francis

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

 
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




 
lowercase baba
Posts: 12745
51
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
 
Rancher
Posts: 3353
33
  • 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: 3353
33
  • 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
 
Bartender
Posts: 5839
56
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: 3353
33
  • 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
Bartender
Posts: 5839
56
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: 3353
33
  • 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
Posts: 12745
51
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....
 
Marshal
Posts: 5988
156
Chrome Eclipse IDE Java Postgres Database Ubuntu 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.
 
Humans and their filthy friendship brings nothing but trouble. My only solace is this tiny ad:
how do I do my own kindle-like thing - without amazon
https://coderanch.com/t/711421/engineering/kindle-amazon
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!