• Register
1 vote
198 views

Problem:

“This method must return a result of type boolean” What does it mean? I have a sample java program but I can’t run my program due to the above error. Once I try to run the program it gives back the error. Please find my code snippet below to understand my problem:
 

public boolean Winner() {
    for (int a = 0; a < 3; a++) {
            if (board[a] != null && board[a] == board[a+3] && board[a] == board[a+6]
                    ) {
                return true;
            } 
    }
    for(int n=0; n<7;n+=3){
        if (board[n] != null && board[n] == board[n+1] && board[n] == board[n+2]) {
    return true;}
    }
}

Thanks for your valuable time

12 7 7
15,250 points

Please log in or register to answer this question.

1 Answer

1 vote

Solution:

This type-error occurs generally when we missed the return value for a block The Java compiler doesn't make assumptions that a for-loop will have an iteration or that an if-statement block will run. In your case, the function boolean Winner() is not guaranteed to return boolean type statement so far. Just add a boolean type return below the code and boom!

public boolean Winner() {
    for (int a = 0; a < 3; a++) {
            if (board[a] !=null && board[a]==board[a+3] && board[a]== board[a+6]{
            return true;
           } 
    }
    for(int n=0; n<7;n+=3){
        if (board[n] != null && board[n] == board[n+1] && board[n] == board[n+2]{
    return true;}
    }
    return false;
}

You are all set to go.

13 9 6
94,240 points

Related questions

0 votes
1 answer 7 views
7 views
What am I doing wrong?
asked Jan 24 TeamScript 13.5k points
1 vote
2 answers 68 views
68 views
Problem: Hello Kodlogs, I have a simple java code that I wrote a few minutes back I was trying to compare numbers using methods. The program throws the error again and again: this method must return a result of type int. I couldn&rsquo;t figure out the problem. Could you please shed some light on this ... []) { int x = 8; int y = 9; int z = 20; System.out.println(largest(x, y, z)); } } Many thanks
asked Mar 27, 2020 Gavin 15.3k points
0 votes
1 answer 31 views
31 views
Why is this so?
asked Jan 1 TeamScript 13.5k points
1 vote
1 answer 46 views
46 views
Problem: I just started to learn java programming and I am very noob a it. I wrote a code which shows me error Main.java:1: error: class, interface, or enum expected { ^ Main.java:3: error: class, interface, or enum expected public String getName() ^ Main.java:6: ... main { public static void main (String [] args){ system.out.println(emp1.getname()); } } Please help me out to solve this problem.
asked Apr 7, 2020 LizzyM 6.1k points
0 votes
1 answer 12 views
12 views
Problem: Hello there! I am working with C# in Visual Studio and I am learning user-defined functions in C#. When I try to execute my program, it throws an error that says method must have a return type. I have checked my function and I find nothing wrong with it. ... ;t know what&rsquo;s the problem and what should I do. If someone could tell me, that would be a big favor. Thanks in advance
asked Oct 16, 2020 Code Learner 5.8k points
1 vote
1 answer 406 views
406 views
Problem: I wrote a program in which gives me the following error : TypeError: startswith first arg must be bytes or a tuple of bytes, not str The program I wrote is: def allFasta(filename): xd = open(filename, 'yb') headers = "" sequence = "" while True: ... ("youn.xd") Why is this type error message is appearing for this program. Is there any problem with my programs arguments? Thanks in advance.
asked Jun 11, 2020 samhaz 5k points
0 votes
1 answer 13 views
13 views
Problem: On a Java OOP project I got three errors on my constructor: .\Voter.java:14: error: invalid method declaration; return type required .\Candidates.java:7: error: invalid method declaration; return type required .\Candidates.java:14: error: invalid method declaration; return type required
asked Feb 17 Ali 4.4k points
0 votes
1 answer 49 views
49 views
Anything I missed?
asked Dec 19, 2020 TeamScript 13.5k points