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

253
Views
Formatear toString en Java

Probé el formateador de cadenas en mi toString para que mi salida se vea bien. Alguien sabe cómo puedo solucionar esto, soy un nuevo usuario del formateador en Java.

Estoy usando este código para leer datos de un archivo en formato ".ser" El código se ve así,

 public void printList(List<Person> list, File filepath) throws IOException, ClassNotFoundException { Person person = new Person(); if (filepath.isFile()) { ObjectInputStream inputStream = new ObjectInputStream(new FileInputStream(filepath)); list = (List<Person>) inputStream.readObject(); inputStream.close(); ListIterator iterator = list.listIterator(); int count = 0; while (iterator.hasNext()) { System.out.println((count +1)+ ". " +iterator.next()); count++; } if(count > 0){ System.out.println("\nThere are : " + count +" persons.\n"); }else { System.out.println("The list is empty....!\nTo add to list enter 1:"); } } }

Estoy usando ObjectInputStream y OutPutstream para leer estos datos. Mi código funciona bien, sin embargo, la salida que me gusta arreglar está en mejor forma.

 public class Person extends Addresses implements Serializable { private String firstName = ""; private String lastName = ""; private String age= ""; private double length; private String postAddress = ""; private String postNumber = ""; private String postArea = ""; public Person() { this("", "", "", 0.0, "", "", ""); } public Person(String firstName, String lastName, string age, double length, String postAddress, String postNumber, String postArea) { super(postAddress, postNumber, postArea); this.firstName = firstName; this.lastName = lastName; this.signature = age; this.length = length; } @Override public String toString() { String header = String.format(java.util.Locale.US, "\t%5s \t%1s %1s \t%10.2f", this.age, this.firstName, this.lastName, this.length); return header; }
 No Age Name Length[M] 1. 12 John Doe 1.80 2. 32 Mona lisa 1.69 3. 50 Benjamin Franklin 1.75 4. 45 Robert Oppenheimer 1.82

La salida que quiero es

 No Age Name Length[M] 1. 12 John Doe 1.80 2. 32 Mona lisa 1.69 3. 50 Benjamin Franklin 1.75 4. 45 Robert Oppenheimer 1.82
over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

solución 1: puede aumentar el límite de cadenas de tal manera que todas las cadenas resultantes se ajusten al límite dado para que haya una sangría de tabulación uniforme.

solución 2: puede limitar los límites de caracteres como se muestra a continuación para que siempre la cadena esté en el límite dado, por lo que habrá una sangría adecuada

 String result = String.format("|%.5s|", "Hello World");// output -> |Hello|
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!