• Register
1 vote
69 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,240 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 31 views
31 views
Why is this so?
asked Jan 1 TeamScript 13.5k points
0 votes
1 answer 7 views
7 views
What am I doing wrong?
asked Jan 24 TeamScript 13.5k points
1 vote
1 answer 202 views
202 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 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’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 6k 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 5.1k points
1 vote
1 answer 33 views
33 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 6 views
6 views
Problem: hi i am a beginner and i want explain to me what mean this with example
asked Feb 9 ahmedadel9090 2.6k points
0 votes
1 answer 12 views
12 views
Problem: The keyword break in Java can be used for breaking out of a loop or switch statement. Is there anything which can be used to break from a method?
asked Feb 5 Ali 5.1k points
0 votes
1 answer 4 views
4 views
Problem: Currently I have two classes. a classroom class and a School class. I would like to write a method in the School class to call public void setTeacherName(String newTeacherName) from the classroom class. classroom.java
asked Feb 5 Ali 5.1k points