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

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 1
3,890 points

Related questions

1 vote
1 answer 68 views
68 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 Gavin 15.3k 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’s the problem and what should I do. If someone could tell me, that would be a big favor. Thanks in advance
asked 5 days ago Sheeza 2.3k 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’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 Gavin 15.3k points
0 votes
1 answer 6 views
6 views
I want to make a static reference to the non-static method.
asked Sep 22 Daniel Anderson 4k points
1 vote
1 answer 19 views
19 views
Problem: Hello Kodlogs, I find this website very helpful besides the StackOverflow. You guys are really awesome and friendly. I want to know from you guys, how to pass an array to a method? Also what happens when an array is passed to a method? Thanks for your valuable and patient answer.
asked Mar 31 Gavin 15.3k points