• Register
1 vote
82 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

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

1 vote
2 answers 45 views
45 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 Gavin 15.3k points
1 vote
1 answer 27 views
27 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 LizzyM 6.1k points
0 votes
1 answer 3 views
3 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 Sheeza 2.9k points
1 vote
1 answer 20 views
20 views
Problem: Well, I am a new student of java programming. Nowadays I am learning how to use multiple classes in a java program. Based on my learning I&rsquo;ve written a code that gave me an error, &ldquo;Invalid method declaration; return type required&rdquo; I am attaching the ... * Math.PI * radius; return c; } public double radius; } Can you, please tell me where I am doing the wrong thing?
asked Mar 9 Gavin 15.3k points