简体   繁体   中英

Java: How to return int array from list of inputs?

After watching through a lot of basic tutorials, I decided it's time to try making a few simple applications of my own to help me understand OOP and remember what I learned so far.

My attempt is to make an app with a main class, and 2 object classes.

I have Main.java which just calls Performer.java and Calculations.java.

For now I am working on my Performer.java object.

I want the Performer.java to get 5 integers from the user, and then I want to be able to return it as an array and run it through my Calculations.java which would perform some simple tasks like counting averages and totals.

I set my method to public, and if I understand correctly this should allow me to access these variables from anywhere else in my app.

I have tried returning it as

return arrayList[] = {u1,u2,u3,u4,u5};

and this gave me an error "Enum header expected instead"

import java.util.Scanner;

public class Performer {

    public static int getUnit(){
        Scanner scan = new Scanner(System.in);

        System.out.println("Enter the first number: ");
        int u1 = scan.nextInt();

        System.out.println("Enter the first number: ");
        int u2 = scan.nextInt();

        System.out.println("Enter the first number: ");
        int u3 = scan.nextInt();

        System.out.println("Enter the first number: ");
        int u4 = scan.nextInt();

        System.out.println("Enter the first number: ");
        int u5 = scan.nextInt();

        return u1, u2, u3, u4, u5;  <--------Confusing Part
    }
}

My second attempt was to return them as separate variables but this tells me expected type is int. Which I thought it is since those variables scan.nextInt().

What can I do to return an array that I can pass through my other objects?

Thank you!

you can use List or array.

Try this -

public static List<Integer> getUnit(){
    List<Integer> list = new ArrayList<>();
    System.out.println("Enter the first number: ");
    list.add(scan.nextInt());
    ...

    return list;
}

change the return value of getUnit() to int[] and return the values as follows:

return new int[] {u1, u2, u3, u4};

if you want to return a List, you can set the return value to List<Integer> and return the values as follows:

return Arrays.asList(u1, u2, u3, u4);

Note that Arrays.asList returns an immutable List, so you can't add or remove values from it.

As you are new to programming, first let me clear some of the errors in your code. This line here:

 return u1, u2, u3, u4, u5;  <--------Confusing Part

where you have used the return statement, the return allows only one value to be passed at a time, so you cannot pass all u1,u2,u3,u4 at the same time.

The solution to this has been provided above by the other coders: either you can change the return type of your getUnit() method to an integer array and returning the reference of that array or you can change the return type of your getUnit() method to an integer list and return the reference of the list.

Since you are new to coding and you don't know about ArrayList, here is a link to it: Java ArrayList class

Well, to start with, the return type of your function is int . You need it to be int[] at the very least to denote that you want to return an array of integers, not a single integer.

Then, you need to figure out the array literal syntax in Java.

And then, you should consider whether the Java collections classes might be a better fit for what you're trying to do. Check out some of the examples around how you construct an ArrayList .

replace your confusing part:

return new int[]{u1, u2, u3, u4, u5};

and function signature:

public static int[] getUnit()

Try this

`public static int[] getUnit() {

    int[] unit = new int[5];
    Scanner in = new Scanner(System.in);
    for (int i = 0; i < unit.length; i++) {
        System.out.println("Enter element number " + (i + 1) + ":");
        unit[i] = in.nextInt();
    }
    System.out.println("All unit entered");
    return unit;
}`

This is what you want i guess.

you can do this also:

public static int[] getUnit(){
        Scanner scan = new Scanner(System.in);


        System.out.println("Enter the first number: ");
        int u1 = scan.nextInt();
        System.out.println("Enter the first number: ");
        int u2 = scan.nextInt();
        System.out.println("Enter the first number: ");
        int u3 = scan.nextInt();
        System.out.println("Enter the first number: ");
        int u4 = scan.nextInt();
        System.out.println("Enter the first number: ");
        int u5 = scan.nextInt();
        int[] array ={ u1, u2, u3, u4, u5};

        return  array;
        }
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM