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

  • SEARCH
  • Home
  • 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 8522929
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T07:16:00+00:00 2026-06-11T07:16:00+00:00

I am writing a file writer that generates strings in a certain format, using

  • 0

I am writing a file writer that generates strings in a certain format, using provided arguments. I would like the arguments to be passed to the method in the simplest way possible for the user. The arguments can be of any type, since they will use the toString() method to be turned into strings. Here’s a simple example:

public String writeLine( Object arg1, Object arg2 ) {
    return "(" + arg1.toString() + "," + arg2.toString() + ")";
}

However, I need to be able to pass a potentially unlimited amount of arguments (arg3, arg4, etc.). What is the best way to do this? I thought of doing this:

public String writeLine( Object[] argArray ) {
    String str = new String();
    for (int i = 0; i < argArray.length; i++) {
        str += argArray[i].toString();
    }
    return str;
}

public static void main(String[] args) {
    float[] values = {0.1, 0.2, 0.4, 0.8};
    writeLine( values ); //<------ Compile Error
}

But I quickly realized that this does not work if I want to pass specific types of arrays. The float array would need to be converted to an Object array first, which would be unnecessary work on the user’s part. What is the best way to simplify this?

  1. Write several methods that take common array types as arguments ( writeLine( float[] argArray ), writeLine( String[] argArray ), etc. ) (won’t work for all types)
  2. Convert all arrays to Object arrays before passing (slow and extra work).
  3. Something else?
  • 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-11T07:16:02+00:00Added an answer on June 11, 2026 at 7:16 am

    try:

    public String writeLine( Object... args ) {
    ...
    }
    

    When using primitive arrays, make sure to replace them with an array of the appropriate wrapper type, e.g.

    Float[] values = {0.1f, 0.2f, 0.4f, 0.8f};
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am writing the below structure in to the file using Binary Writer. File
I'm using StreamWriter to write log file. And this is the writing log method,
I'm writing file save and load functions for a specific file format that I
I have a bunch of strings that I'm writing to a file: private void
How can I write data to stdout instead of writing file? I'm using GPG
I'm writing a MIDlet which needs to write file. I'm using FileConnection from JSR-75
When I am writing a file into GridFS using grid.put(), which has the same
I'm writing a couple of classes that generate xml file. (Details probably not important
I'm writing a getSitemapR handler that uses yesod-sitemap to generate a sitemap file. The
I am writing a program that gets a HTML file in Android and I

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.