• Register
1 vote
135 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’t figure out the problem. Could you please shed some light on this? Find my sample code snippet below:

public class myClass {
    public static int largest(int x, int y, int z) {
        if (x > y) {
            if (x > z) {
                return x;
            } else if (y > z) {
                return y;
            } else {
                return z;
            }
        }
    }

    public static void main(String args[]) {
        int x = 8;
        int y = 9;
        int z = 20;

        System.out.println(largest(x, y, z));
    }
}

Many thanks

12 7 7
15,250 points

Please log in or register to answer this question.

2 Answers

1 vote

Solution:

The problem is that you can have only one public class in your java program and that too be matched with your class file name.

To avoid the error you just remove the “public” in front of your upper-top class and make it a static class of the main method.

If you still have any confusion feel free to ask in this thread. Thanks. 

13 9 6
94,260 points
0 votes

The problem is that you have only one public class per file and this file should have the same name as the class. You just have to remove the public in front of the definition of first-class. A better approach to do would be to make it a static method of the main class.

Solution:

public class myClass {

public static int largest(int a,int b,int c)

    {

        if(a>b)

        {

            if(a>c)

                return a;

            else

                if(b>c)

                    return b;

                else

                    return c;

        }

        else

        {

            if(b>c)

                return b;

            else

                return c;

        }

    }  



    public static  void main(String args[]) {



        int a=19;

        int b=2;

        int c=1;



        System.out.println(largest(a,b,c)); 

  }

}

Second Answer:

  • Only one public top-level class is allowed per .java file.
  • Define funk in the funk.jaba with no other top-level classes.
  • Put any other classes in their own files where the file name matches the class name.
  • To answer your second question, if you have declared a method to return a particular type; like int then all the paths through that method must result in a return statement returning a valid value. In your case the if statement might not be entered.

Note:

A top-level class is a class that is not contained in any other class.

11 5 2
3,890 points

Related questions

0 votes
1 answer 21 views
21 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’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 9.8k points
0 votes
1 answer 45 views
45 views
Why is this so?
asked Jan 1 TeamScript 13.5k points
1 vote
1 answer 50 views
50 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’ve written a code that gave me an error, “Invalid method declaration; return type required” 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, 2020 Gavin 15.3k points
0 votes
1 answer 11 views
11 views
What am I doing wrong?
asked Jan 24 TeamScript 13.5k points
1 vote
1 answer 283 views
283 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: ... && board[n] == board[n+1] && board[n] == board[n+2]) { return true;} } } Thanks for your valuable time
asked Mar 28, 2020 Gavin 15.3k points
0 votes
1 answer 6 views
6 views
Problem: Please assist me in resolving this issue: which method will return the element that has the id attribute with the specified value
asked Apr 1 rakib1 51.5k points
0 votes
1 answer 10 views
10 views
Problem: Hi there! I want the answer to following MCQ: Which of the following is not part of a method call? method name return type parentheses all of the above are part of a method call Thanks!
asked Mar 6 Code Learner 9.8k points
1 vote
1 answer 2 views
2 views
Problem: How to fix this ? Groovy.lang.missingmethodexception: no signature of method:
asked Apr 25 PkGuy 23.5k points
1 vote
1 answer 6 views
6 views
Problem: Any one here to figure out thids problem? Typeerror: super() argument 1 must be type, not classobj.
asked Apr 26 PkGuy 23.5k points
0 votes
1 answer 29 views
29 views
Problem: I get some error that I can't figure out. Any clue what is wrong with my sample code? class B: def meth(self, arg): print arg class C(B): def meth(self, arg): super(C, self).meth(arg) print C().meth(1) I got the sample test code from help of 'super' built-in method.
asked Feb 23 Mashhoodch 13k points