0

My code is like this:

    public class Test() {

    String [] ArrayA = new String [5] 

    ArrayA[0] = "Testing";

      public void Method1 () {

            System.out.println(Here's where I need ArrayA[0])

         }

     }

I've tried various methods (No pun intended) but none worked. Thanks for any help I can get!

1
  • Do you have main function in this class ? Commented May 17, 2013 at 5:52

5 Answers 5

1
public class Test {

    String [] arrayA = new String [5]; // Your Array

    arrayA[0] = "Testing";

    public Test(){ // Your Constructor

        method1(arrayA[0]); // Calling the Method

    }

      public void method1 (String yourString) { // Your Method

            System.out.println(yourString);

         }

     }

In your main class, you can just call new Test();
OR if you want the method to be called from your main class by creating an instance of Test you may write:

public class Test {

    public Test(){ // Your Constructor

        // method1(arrayA[0]); // Calling the Method // Commenting the method

    }

      public void method1 (String yourString) { // Your Method

            System.out.println(yourString);

         }

     }

In your main class, create an instance of test in your main class.

Test test = new Test();

String [] arrayA = new String [5]; // Your Array

arrayA[0] = "Testing";

test.method1(arrayA[0]); // Calling the method

And call your method.

EDIT:

Tip: There's a coding standard that says never start your method and variable in uppercase.
Also, declaring classes doesn't need ().

Sign up to request clarification or add additional context in comments.

Comments

0

If we're talking about passing arrays around, why not be neat about it and use the varargs :) You can pass in a single String, multiple String's, or a String[].

// All 3 of the following work!
method1("myText");
method1("myText","more of my text?", "keep going!");
method1(ArrayA);

public void method1(String... myArray){
    System.out.println("The first element is " + myArray[0]);
    System.out.printl("The entire list of arguments is");
    for (String s: myArray){
        System.out.println(s);
    }
}

Comments

0

try this

private void Test(){
    String[] arrayTest = new String[4];
    ArrayA(arrayTest[0]);
}

private void ArrayA(String a){
    //do whatever with array here
}

1 Comment

you are passing the complete array , he need only the first sting in the array , why bother passing the whole array
0

Try this Snippet :-

public class Test {

        void somemethod()
        {
            String [] ArrayA = new String [5] ;

                ArrayA[0] = "Testing";

                Method1(ArrayA);
        }
      public void Method1 (String[] A) {

            System.out.println("Here's where I need ArrayA[0]"+A[0]);

         }
      public static void main(String[] args) {
        new Test().somemethod();
    }

}

The Class name should never had Test()

Comments

0

I am not sure what you are trying to do. If it is java code(which it seems like) then it is syntactically wrong if you are not using anonymous classes.

If this is a constructor call then the code below:

  public class Test1() {
    String [] ArrayA = new String [5]; 
    ArrayA[0] = "Testing";
      public void Method1 () {
            System.out.println(Here's where I need ArrayA[0]);
         }
     }

should be written as this:

public class Test{
    public Test() {
    String [] ArrayA = new String [5]; 
    ArrayA[0] = "Testing";
        Method1(ArrayA);          
    }
    public void Method1(String[] ArrayA){
        System.out.println("Here's where I need " + ArrayA[0]);
    }
}

3 Comments

System.out.println("Here's where I need " + ArrayA[0]);
Yeah quotes are really very important in java. My bad :( I edited it.
String [] ArrayA = new String [5]; Semi colons are also important. :P

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.