• 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
  • Paul Clapham
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Frits Walraven
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • salvin francis
  • fred rosenberger

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

 
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: 12856
52
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: 3797
35
  • 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: 3797
35
  • 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
 
Saloon Keeper
Posts: 7179
65
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
 
Norm Radder
Rancher
Posts: 3797
35
  • 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: 7179
65
Eclipse IDE Firefox Browser MySQL Database VI Editor Java 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: 3797
35
  • 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: 12856
52
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....
 
Sheriff
Posts: 7108
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • 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.
 
If a regular clown is funny, then a larger clown would be funnier. Math. Verified by this tiny ad:
Try Free Java/.NET Libraries for Word Excel PowerPoint and PDF
htttp://www.e-iceblue.com/free-apis.html
    Bookmark Topic Watch Topic
  • New Topic