• Post Reply Bookmark Topic Watch Topic
  • New Topic

Perfect Numbers  RSS feed

 
Shal Lango
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
An integer  number is said to be a perfect number if its factors, including 1 (but not the number itself), sum  to the number. For example, 6 is a perfect number, because 6 = 1 + 2 + 3. Write a method  isPerfect that determines whether  parameter  number is a perfect number. Use this method  in an application that displays all the perfect numbers between  1 and and integer  entered by the user. Display the factors of each perfect number to confirm that the number is indeed perfect. Challenge the computing power of your computer by testing numbers much larger  than 1000. Display the results.

This is what I have so far:

Anyone have any suggestions for these errors? Thank you!
PerfectNumbers.java:7: error: cannot find symbol
  user = input.nextInt();
  ^
  symbol:   variable user
  location: class PerfectNumbers
PerfectNumbers.java:7: error: non-static variable input cannot be referenced from a static context
  user = input.nextInt();
         ^
PerfectNumbers.java:8: error: cannot find symbol
    for (int num = 1; num <= user; num++) {
                             ^
  symbol:   variable user
  location: class PerfectNumbers
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here, the variable "input" is of type "Scanner"


But what type is "user"?
 
Liutauras Vilda
Sheriff
Posts: 4916
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1. Also, what this variable 'user' represents? It is a very poor variable name need to say. At least would be 'userInput', but that wouldn't be good enough too, as you don't know what this input is meant to represent - try to come up with a better name.

2.
Shalmika Elango wrote:
Why do you return 'Boolean'? Look for the difference between 'boolean' and 'Boolean'. In your case most likely you need to use former.

3.
Shalmika Elango wrote:
Could you think of a case how to simplify this expression to a one liner?


4. Most important: why do you think code formatting and indentation are least important in programming?



 
Ivan Jozsef Balazs
Rancher
Posts: 999
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
isPerfect is called twice, once to tell whether it is a perfect number and once to yield the factors of the number, according to the output text.
 
Campbell Ritchie
Marshal
Posts: 56525
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why have you got so much code in the main method? That code shou‍ld be moved out. What about creating a PerfectNumber class:-You can shorten that to
System.out.println(new PerfectNumber(123456));
MyClass.java:999: error: non-static variable XXXXXX cannot be referenced from a static context
I think that error message is misleading. It lets you think the answer is to add the keyword static to things. The correct solution is to move something else out of the static ontext. I think the error message would have been better as:-
MyClass.java:999: error: non-static variable XXXXXX can only be referenced from a non-static context
Better still to call non‑static “instance”
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!