Home > other >  How can I return an array from a method
How can I return an array from a method

Time:10-06

I am working with a project and I need read and save the all file information to the the arrays, Now I want to return one of arrays which is customer Id so that I can add in to another method to search the location.

public static int[] booking(int dis) { //dis is in the main method to seanner user input
        
        Scanner date = new Scanner(System.in);
        String inputDate;
        if(dis==1) {
            System.out.println("Input the date for the booking");
             inputDate = date.next();
             String str;
             String[] tokens = null;
             int num;
             int line=0;
             int code;
             Booking[] booking = new Booking[20];
             File f = new File("bookings-" inputDate ".txt");
             try {
                Scanner file = new Scanner(f); //scanner booking file
                
                while(file.hasNext()) {
                    str=file.nextLine();
                    tokens=str.split(",");              
                    booking[line] = new Booking();  // save all values in booking class
                    booking[line].setBookingId(Integer.parseInt(tokens[0]));
                    booking[line].setCustomerID(Integer.parseInt(tokens[1]));
                    booking[line].setBookingDate(tokens[2]);
                    for(int i=0;i<tokens[3].length();i  ) { //some value in null 
                        if(tokens[3]==null) {
                            tokens[3]="0";
                            }else booking[line].setTotalPrice(Float.parseFloat(tokens[3]));
                    }
                    num=tokens.length-4;
                    int[] intArray = new int[num];
                    for(int i = 0; i < num; i  ) {
                        
                        intArray[i] = Integer.parseInt(tokens[4 i]);
                        
                    }
                    booking[line].setServiceCodes(intArray);
                    line=line 1;
                    
                }
                for(int i=0;i<line;i  ) { // the print is works
                //System.out.println(booking[i].getBookingId() "\t\t" booking[i].getCustomerID() "\t\t" booking[i].getBookingDate() "\t\t" booking[i].getTotalPrice() "\t\t" Arrays.toString(booking[i].getServiceCodes()));
                    System.out.println(booking[i].getCustomerID());
                    
                }
                
            } catch (FileNotFoundException e) {
                
                e.printStackTrace();
            }
             
            
        }
        return booking; // the return booking is does not work
        
        
    }

How can I call a method and return the arrays I want?

CodePudding user response:

In your method signature, you specify that the method has to return an int[]. You are trying to return booking, which is of type Booking[]. You can change your method signature to return type Booking[] instead:

public static Booking[] booking(int dis) {
    ...
}

Additionally (as mentioned by OH-GOD-SPIDERS), the booking variable is not defined at the point where you use it in the return statement. You can fix this by moving Booking[] booking = new Booking[20]; above the if statement, or by moving the return statement within the if statement (adding another return statement for the else case). Which solution you want to pick depends on what you want the method to return in these cases.

CodePudding user response:

replace method signature

public static int[] booking(int dis)

with

public static Booking[] booking(int dis)

CodePudding user response:

you can't make a function return an array of bookings while the method's return type is an array of int replace int with booking

CodePudding user response:

The line inside the method while declaring booking. Is booking a type of Booking method.

You cant instantiate a method inside a method so I suggest you turn the property booking to a type of int[] like so:

     int[] booking = new int[20];

Inside the booking method then you can return booking since your method is a type on int array

CodePudding user response:

booking needs to be declared as an int[], not a Booking[]. Or you need to state Booking[] as return statement.

  • Related