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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T05:47:07+00:00 2026-05-26T05:47:07+00:00

We have method that looks like this : public String getCommandString(Object…args) { return this.code

  • 0

We have method that looks like this :

public String getCommandString(Object...args) {
    return this.code + " " + String.format(this.pattern, args);
}

Where this.code:int and this.pattern:String.

This method is usually called like this :

// cmd.code = 20
// cmd.pattern = "%1$s = %2$d"
String str = cmd.getCommandString("foo", 3);   // -> "20 foo = 3"

and other string patterns (this is for a very simple text-based server-client program)

Now, is it possible for pattern to take into account a variable number of arguments, such as

// cmd.code = 20
// cmd.pattern = ???
String s1 = cmd.getCommandString("a", "b", "c");               // -> 20 a b c
String s2 = cmd.getCommandString("Hello", "world");            // -> 20 Hello world
String s3 = cmd.getCommandString("1", "2, "3", "4", "5", "6"); // -> 20 1 2 3 4 5 6

Assuming perhaps that each argument is of the same type (strings)? Or do I have to override the method and format the string manually? More specifically, I’m looking for a generic string pattern to format a variable number of arguments (of the same type). I remember making such a thing in C, but is this possible in Java?

  • 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-26T05:47:08+00:00Added an answer on May 26, 2026 at 5:47 am

    The functionality you are asking for, if I understand it correctly, does not exist. Here is the javadoc section from the Formatter class:

    Format specifiers can reference arguments in three ways:

    Explicit indexing is used when the format specifier contains an argument index. The argument index is a decimal integer indicating the position of the argument in the argument list. The first argument is referenced by “1$”, the second by “2$”, etc. An argument may be referenced more than once. For example:

    formatter.format(“%4$s %3$s %2$s %1$s %4$s %3$s %2$s %1$s”,
    “a”, “b”, “c”, “d”)
    // -> “d c b a d c b a”

    Relative indexing is used when the format specifier contains a ‘<‘ (‘\u003c’) flag which causes the argument for the previous format specifier to be re-used. If there is no previous argument, then a MissingFormatArgumentException is thrown.

    formatter.format(“%s %s % “a b b b”
    // “c” and “d” are ignored because they are not referenced

    Ordinary indexing is used when the format specifier contains neither an argument index nor a ‘<‘ flag. Each format specifier which uses ordinary indexing is assigned a sequential implicit index into argument list which is independent of the indices used by explicit or relative indexing.

    formatter.format(“%s %s %s %s”, “a”, “b”, “c”, “d”)
    // -> “a b c d”

    It is possible to have a format string which uses all forms of indexing, for example:

    formatter.format(“%2$s %s % “b a a b”
    // “c” and “d” are ignored because they are not referenced

    The maximum number of arguments is limited by the maximum dimension of a Java array as defined by the Java Virtual Machine Specification. If the argument index is does not correspond to an available argument, then a MissingFormatArgumentException is thrown.

    If there are more arguments than format specifiers, the extra arguments are ignored.

    What you are asking for is a way to construct a pattern that will accept an arbitrary number of arguments and use tham all. The problem is that the Formatter class can only associate format specifiers with arguments either explicitly, relatively, or ordinarily. In each of these cases, the number of forma specifiers is fixed. There is no construct in which you can repeat or loop a format specifier. The relative technique looks promising, but there is no way to nest, or loop, it.

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

Sidebar

Related Questions

I have a method that looks like this: public static String escape(String text) {
I have an action method that looks like this: public ActionResult DoSomething(string par, IEnumerable<string>
I have a method in a url rewriting module that looks like this public
I have a source object that looks like this: private class SourceObject { public
I have a method that starts like this: public static UnboundTag ResolveTag(Type bindingType, string
I have a controller method that looks like this: [AcceptGet] public ActionResult Index(SecurityMatrixIndexViewModel model)
I have a class that looks like this: public class UploadBean { protected UploadBean(Map<String,?>
I have a model that looks like this: public class Book { public string
I have a class that looks like this: public class Person { public string
I have a class that looks like this public class TestCase { public IEnumerable<string>

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.