我在java中将csv读取到数组中,试图将数组的内容设置到JComboBox中



我已经将csv文件读取到java中的一个数组中。我这样做是为了获得regPlate列,希望能返回所有的regPlates。我试图使allRegPlates等于regPlates[0],因为当我在CLI中打印regPlates[0]时,它会显示所有的车牌列。

这是我的CSV。CSV图片

这是我的代码:

Cars.java

package prototype;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.*;
import java.util.Scanner;
import java.io.FileReader;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.DataInputStream;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.io.IOException;

public class Cars {

public String regPlate;
public double price;
public String make;
public String model;
public String fType;
public String colour;
public int mileage;
public int prevOwners;
public double mpg;
public double noughttosixty;
public int bhp;
public int maxSpeed;
public String feature1;
public String feature2;
public String feature3;
public String feature4;
public boolean sold;
public double boughtFor;
public double profit;

public String[] regPlates;
public String allRegPlates;

Scanner input = new Scanner(System.in);
public List<String[]> line = new ArrayList<String[]>();
String lines = "";

public String thisLine;
public List<String> getAllRegPlates() 
{
String currentLine = "";
List<String> allRegPlates = new ArrayList<>();
String[] tempRegPlates;
try (BufferedReader br = new BufferedReader(new FileReader("Cars.csv"))) {
while ((currentLine = br.readLine()) != null) {
tempRegPlates = currentLine.split(",");
System.out.println(tempRegPlates[0]);
// Add all found plates to your collection
allRegPlates.add(tempRegPlates[0]);
}
} catch (IOException e) {
e.printStackTrace();
}

return allRegPlates;
}

GUI.java

Cars cars = new Cars();

List<String> plates = cars.getAllRegPlates(); // get all the plates as list
String[] platesAsArray = (String[]) plates.toArray();

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(500, 200, 1000, 600);
contentPane = new JPanel();
contentPane.setBackground(Color.BLACK);
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
GridBagLayout gbl_contentPane = new GridBagLayout();
gbl_contentPane.columnWidths = new int[]{0, 124, 123, 0, 0, 0, 0, 0, 0, 0, 0, 275, 189, 7, 0};
gbl_contentPane.rowHeights = new int[]{0, 0, 36, 0, 0, 0, 0, 0, 0, 0, 0, 0, 155, 0};
gbl_contentPane.columnWeights = new double[]{0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, Double.MIN_VALUE};
gbl_contentPane.rowWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE};
contentPane.setLayout(gbl_contentPane);

JComboBox comboBox = new JComboBox(platesAsArray);
GridBagConstraints gbc_comboBox = new GridBagConstraints();
gbc_comboBox.insets = new Insets(0, 0, 5, 5);
gbc_comboBox.fill = GridBagConstraints.HORIZONTAL;
gbc_comboBox.gridx = 1;
gbc_comboBox.gridy = 2;
contentPane.add(comboBox, gbc_comboBox);

这就是我的GUI显示的

我也得到这个错误:

regPlate
la1 abc
la6 5gb
rifol1
SX72 44
java.lang.ClassCastException: class [Ljava.lang.Object; cannot be cast to class [Ljava.lang.String; ([Ljava.lang.Object; and [Ljava.lang.String; are in module java.base of loader 'bootstrap')
at prototype.GUI.<init>(GUI.java:98)
at prototype.GUI$1.run(GUI.java:43)
at java.desktop/java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:316)
at java.desktop/java.awt.EventQueue.dispatchEventImpl(EventQueue.java:770)
at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:721)
at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:715)
at java.base/java.security.AccessController.doPrivileged(AccessController.java:391)
at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:85)
at java.desktop/java.awt.EventQueue.dispatchEvent(EventQueue.java:740)
at java.desktop/java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:203)
at java.desktop/java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:124)
at java.desktop/java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:113)
at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:109)
at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.desktop/java.awt.EventDispatchThread.run(EventDispatchThread.java:90)

如有任何帮助,我们将不胜感激。

如注释中所述,第一件事是修复您的输入。在getAllRegPlates((中,实际上并没有将所有输入汇总到一个集合或数组中。此外,你正在返回颜色,它甚至没有在那里定义。

因此,这里是您的getAllRegPlates()的修改版本,它将返回字符串的List,其中包含CSV文件中找到的所有板

public List<String> getAllRegPlates() {
String currentLine = "";
List<String> allRegPlates = new ArrayList<>();
String[] tempRegPlates;
try (BufferedReader br = new BufferedReader(new FileReader("Cars.csv"))) {
while ((currentLine = br.readLine()) != null) {
tempRegPlates = currentLine.split(",");
System.out.println(tempRegPlates[0]);
// Add all found plates to your collection
allRegPlates.add(tempRegPlates[0]);
}
} catch (IOException e) {
e.printStackTrace();
}
// finally, return the list of regplates
return allRegPlates;
}

由于输入现在应该工作了,下一步将是更正JComboBox的创建。这看起来像这样:

Cars cars = new Cars();
List<String> plates = cars.getAllRegPlates(); // get all the plates as list
String[] platesAsArray = (String[]) plates.toArray(); // convert to String array
JComboBox comboBox = new JComboBox<String>(platesAsArray); // create the combobox
// finally, do layout stuff and add to your frame / panel

最新更新