1

I dont understand why this isnt working, i keep getting an error on the printarray call that says "The method printArray(int[]) is undefined for the type Array_1m"

here is my main class

import java.util.Scanner;
public class Array_1m{
    public static void main(String[] args){
        int[] intarray = new int[] {1,2,3,4,5} ;
        System.out.println("Here are our starting arrays");
        printArray(intarray);

        System.out.println("What would you like to do?");
        System.out.println("");

    }

}

here is my array class, i have to make a bunch of different methods for this class to be used in the main class and im just trying to print two arrays initially in order to manipulate them later. ill probably be back on here later for more help because I am having so much trouble with this program.

import java.util.ArrayList;
import java.util.Scanner;
public class Array_1 {
    int[] internalarray;

    public Array_1(int x) {
        if(x>0) {
            this.internalarray = new int [x];
        }
        else {
            System.out.println("Error: Array size must be non-negative");

        }

    }
    public static void printArray(int[] intarray) {
           for (int i = 0; i < intarray.length; i++) {
              if (i > 0) {
                 System.out.print(", ");
              }
              System.out.print(intarray[i]);
           }
        }
 }
1
  • 1
    If you're calling a method from another class, you need to prefix the class name: Array_1.printArray(intarray); Commented Apr 24, 2018 at 4:09

2 Answers 2

2

First you have to import Array_1 in your code file and then you can call printArray as

Array_1.printArray(intarray);
Sign up to request clarification or add additional context in comments.

Comments

0

your program is already work. It is work in my netbeans ide. Please try again to use with some changes in my code. import your Array_1 java class. Her is Array_1m- main method

import static stackoverflowtest.Array_1.printArray;

public class Array_1m {
    public static void main(String[] args) {
        int[] intarray = new int[] {1,2,3,4,5} ;
        System.out.println("Here are our starting arrays");
        printArray(intarray);

        System.out.println("What would you like to do?");
        System.out.println("");
    }
}

and after this is next java class Array_1

import java.util.ArrayList;
import java.util.Scanner;
public class Array_1 {
    int[] internalarray;

    public Array_1(int x) {
        if(x>0) {
            this.internalarray = new int [x];
        }
        else {
            System.out.println("Error: Array size must be non-negative");

        }

    }
    public static void printArray(int[] intarray) {
           for (int i = 0; i < intarray.length; i++) {
              if (i > 0) {
                 System.out.print(", ");
              }
              System.out.print(intarray[i]);
           }
        }
 }

` this is result enter image description here

question solve github repositary is https://github.com/randikawann/StackHelpArray_1

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.