Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6635311
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T23:02:16+00:00 2026-05-25T23:02:16+00:00

Lets say I have this program below. Now I want to pass both the

  • 0

Lets say I have this program below. Now I want to pass both the users first name AND last name into the method without calling it again and changing the parameter and making the user retype there first name and last name again.

I know I can make two different methods for finding the first name and last name but I was wondering if there was a way to do this is just one method.

import java.util.Scanner;

 public class Document3 {
public static void main(String[] args) {
    String name;
    name = getName("lastName"); //But how would I get my lastName WITHOUT recalling the method with "lastName" in the parameters?
    System.out.println(name); //I want to also get the other name into in my main method somehow
}
public static String getName(String nameOption) {
    Scanner x = new Scanner(System.in);
    String firstName = "";
    String lastName = "";
    String nameChoice = "";
    System.out.print("Please enter your first name: ");
    firstName = x.nextLine();
    System.out.print("\nPlease enter your last name: ");
    lastName = x.nextLine();
    if (nameOption.equals("firstName")) {
        nameChoice = firstName;
    }
    if (nameOption.equals("lastName")) {
        nameChoice = lastName;
    }
    return nameChoice; //how do I return both variables (firtName and lastName) and how would I access them
}

}

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T23:02:17+00:00Added an answer on May 25, 2026 at 11:02 pm

    Create a small wrapper class which holds the values you want to return, and return an instance of that class.

    class Name
    {
        final String firstName;
        final String lastName;
    
        Name(String first, String last)
        {
            firstName = first;
            lastName = last;
        }
    }
    

    How to use it:

    public static void main(String[] args)
    {
        Name name = getName();
        String first = name.firstName;
        String last = name.lastName;
    }
    
    public static Name getName()
    {
        Scanner x = new Scanner(System.in);
    
        System.out.print("Please enter your first name: ");
        String firstName = x.nextLine();
    
        System.out.print("\nPlease enter your last name: ");
        String lastName = x.nextLine();
    
        return new Name(firstName, lastName);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Lets say I have hierarchy like this (This is just a test program. Please
Lets say I have a C++ program and I want to call functions from
Hello Lets say I have a console application, that looks like this class Program
Lets say you have an program like this: $info = array( 'username'=>'jeff', 'password'=>'kay' );
Consider this problem: I have a program which should fetch (let's say) 100 records
Lets say have this immutable record type: public class Record { public Record(int x,
Lets say I have this array, int[] numbers = {1, 3, 4, 9, 2};
Lets say I have this code: <?php class hello { var $greeting = hello;
Lets say i have this usercontrol public class test : UserControl { public int
Lets say I have this class in foobar-shared.lib: class FooBar { std::string m_helloWorld; }

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.