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 8876081
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T19:07:38+00:00 2026-06-14T19:07:38+00:00

If I have the two functions below. How can I select the function that

  • 0

If I have the two functions below.
How can I select the function that will be chosen?

I imagine there is either some form of statement to determine the content of the scanner and therefore only have one function. Or it would be something that is passed to the function.

    public static int questionAsk(String question)
{
    Scanner scan = new Scanner (System.in);
    System.out.print (question+"\n");
    System.out.print ("Answer: ");
    return scan.nextInt();
}
    public static String questionAsk(String question)
{
    Scanner scan = new Scanner (System.in);
    System.out.print (question+"\n");
    System.out.print ("Answer: ");
    return scan.nextLine();
}
  • 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-06-14T19:07:39+00:00Added an answer on June 14, 2026 at 7:07 pm

    There is no way for the compiler to know which one of those methods you are calling. You could make it type safe by changing the String Types to something else, like this:

        public static int questionAsk(IntQuestion question)
    {
        Scanner scan = new Scanner (System.in);
        System.out.print (question.toString() +"\n");
        System.out.print ("Answer: ");
        return scan.nextInt();
    }
        public static String questionAsk(StringQuestion question)
    {
        Scanner scan = new Scanner (System.in);
        System.out.print (question.toString() +"\n");
        System.out.print ("Answer: ");
        return scan.nextLine();
    }
    

    And adding two new classes:

       public class IntQuestion extends String{
           public IntQuestion(String question){
                super(question);
           }
       }
    
       public class StringQuestion extends String{
            public StringQuestion(String question){
                super(question);
            }
       }
    

    When you construct an IntQuestion of StringQuestion, you can simply construct them the same way you would construct a String if you called the constructor:

    IntQuestion intQuestion = new IntQuestion("Some String Here");
    

    This is just a little bit of syntactic sugar to get the compiler to play nice and select the correct method based on the type.

    I hope this helps.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the below jquery function $(function(){ $(select#rooms).change(function(){ $.getJSON(/admin/select.php,{id: $(this).val(), ajax: 'true'}, function(j){ var
I have a package with two variables that has constant values like below. person_name
I have two functions: one that creates a new <textarea> when a button is
I have two chained select boxes where the second drop down populates based on
I have a table that includes a column for the user to select from
I have this query below: Select count(CDT) as Actions From Player_Tapjoy Where Trunc(CDT) >=
I have a question regarding a call to a function that takes quite a
I have a question here that looks a little like some of the ones
I have a scenario where a visitor can select a value from a drop-down
I wrote the two methods below to automatically select N distinct colors. It works

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.