swing—我在java中将csv读入数组,试图将数组的内容设置到jcombobox中

wj8zmpe1  于 2021-07-06  发布在  Java
关注(0)|答案(1)|浏览(350)

我已经用java将csv文件读入数组。我这样做是为了得到 regPlate 专栏,希望把所有的 regPlates . 我在试着 allRegPlates 等于 regPlates[0] 因为当我打印的时候 regPlates[0] 在cli中,它显示所有的车牌列。
这是我的csv。csv图片
这是我的密码:
汽车.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;
    }

图形用户界面.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)

任何帮助都将不胜感激。

vpfxa7rd

vpfxa7rd1#

正如在评论中已经指出的,第一件事就是修正你的输入。在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

相关问题