gpt4 book ai didi

Java:读取每行只有第一个单词的.csv文件

转载 作者:行者123 更新时间:2023-11-29 03:13:49 27 4
gpt4 key购买 nike

我正在尝试将一个 .csv 文件读入一个数组,该数组包含文件中每一行的第一个索引。

我要实现的只是每行的第一个字,而不是像下图那样:

Bonaqua
California
Gallardo
City
Skyline

dropbox image

下面是我的读文件类:

import java.io.File;
import java.util.Scanner;
import javax.swing.JOptionPane;

public class readfile {

private Scanner s;

public void openFile() {
try {
s = new Scanner(new File(readpath.a));
} catch (Exception e) {
JOptionPane.showMessageDialog(null, "File not found!");
}
}

public void readFile() {

String read = "";

while (s.hasNextLine()) {
read += s.nextLine() + "\n";
}

String menu[] = read.split("\n");
Object[] selectionValues = menu;
String initialSelection = "";

Object selection = JOptionPane.showInputDialog(null,
"Please select the Topic.", "Reseach Forum Menu",
JOptionPane.QUESTION_MESSAGE, null, selectionValues,
initialSelection);

JOptionPane.showMessageDialog(null, "You have choosen "
+ selection + ".", "Reseach Forum Menu",
JOptionPane.INFORMATION_MESSAGE);

if (selection == null) {
JOptionPane.showMessageDialog(null, "Exiting program...",
"Research Forum Menu", JOptionPane.INFORMATION_MESSAGE);
System.exit(0);
}
}

public void closeFile() {
s.close();
}
}

最佳答案

s.nextLine() 更改为 s.nextLine().split(",")[0]

关于Java:读取每行只有第一个单词的.csv文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27788157/

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