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

225
Vistas
Text File to StringBuilder to DefaultListModel to JList Display

I want to add strings from a file to a StringBuilder so that I can display the information as one String. How come the line System.out.println(sb.toString()); isn't displaying in console? I cant tell if the code is working right, and it doesnt show any errors.

File Example:
LastName
FirstName
ID
Hometown
HomeState
Phone Number

String input = textFieldLastName.getText() is gathered from a JTextField

private String filename = "test.txt"
private static File file;
private static PrintWriter outputFile;
@SuppressWarnings("rawtypes")
private static JList list;
private static Scanner inputFile;

public void Read() throws IOException {

    if(file == null || filename.equals("")) {
            System.out.println("File not selected!");
    }else{
        inputFile = new Scanner(filename);
        StringBuilder sb = new StringBuilder();
        sb.delete(0, sb.length());
        DefaultListModel listModel = new DefaultListModel();
        while(inputFile.hasNext()){
            // Read the next name
            String ln = inputFile.nextLine();
            String input = textFieldLastName.getText(); 

            if(input.equalsIgnoreCase(ln)){
                // Save friend information to variables
                sb.append(ln);
                        for(int i = 0; i <=5; i++){
                            sb.append(", " + inputFile.nextLine());
                        }
                listModel.addElement(sb.toString()); 
                System.out.println(sb.toString()); 
            }
        }
        list = new JList();
        list.setModel(listModel);
    }
}
over 4 years ago · Santiago Trujillo
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