Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

135
Views
How to return String.format with arrays

When I print an instance of my class, the instance is referenced as "null" because I return "null", how can I format the toString class so it will return what I actually wrote in this function:

public String toString()
{
    System.out.print(this.coefficient);
    for(int i=0; i<26;i++)
    {
        if(degrees[i] == 1)
        {
            System.out.print(variables[i]);
        }
        if(degrees[i]>1)
        {
            System.out.print(variables[i] + "^" + degrees[i]);
        }
    }
    System.out.println('\n');
    return null;
}

For example, it has to return "m1 = 13a^2b^3" (it is a polynom) but instead is returns "13a^2b^3 m1 = null"

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

Instead of directly printing each component of the String, concatenate them by using a StringBuilder:

public String toString()
{
    StringBuilder s = new StringBuilder();

    s.append(this.coefficient);
    for (int i = 0; i < 26; i++)
    {
        if (degrees[i] == 1)
        {
            s.append(variables[i]);
        }
        else if (degrees[i] > 1)
        {
            s.append(variables[i]).append('^').append(degrees[i]);
        }
    }

    return s.toString();
}
over 4 years ago · Santiago Trujillo Report

0

Use String Builder . Wherever you are using System.out.println

Instead of that

      StringBuilder temp=new StringBuilder();
      temp.append();// Add here the content what you are printing with Sysout 

       // at the end 
        return temp.toString();
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!