• Post Reply Bookmark Topic Watch Topic
  • New Topic

What to return to get an integer from this code?  RSS feed

 
Brenton Willcocks
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey there everyone, I am having massive trouble on this, have been trying to figure it out for hours, I am new to java, and it is killing me. This compiles, but it says I am missing a return, and I don't know what to return, I have tried returning everything, but it just won't work, if anyone could help, it would be greatly appreciated, this is urgent, thankyou for your replies in advance.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Clearly you're not returning anything in that code, so you know that has to be wrong, and why.

Why not show one of your attempts to return something, and copy/paste the exact complete error message.
 
Brenton Willcocks
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well if I compile it at the moment the error message is "missing return statement"
If I put, say return i; then I get the message "incompatible types"
If I put return intValue; then the message is "cannot find symbol- variable intValue"
 
Brenton Willcocks
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh sorry about that, and thanks, I think I will be using this a lot in the future, I have already used other people solutions to help myself!
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Brenton Willcocks wrote:Well if I compile it at the moment the error message is "missing return statement"


Right. Presumably you understand why.

If I put, say return i; then I get the message "incompatible types"


Okay. So let's think about what that might mean. (And I bet it told you more detail than just that--like it probably told you which two types were incompatible.)

Do you understand what a type is in Java? Do you know what "incompatible" means? When you wrote your method, what type did you decide it would return, and why? What type are you actually returning there?

If I put return intValue; then the message is "cannot find symbol- variable intValue"


That's pretty self explanatory. You have to declare each symbol (variable, method, named thingy) that you use in Java. There's no such thing as intValue because you haven't declared it, at least not in that scope, so it can't be returned.
 
Brenton Willcocks
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, I believe I understand the types, such as I am supposed to return an int, but instead am returning a string, I am not sure how I to change it, or even if that it what I am supposed to be doing.
The reason I added the last one is so I could have a bit of variety in my error messages, and I think I understand declaring part of it.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Brenton Willcocks wrote:Yes, I believe I understand the types, such as I am supposed to return an int, but instead am returning a string, I am not sure how I to change it, or even if that it what I am supposed to be doing.
The reason I added the last one is so I could have a bit of variety in my error messages, and I think I understand declaring part of it.


Do you know what each piece of this line means? In particular the one that relates directly to your problem and one of the error messages you got?




pulblic = ???
Stroke = ???
getStroke = ???
(int i) = ???


If you don't know, you'll want to go back to your book or tutorial to review method declarations.

Such as here: http://docs.oracle.com/javase/tutorial/java/javaOO/methods.html
 
Brenton Willcocks
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
public = meaning that anyone can access this function or information
Stroke = a line segment
getStroke = retreiving a specific stroke
int i = an integer value for the letter i
the thing is, this was given to us as what was described as a skeleton, and we are meant to continue on from it to make the writing in the first 5 lines happen when we run it
 
Brenton Willcocks
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I asked a friend, and he helped me out with it, turns out it was just
public Stroke getStroke(int i)
{

{
Stroke a = drawing.get(i);
return(a);
}
}
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Brenton Willcocks wrote:
Stroke = a line segment
int i = an integer value for the letter i


But what do those mean as far as the Java language is concerned?

Java doesn't know that a Stroke for you is a line segment. What it does know is that public is the access modifier that means the method can be accessed from anywhere. It also knows that getStroke is the method name.

What does it know about Stroke and (int i)? And remember, I'm asking you this because it relates directly to your original question. If you don't know the answer, click the link in my previous post.


 
Vineeth Menon
Ranch Hand
Posts: 79
Eclipse IDE Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Brenton,

Have you heard of AccessModifier Class ClassName ??? I think you ought to read this Java Doc
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Brenton Willcocks wrote:I asked a friend, and he helped me out with it, turns out it was just
public Stroke getStroke(int i)
{

{
Stroke a = drawing.get(i);
return(a);
}
}


But do you understand why that works? Do you understand how this fixes the "incompatible types" error?
 
Brenton Willcocks
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not entirely, because I still see it as it returning a storke, but just naming it a, I don't know why it suddenly is turned into an integer
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Brenton Willcocks wrote:Not entirely, because I still see it as it returning a storke, but just naming it a, I don't know why it suddenly is turned into an integer


It hasn't turned into an integer and I don't know why you think it has.

Actually, I can guess why you think that. It's because you still don't know what the pieces of the line mean. Seriously, click the link.

http://docs.oracle.com/javase/tutorial/java/javaOO/methods.html
 
Brenton Willcocks
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah, I just find it really difficult to understand all this Java lingo, I will get my head around it one day, thanks for all your help, I will definitely read through that link thoroughly, have a good one mate =)
 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A method can be defined to return nothing, or to return <something>. But you have to explicitly state what it will be.

If a method is going to return nothing, you declare it with the null 'void' keyword. That tells the compiler that there will be no line along the lines of "return <whatever>".

Otherwise, you have to tell it the exact type of the thing you are returning. Think of vending machines. The label on a button tells you "When you press THIS button, you will get back THIS kind of item". Java is the same way. You tell the compiler "When THIS method is called, you will get back THIS kind of thing". The compiler then enforces that rule.

So, this line:

says (among other things) that when the method "getName" is called, it will return a String object. That is why if you write code in that method that said


The compiler would complain. "age" in my example is NOT a String, and cannot be made into a string (at least, not automatically by the compiler). If the method is declared to return a String, then it has to return a string no matter what the execution path.

Your example has a similar problem. Your method is declared to return one type, and then you tried to return a different type.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
fred rosenberger wrote:
If a method is going to return nothing, you declare it with the 'null' keyword.


Er, void anyone?
 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeff Verdegan wrote:Er, void anyone?

my bad...corrected. should have had more coffee
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!