• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

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

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 13014
66
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Master Rancher
Posts: 4509
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The print statement under method sumPair6 .
 
Norm Radder
Master Rancher
Posts: 4509
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i JSUT TREID 1 2 3 4
 
Saloon Keeper
Posts: 8760
71
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Master Rancher
Posts: 4509
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 8760
71
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Master Rancher
Posts: 4509
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 13014
66
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 7111
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic