0

What I am trying to do is prompt a user to enter their first and last name by calling a function twice. First call would be for the first name and the second call would be for the last name. The program would then concatenate and display "Hello, firstname lastname!" I feel like I am very close to the correct outcome but I am obviously missing something. New guy here. Thank you for any and all responses.

import java.util.Scanner;

public class firstLastName2 {
  static String F_NAME;
  static String L_NAME;
  static String name;
  static void firstName(String name) {
    Scanner keyboard = new Scanner(System.in);
    System.out.println("Please enter your first name.");
    F_NAME = keyboard.nextLine();
    System.out.println("Please enter your last name.");
    L_NAME = keyboard.nextLine();
  }
  public static void main(String[] args) {
    firstName(name);
    System.out.println("Hello, " + F_NAME + " " + L_NAME + "!");
  }
}
5
  • What problem you are facing now? Your current code doesn't show any effort about what you are trying to do. Commented Oct 16, 2020 at 15:09
  • It is not clear what you want. Your code has the correct output for me. Commented Oct 16, 2020 at 15:12
  • I am trying to call the function twice, first passing a prompt string for the user's first name, followed by a second call which passes a prompt string for the user's last name. Commented Oct 16, 2020 at 15:13
  • Why do you need to call a function twice? The method firstName (which is badly named - should be called e.g. getNames) does what you need. Commented Oct 16, 2020 at 15:18
  • I changed it to getNames. Thank you for that feedback. I realize that it does what I need but the course I am taking asks for this in the problem statement. I have tried different ways to call twice without repeating the entire function with no luck. Commented Oct 16, 2020 at 15:27

1 Answer 1

2

If you want to call a function twice, then i suggest that the function return the user input and receive the question text. Your code seem to be right.

import java.util.Scanner;

public class firstLastName2 {
  static Scanner keyboard = new Scanner(System.in);

  static String getUserInput(String question) {
    System.out.println(question);
    return keyboard.nextLine();
  }

  public static void main(String[] args) {
    String name = getUserInput("Please enter your first name.");
    String sunrName = getUserInput("Please enter your last name.");
    System.out.println(String.format("Hello, %s %s!", name , surName));
  }
}
Sign up to request clarification or add additional context in comments.

3 Comments

It seems overkilling to write a method for this purpose I think. And remove property from class also a bad idea when OP design that way.
I know, but i was only trying to answer the question with minimun modifications to original code. As you say is not a good idea to make a blocking calll inside a class method. I disagree with what you say about removing class methods, as it can be a good idea in a concurrency scenario, as if you have the chance of having a call from the same VM from different threads at the same time, access to static class members may be syncrhonized to avoid unexpected behaviours in run time
remove property from class Actually I am saying about not static field, create an instance of class and do what you want to do.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.