I am just learning how to call arrays through methods and I know how to do it when they are integers, however, my teacher did not go through too much on what to do with Strings. I figured they weren't much different, overall same concept, however I keep getting an error incompatable type message. I am trying to return three dog names to my main method, but it's not working. I would love if someone could explain what I'm doing wrong here so I could better grasp this concept.

  private static Scanner input = new Scanner(System.in);

  public static void main(String[] args)
  {
     String dogs2[] = new String[3];
    dogTypes(dogs2);

      for(int i =0; i < dogs2.length; i++)
    {
      System.out.printf("\nDog Type %d: %s", i+1, dogs2);
    }
  }



     public static String dogTypes(String dogs[])
            {


     System.out.printf("\nPlease enter 3 dog types: ");
     {
     for(int i =0;i < dogs.length; i++)
     dogs[i] += input.nextLine();
       }

      return dogs;

      }

I realized I needed to put dogs2[i], but it still wants a [] with my return statement, I used return dogs[0] and it worked, but I don't think that is technically the right way.

no. it is not. there is also a big logical error there. it'll work, but it's useless.

what you want is:

create array
fill array with dogs
return array

now, you may or may not want variable lengths, but then you still don't pass an array as parameter.

try something like this:

public static String[] dogTypes(){ // have an array of Strings as return type
  String[] dogs = new String[3];
  //...
  return dogs;
  }

or, for a variabel length:

public static String[] dogTypes(int size){ // have an array of Strings as return type
  String[] dogs = new String[size];
  //...
  return dogs;
  }

Ok, so I did the second one before and then when I try and pass my array from the main method through it, it tells me this: method dogTypes in class GreenBArraysV1 cannot be applied to given types;
required: int
found: java.lang.String[]
reason: actual argument java.lang.String[] cannot be converted to int by method invocation conversion

Actually, I may not need to create an array in the main mrthod to pass through the second method, so I may just do that and see if it prints out.

indeed, that's what I said, there's no reason to create an array, pass it to the method (where you should create it), and return it.
do you still have that problem?
can you show what code you have now if you do?

Well, my teacher is quite picky and I had thought she wanted us to pass an array through an argument, but this is what I ended up with:

 private static Scanner input = new Scanner(System.in);

  public static void main(String[] args)
  {

     String[] x =  dogTypes();

      for(int i =0; i < x.length; i++)
    {
      System.out.printf("\nDog Type %d: %s", i+1, x[i]);
    }
      System.exit(0);
  }



     public static String[] dogTypes()
            {
     String[] dogs = new String[3];

      for(int a =0; a < dogs.length; a++)
      {
        System.out.printf("Please enter dog type %d: ", a+1);
         dogs[a] = input.nextLine();

      }
       return dogs;

    }

}

oops, just saw that you asked for code if I still had problem, ha, so disregard that code!

so, that code works. if you want to make it more dynamic, just pass the length of the array to the method from the calling method (here main)
or create a list, instead of an array, that way you can have length = variableX instead of length = 3

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.