Adz
Adz

Reputation: 2847

'This method must return a result of type int' Java?

public static void main(String args[])
{
    double arr[] = {1,-6.3,9000,67.009,1.1,0.0,-456,6,23,-451.88};

    ArrayList<Integer> List = new ArrayList<Integer>();

     List.add(1);
     List.add((int) -6.3);
     List.add(9000);
     List.add((int) 67.009);
     List.add((int)1.1);
     List.add((int)0.0);
     List.add(-456);
     List.add(6);
     List.add(23);
     List.add((int)451.88);
}

public static int ArrayListMax(ArrayList List)
{

    for (int i=0; i<List.size(); ++i)
    {
        System.out.println(List.get(i));
    }

The error is in:

  public static int ArrayListMax(ArrayList List) 

This is probably a very nooby mistake, but I'm new to Java so forgive me.

Any help please?

Thank you.

EDIT:

I want the ArrayListMax method to print the size of the List!

Upvotes: 0

Views: 4244

Answers (7)

Reimeus
Reimeus

Reputation: 159784

Assuming you are trying to get the maximum value in your List in the method arrayListMax, you need to return an integer in accordance with your method signature, which the error is telling you

This method must return a result of type int

Instead of printing all the values in the list, you could do:

public static int arrayListMax(List<Integer> List) {
   return Collections.max(list);
}

Use Java Naming Conventions. Method & variable names begin with a lowercase letter. Using this approach helps avoid confusion between instances & types (e.g. in the case of List in the main method).

Upvotes: 1

Tushar Paliwal
Tushar Paliwal

Reputation: 301

You should use a return statement as Methode is returning an int type value.

Upvotes: 0

Bhushan Firake
Bhushan Firake

Reputation: 9458

First, you are not returning anything with ArrayListMax method even if you have set its retun type to int. Either return void as you are directly printing from the list.

Second, even if you set the correct return type to ArrayListMax method, you will still need to call that method in the main method as below:

public static void main(String args[])
{
    double arr[] = {1,-6.3,9000,67.009,1.1,0.0,-456,6,23,-451.88};

    ArrayList<Integer> List = new ArrayList<Integer>();

     List.add(1);
     List.add((int) -6.3);
     List.add(9000);
     List.add((int) 67.009);
     List.add((int)1.1);
     List.add((int)0.0);
     List.add(-456);
     List.add(6);
     List.add(23);
     List.add((int)451.88);
     YourClassNameInWhichMethodIs.ArrayListMax(List);
}

Upvotes: 0

Asaph
Asaph

Reputation: 162801

Either add a return statement to your ArrayListMax() method that returns an int or change the method signature from public static int to public static void. And add a closing } to the method too.

Also, you shouldn't use List as a name for the argument to that method because it's the name of an interface that you're actually importing in this code. The convention in java is for variable names and method names to begin with a lowercase letter (camel case) and class names to begin with an uppercase letter (pascal case).

Upvotes: 1

mac10688
mac10688

Reputation: 2205

if you are just printing out values the signature should be public static void ArrayListMax(ArrayList List).

Upvotes: 0

GeorgeVremescu
GeorgeVremescu

Reputation: 1253

You created the function as returning a result

public static int ArrayListMax(ArrayList List) 

In order to remove your problem, if you need not return anything, write

public static void ArrayListMax(ArrayList List) 

void means you do not want the method to return a result.

Upvotes: 0

Pradeep Simha
Pradeep Simha

Reputation: 18123

Problem is in this method:

public static int ArrayListMax(ArrayList List)
{

    for (int i=0; i<List.size(); ++i)
    {
       System.out.println(List.get(i));
    }
}

You are not returning from this method, you must return of type int to solve that error. Since you have specified int as return type.

Upvotes: 0

Related Questions