• Post Reply Bookmark Topic Watch Topic
  • New Topic

This method must return a result of type int, at the first class  RSS feed

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is the first class :



This is the second class :




The error is : this method must return a result of type int, at the first class.

What should I do?

[Added code tags - see UseCodeTags for details]
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to The Ranch!

Sorry, but I'm not sure what you mean. Which method are you referring to? There are only three methods in the second class, and none of them can return an int. If you're getting an error, then you need to explain to us what the error is (the exact error message and the line affected, for instance). If not, it might better if you explain what you're trying to do, and what the problem is now.

By the way, looking at your Arithmetic class - do you realise that for loop isn't doing anything? The first time round the loop (when i = 1) it immediately returns a value. As soon as it does that the method exits - the following values of the loop are completely ignored.
 
Bartender
Posts: 563
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's a short runnable example using your own code that demonstrates a few things:


One of the first things it demonstrates is that you didn't have to post all of that code to show your problem. If you remove line 15 from the above, you'll get the same error.
It demonstrates how to fix your error - add line 15 - which hints at the reason for the error. The compiler does not consider all return statements buried in loops and conditional statements as "reachable." Rather, it seems to consider those return statements first as unreachable, looking for a return statement that it knows for sure will be executed. Without line 15, the compiler didn't have that assurance.
The next point is that the simple example above shows that the method returns one int. (I changed the starting point to 2 so that you could see the return was coming from inside the loop.) Why do you have the return statement in a loop in the first place? What were you hoping to achieve with that?
I was thinking there was one more point I wanted to make, but I lost it.
Hope this helps.
 
Greg Brannon
Bartender
Posts: 563
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ah! That was the other thing.

As MB pointed out, it also demonstrates that the return occurs on the first iteration of the for loop, so the rest of the for loop is unused.

It also illustrates that without knowing the answer, you couldn't have posted a short, runnable example. Sometimes you have to post a short, unrunnable example - or at least that's what's desired, when a runnable one isn't possible.
 
Cathy William
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would like to display a list of multiplication result when the user click 'Check your answer!', but it will only show one result, not a list. How to solve the problem of the for loop which is not working?
 
Greg Brannon
Bartender
Posts: 563
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm trying to follow your logic in the actionPerformed() method and having trouble. I changed it slightly so that the answers it gives are correct. Do you mean that you want to show a historical list of the user's answers? What you're doing currently is printing a list of 12 results, each on top of the previous, but the results shown are driven by the iteration variable, i, rather than by the user's input. If you want to print a history of results, you can add to the checkField using the append() method. I've added my changes before the append() change in the code block below, but here's what the append() change would look like. (I think you mean to show the user's answers (not the correctly computed answer), so you should substitute "inputStr3" for "userInput1 * userInput2" in the line below):



Suggested changes to your actionPerformed() method:



You should also add:

setDefaultCloseOperation( EXIT_ON_CLOSE );

to your JFrame.

Hope this helps. Keep coding.
 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey Cathy,

Not sure what you are trying to do with the 'Arithmetic ' class, but in order to show a list of results in your JTextArea you need to create a string containing the list before you set it to it.

Try the following

======================
MODERATOR ACTION:
Deleted code.

Rajeev Motha, please DontBeACodeMill.(⇐click)
======================
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!