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

226
Views
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
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!