1
我試圖讀取.csv文件的文件,並將文件中每行的第一個索引讀入數組。Java:讀取每行只有第一個字的.csv文件
我想實現的是隻有每一行的第一個字,而不是像下面的圖片:
Bonaqua
California
Gallardo
City
Skyline
下面是我讀文件類:
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();
}
}
嗨@迪瑪,謝謝,它現在的作品就像我想的那樣。 – Jaymaan