Hi i am new to java and just learning as i go , I the basic of calling mothod arguments and so forth but i am having difficulties calling my array method so it would come up to the terminal to the user when the code is running .

i have declared the array list paramter to ( int [] name ) , then called the array MyObject.MyMethod ( 01234 ) . I tried to return the array list then call the method that way but is not worrking please can some one show me the correct way to implement this using my code example below

   public static void main(String[] args) {

        BookingController  details =  new BookingController (); 

        () // PLEASE SHOW ME HERE HOW TO CALL THE ARRAY 

      }

public int lessonsList( /// PLEASE SHOW ME IN HERE WHAT PARAMTER I HAVE TO DECLARE FOR ARRAY LIST ) {

      final int lESSON_COUNT = 4;

        System.out.println("###############################################");

        System.out.println("PLEASE SELECT LESSON");

        System.out.println("###############################################");

        String[] lessonList = new String[lESSON_COUNT];   // new string of name of 3 
        lessonList[0] = "Zumba";
        lessonList[1] = "Spin";
        lessonList[2] = "Yoga";
        lessonList[3] = "BodyScuplt";

        for (int i = 0; i < lESSON_COUNT; i++) {

            System.out.println((i + 1) + ". " + lessonList[i]);
        }
        System.out.println("Please select the values between 1-3 to book lessons");
        int y = sc.nextInt();
        y -= 1;
        System.out.println("You have selected lesson " + lessonList[y]);
        sc.nextInt();
        if (y > lESSON_COUNT | y < 1) {
            System.out.println("Can you please re-enter selection.");

        }
        y = sc.nextInt();
        System.out.println((y + 1) + ". " + lessonList[y]);

      return y;
    }

}

Recommended Answers

All 11 Replies

Can some one answer as it will take two miniutes ? please

Hi i do not want others to code it for me , As i have developed all my code , i just want clarity . there is members on this fourms that alsk people to develop the whole of their code for certain functionality . so how is that fair compared to a simple method call ? lI have mentioned in the description that i have TRIED different routes . As this showed me making import . Thanks for the tutorial any way

I had a look at this tutorial and i do not get the tutorial base the on the methods use are static .

this is my input of what i have done for the whole day , looking at tutorials and making sense of it

i* I HAVE ATTAMPTED THE PARAMTER OF THE FUNCTION *** CODE BELOW

public int lessonsList( int [] array ) {

** iI HAVE INVOKED THE ARGUMENT IN THE MAIN FOR ARRAY CODE BELOW

details.lessonsLlist(1234 ); 

** I HAVE TRIED TO RETURN THE ARRYLIST AND CALLED THE ARRAY LIST **

I recieve erros this is why i need some clarity ?

When I read your post I didn't find the array "name" or MyObject.MyMethod in the supplied code. So that's a dead end to me. Some will say "Instructions unclear."

So I took a guess you just needed the shortest primer on passing an array to a method.

Clarity, in my view is needed in your problem statement. If you write you are having an issue with MyObject.MyMethod then I better see that in the (broken?) code shared.

Fair enough ,but what i have seen on this forum so far people will ask for functionality when they have 0 input in what they are asking and people still respond back to them with 0 input compared to my code 96 % input . How ever here is my issue below i have tried different ways of calling the array method . I just want some clarity on the right direction to go down

I understand in calling a menthod by creating a object and invoking the method . I am having trouble calling my array list within main

I have tried codes below

Attampt . 1

public class BookingController {

     String customerName = "";
        int customerAge = -1;
        char membership;

     Scanner sc = new Scanner(System.in);

      public static void main(String[] args) {

        BookingController  details =  new BookingController (); 

        details.customerDetails(); 

       details.lessonsList( int[] array = { 0, 1 , 2, 3, 4 }; 

    public  int lessonsList( int [] array ) {

      final int lESSON_COUNT = 4;

        System.out.println("###############################################");

        System.out.println("PLEASE SELECT LESSON");

        System.out.println("###############################################");

        String[] lessonList = new String[lESSON_COUNT];   // new string of name of 3 
        lessonList[0] = "Zumba";
        lessonList[1] = "Spin";
        lessonList[2] = "Yoga";
        lessonList[3] = "BodyScuplt";

        for (int i = 0; i < lESSON_COUNT; i++) {

            System.out.println((i + 1) + ". " + lessonList[i]);
        }
        System.out.println("Please select the values between 1-3 to book lessons");
        int y = sc.nextInt();
        y -= 1;
        System.out.println("You have selected lesson " + lessonList[y]);
        sc.nextInt();
        if (y > lESSON_COUNT | y < 1) {
            System.out.println("Can you please re-enter selection.");

        }
        y = sc.nextInt();
        System.out.println((y + 1) + ". " + lessonList[y]);

      return y;

    }

}

attampt 2

tring customerName = "";
            int customerAge = -1;
            char membership;

         Scanner sc = new Scanner(System.in);

          public static void main(String[] args) {

            BookingController  details =  new BookingController (); 

            details.customerDetails(); 

           details.lessonsList( 0, 1 , 2, 3, 4 }; 

        public  int lessonsList( int [] array ) {

          final int lESSON_COUNT = 4;

            System.out.println("###############################################");

            System.out.println("PLEASE SELECT LESSON");

            System.out.println("###############################################");

            String[] lessonList = new String[lESSON_COUNT];   // new string of name of 3 
            lessonList[0] = "Zumba";
            lessonList[1] = "Spin";
            lessonList[2] = "Yoga";
            lessonList[3] = "BodyScuplt";

            for (int i = 0; i < lESSON_COUNT; i++) {

                System.out.println((i + 1) + ". " + lessonList[i]);
            }
            System.out.println("Please select the values between 1-3 to book lessons");
            int y = sc.nextInt();
            y -= 1;
            System.out.println("You have selected lesson " + lessonList[y]);
            sc.nextInt();
            if (y > lESSON_COUNT | y < 1) {
                System.out.println("Can you please re-enter selection.");

            }
            y = sc.nextInt();
            System.out.println((y + 1) + ". " + lessonList[y]);

              return y;

            }

    }

attampt 3

tring customerName = "";
            int customerAge = -1;
            char membership;

         Scanner sc = new Scanner(System.in);

          public static void main(String[] args) {

            BookingController  details =  new BookingController (); 

            details.customerDetails(); 

           details.lessonsList( }; 

        public  int lessonsList( int [] array ) {

          final int lESSON_COUNT = 4;

            System.out.println("###############################################");

            System.out.println("PLEASE SELECT LESSON");

            System.out.println("###############################################");

            String[] lessonList = new String[lESSON_COUNT];   // new string of name of 3 
            lessonList[0] = "Zumba";
            lessonList[1] = "Spin";
            lessonList[2] = "Yoga";
            lessonList[3] = "BodyScuplt";

            for (int i = 0; i < lESSON_COUNT; i++) {

                System.out.println((i + 1) + ". " + lessonList[i]);
            }
            System.out.println("Please select the values between 1-3 to book lessons");
            int y = sc.nextInt();
            y -= 1;
            System.out.println("You have selected lesson " + lessonList[y]);
            sc.nextInt();
            if (y > lESSON_COUNT | y < 1) {
                System.out.println("Can you please re-enter selection.");

            }
            y = sc.nextInt();
            System.out.println((y + 1) + ". " + lessonList[y]);

          return y;

        }

}

I have to say that I expected you to tell me exactly what this list's name was in the code. Don't make me work at this or guess. I will guess badly.
Let's see what others can contribute.

Sorry for the confusion , but I used that as a example "MyObject.MyMethod ( 01234 ) " as using the standard java technique to call Methods which I should of said " for example " my apologies . But I am not expecting you to work on the code. I would just appreciate someone who has experience in this, to let me know if I am using the right technique in calling array list arguments effectively or not. Or if I am totally off track ... that is all

I may be repeating but if you tell me "MyObject.MyMethod" I go looking for that.

I wonder if English isn't working for us here. You wrote "calling array list arg" and here I usually access the arguments. If you are calling some array list arguments this to me is when I pass a list of functions/methods I want the method to call in some order. Not something like sorting an array of strings.

// simple example...

int[] data = {1,2,3};  // create an array
doStuff(data);         // pass it to the method
...

void doStuff(int[] arg) {   // declare method with an array parameter
    // do stuff with arg[0], arg[1] etc
}

To call an instance method from main (which is static) you need an instance of the class, but you have got this right in the 3 variants you posted.

If that doesn't answer the question then please explain what else you need.

ps: ArrayList is a class in the Java API that is often used instead of an array, but is not the same as an array, so beware of confusing the terminology

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.