gpt4 book ai didi

java - 我如何实际使用分隔符?

转载 作者:行者123 更新时间:2023-12-01 17:30:05 24 4
gpt4 key购买 nike

如何在 java 中实际使用 delimeter,这是示例 data.dat 文件

   1 yuri
2 fisher
3 McMillan
4 price
5 soap

如何使用 useDelimiter() 方法仅获取文件中每一行的第一个数字

我该如何处理这段代码

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);
}
});

这是整个代码.. 嗯.. 文件中并不总是“lol”,我稍后会更改它

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
}

最佳答案

您可以使用src.useDelimiter("\\s*lol\\s*")。请参阅http://ideone.com/kZUL6获取解析示例。

关于java - 我如何实际使用分隔符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11946105/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com