-4

package program16;

import java.util.Scanner;

/**
 *
 * @author buddysWorld
 */
public class Program16 
{
    public String[] myName(String firstName, String lastName) 
    {
      Scanner in = new Scanner(System.in);
      System.out.println("Enter your first name:");
      firstName = in.nextLine();
      System.out.println("Enter your last name:");
      lastName = in.nextLine();
      String fullNameFunc[]= {firstName, lastName};
      return fullNameFunc;
   }
   public static void main(String []args) 
   {
      String firstName = "";
      String lastName = "";
      String[] fullNameMain = fullNameFunc.clone();
      System.out.println("Your name is " + fullNameMain[0] + " " + fullNameMain[1]);
   }
}

Here (String[] fullNameMain = fullNameFunc.clone();) it says "cannot find symbol, variable fullNameFunc, class Program16" I have no other errors.

  • `fullNameFunc` is local to `myName`. You may be wanting to use the result of *calling* `myName`, on an instance of `Program16` that does not yet exist (or you mean it to be `static`, hard to say). – Dave Newton Apr 05 '21 at 20:28

2 Answers2

0

Your variable fullNameFunc is defined inside method myName which means that it can only be defined in there because it's its scope.

However, since the variable fullNameFunc is the return value of the method myName you could replace

fullNameFunc.clone();

by

myName(firstName, lastName).clone();

Here is a full corrected version of your code where I also redefined the scopes of firstName and lastName

public static String[] myName() {
    Scanner in = new Scanner(System.in);
    System.out.println("Enter your first name:");
    String firstName = in.nextLine();
    System.out.println("Enter your last name:");
    String lastName = in.nextLine();
    return new String[]{firstName, lastName};
}

public static void main(String[] args) {
    String[] fullNameMain = myName().clone();
    System.out.println("Your name is " + fullNameMain[0] + " " + fullNameMain[1]);
}
Yassin Hajaj
  • 21,337
  • 9
  • 51
  • 89
0
public class Program16 
{
    public static String[] myName(String firstName, String lastName) 
    {
      Scanner in = new Scanner(System.in);
      System.out.println("Enter your first name:");
      firstName = in.nextLine();
      System.out.println("Enter your last name:");
      lastName = in.nextLine();
      String fullNameFunc[]= {firstName, lastName};
      return fullNameFunc;
   }
   public static void main(String []args) 
   {
      String firstName = "";
      String lastName = "";
      String[] fullNameFunc = myName(firstName, lastName);
      String[] fullNameMain = fullNameFunc.clone();
      System.out.println("Your name is " + fullNameMain[0] + " " + fullNameMain[1]);
   }
}

You need to make myName() method static. And you forgot to add "String[] fullNameFunc = myName(firstName, lastName);" line in main() method.

aatwork
  • 2,130
  • 4
  • 17