Win a copy of Java 9 Revealed this week in the Features new in Java 9 forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Fortune generator  RSS feed

 
Leeana Lee
Greenhorn
Posts: 5
Chrome Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So what this is supposed to do is generate a fortune in the format of

A special fortune for (name): " randomly picked fortune."
n1-n2-n3-n4-n5-n6

i made three different classes for each since i'm new to this, pt one asking for the name, pt 2 with the arraylist of fortunes and pt 3 with the numbers. putting 1 + 2 together gives me two errors, [line: 16] Error: The method getFortune() is undefined for the type fortune
[line: 25] Error: This method must return a result of type java.lang.String


here's the code
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 36634
475
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is the method under discussion:



There's no return statement. It prints a fortune, but doesn't return one. You could change the return type to void or add a return statement.
 
Campbell Ritchie
Sheriff
Posts: 54033
130
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, what does fortune String mean in line 19? Are you trying to pass a String, in which case it woud read String fortune. Type first, name afterwards. But then you will have two things called fortune, which won't work. Do you actually want that method parameter at all?
 
Henry Wong
author
Sheriff
Posts: 23026
120
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

There actually seems to be a lot of compiler errors happening. Anyway, as for line 16, which hasn't been mentioned yet.... The getFortune() method doesn't exist in the class where it is used. It exists in another class, but there is no indication of that in how it is used; basically, how the method is called, requires that the method be in the same class.

Henry
 
Leeana Lee
Greenhorn
Posts: 5
Chrome Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Also, what does fortune String mean in line 19? Are you trying to pass a String, in which case it woud read String fortune. Type first, name afterwards. But then you will have two things called fortune, which won't work. Do you actually want that method parameter at all?


string is supposed to be the return type! i forgot what came before the return type and was googling it after posting this ;;
 
Leeana Lee
Greenhorn
Posts: 5
Chrome Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:
There actually seems to be a lot of compiler errors happening. Anyway, as for line 16, which hasn't been mentioned yet.... The getFortune() method doesn't exist in the class where it is used. It exists in another class, but there is no indication of that in how it is used; basically, how the method is called, requires that the method be in the same class.

Henry


i made them in the same class now! but i'm still confused. how would i call to getFortune? i keep getting this compile error: [line: 15]
Error: The method getFortune(fortune) in the type fortune is not applicable for the arguments ()
 
Henry Wong
author
Sheriff
Posts: 23026
120
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Leeana Lee wrote:
i made them in the same class now! but i'm still confused. how would i call to getFortune? i keep getting this compile error: [line: 15]
Error: The method getFortune(fortune) in the type fortune is not applicable for the arguments ()


As I mentioned earlier, you have lots of compiler errors happening... In this case, the compiler is complaining that you are trying to call the getFortune() method that takes no arguments. And the best that it can find is a method that takes a "fortune" type argument.

Henry 
 
Henry Wong
author
Sheriff
Posts: 23026
120
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Leeana Lee wrote:
Campbell Ritchie wrote:Also, what does fortune String mean in line 19? Are you trying to pass a String, in which case it woud read String fortune. Type first, name afterwards. But then you will have two things called fortune, which won't work. Do you actually want that method parameter at all?


string is supposed to be the return type! i forgot what came before the return type and was googling it after posting this ;;


I think Campbell was asking about the "String" in the argument, and not about the "String" in the return type. Are you really trying to declare a variable named "String"?

Henry
 
Campbell Ritchie
Sheriff
Posts: 54033
130
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:. . . I think Campbell was asking about the "String" in the argument, and not about the "String" in the return type.
You think correctly.
Are you really trying to declare a variable named "String"?

Henry
It is a very bad idea to declare a variable with the same name as a class. You can cause yourself no end of confusion like that. This style guide might be old, but I don't think anything in that section shou‍ld be changed. Except that they have swapped the words variable and constants in one place. There are good reasons for those conventions, which will prevent you causing yourself trouble with parameters called String.
In view of the compiler errors you are getting, I think you would be better off removing that parameter altogether (as I suggested last week).

I am not convinced that random googling is the best way to sort out your programming errors. You never know what the quality of the advice given will be.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!