Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8375239
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T15:07:27+00:00 2026-06-09T15:07:27+00:00

How can I actually use delimeter in java, this is the example data.dat file

  • 0

How can I actually use delimeter in java, this is the example data.dat file

   1 yuri
   2 fisher
   3 McMillan
   4 price
   5 soap

how can I only get the first numbers in every lines in the file and using useDelimiter() method

how can I do that to this code

Scanner src = new Scanner(fin);
                                 src.useDelimiter(" \n");
                                DefaultListModel model = new DefaultListModel();
                                while (src.hasNext()) {
                                String lol = src.nextLine();
                                model.addElement(lol);
                                }
                                list.setModel(model);
                                } 
                                });

This is the whole code.. Uhmm.. It’s not always ‘lol’ in the file I’ll change It later

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.DefaultListModel;
import javax.swing.DefaultListSelectionModel;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author Jfetizanan
 */
@SuppressWarnings("serial")
public class IOtestForm extends javax.swing.JFrame {

    /**
     * Creates new form IOtest
     */
    public IOtestForm() {
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        list = new javax.swing.JList();
        input0 = new javax.swing.JTextField();
        load = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jScrollPane1.setViewportView(list);
        DefaultListSelectionModel m = new DefaultListSelectionModel();
        m.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        m.setLeadAnchorNotificationEnabled(false);
        list.setSelectionModel(m);
        list.addListSelectionListener(new ListSelectionListener() {
            @Override
        public void valueChanged(ListSelectionEvent e) {
        int selected[] = list.getSelectedIndices();
        for (int i = 0; i < selected.length; i++) {
        String element = (String) list.getModel().getElementAt(selected[i]);
        input0.setText(element);
        }}});

        load.setText("Load");
        load.addActionListener(new ActionListener() {
                            @Override
            @SuppressWarnings("unchecked")
                            public void actionPerformed(ActionEvent e) {
                                 FileReader fin = null;
                            try {
                                 fin = new FileReader("data.dat");
                                } catch (FileNotFoundException ex) {
                                   Logger.getLogger(IOtestForm.class.getName()).log(Level.SEVERE, null, ex);
                                }
                                Scanner src = new Scanner(fin);
                                src.useDelimiter("\\s*lol\\s*");
                                DefaultListModel model = new DefaultListModel();
                                while (src.hasNext()) {
                                String lol = src.next();
                                model.addElement(lol);
                                }
                                list.setModel(model);
                                } 
                                });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 380, Short.MAX_VALUE)
                    .addComponent(input0)
                    .addComponent(load, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(13, 13, 13)
                .addComponent(load)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(input0, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(94, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {

        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(IOtestForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }

        java.awt.EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                new IOtestForm().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify
    private javax.swing.JTextField input0;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JList list;
    private javax.swing.JButton load;
    // End of variables declaration
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T15:07:28+00:00Added an answer on June 9, 2026 at 3:07 pm

    Let’s start with the basic

    To load the file, you would do this

    Scanner src = new Scanner(is);
    while (src.hasNext()) {
    
        int line = src.nextInt();
        String value = src.next();
    
        System.out.println(line + " = " + value);
    
    }
    

    Now, what we really need is some way to model this information. A better solution would be to create a Object of some kind that represents this information that makes it easier to manage.

    Something like

    public class LineOfText {
    
        private int line;
        private String value;
    
        public LineOfText(int line, String value) {
            this.line = line;
            this.value = value;
        }
    
        public int getLine() {
            return line;
        }
    
        public String getValue() {
            return value;
        }
    
    }
    

    Okay, then we need to apply the properties from the file to it.

    Scanner src = new Scanner(is);
    while (src.hasNext()) {
    
        int line = src.nextInt();
        String value = src.next();
    
        LineOfText lot = new LineOfText(line, value);
    
    }
    

    This we can add to the ListModel you’ve already created.

    model.addElement(lot);
    

    Now if we display this on our UI, it doesn’t look very pretty. What we really need is some way to “render” the text more efficiently.

    Something like

    public class LineOfTextListCellRenderer extends DefaultListCellRenderer {
    
        @Override
        public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
    
            if (value instanceof LineOfText) {
    
                value = ((LineOfText)value).getLine();
    
            }
    
            return super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
    
        }
    
    }
    

    Should do. We need to apply this renderer to the list you created earlier.

    list.setListCellRenderer(new LineOfTextListCellRenderer());
    

    Now, if we display this on the UI, we should only see the line numbers in the list.

    Next, we need some way to detect the change of selection. For this we need a ListSelectionListener

    list.addListSelectionListener(new ListSelectionListener() {
    
        @Override
        public void valueChanged(ListSelectionEvent e) {
    
            Object item = list.getSelectedItem();
            if (item != null && item instance of LineOfText) {
                ListOfText lot = (LineOfText)lot;
                // Now we have access to the text value
                // Simply use "setText" on the text area to apply it
    
                textArea.setText(lot.getValue());
    
                // Remember, "textArea" should be replaced with the name
                // of your text area
            }
    
        }
    
    });
    

    Now you see why it was a separate question 😉

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Does anyone know how DbDataReaders actually work. We can use SqlDataReader as an example.
Can someone give an example of a good time to actually use unsafe and
Hi Can any one help me how i can use NSAttributedStrings with UISearchBar. actually
I need to use some connectors which are actually servlets. How can I do
I'm trying to use PyLucene, and I can't find any code examples that actually
I've recently come across this website. Now, is there any way I can actually
Is there any way that I can actually use the cookies from a cookie
not sure if this can actually be done - OK probably can be done
I have a file named insert.txt. It can look like this (or uglier): ASDFG?|??|?\/\HJKL<MNBVCXZQWERTYUIOP
I'd like to limit what organizational items users can actually see in the CM

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.