Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

134
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda