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

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

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

  1. package prototype;
  2. import java.util.ArrayList;
  3. import java.util.Arrays;
  4. import java.util.*;
  5. import java.util.Scanner;
  6. import java.io.FileReader;
  7. import java.io.BufferedReader;
  8. import java.io.FileInputStream;
  9. import java.io.DataInputStream;
  10. import java.io.File;
  11. import java.io.FileNotFoundException;
  12. import java.io.PrintWriter;
  13. import java.io.IOException;
  14. public class Cars {
  15. public String regPlate;
  16. public double price;
  17. public String make;
  18. public String model;
  19. public String fType;
  20. public String colour;
  21. public int mileage;
  22. public int prevOwners;
  23. public double mpg;
  24. public double noughttosixty;
  25. public int bhp;
  26. public int maxSpeed;
  27. public String feature1;
  28. public String feature2;
  29. public String feature3;
  30. public String feature4;
  31. public boolean sold;
  32. public double boughtFor;
  33. public double profit;
  34. public String[] regPlates;
  35. public String allRegPlates;
  36. Scanner input = new Scanner(System.in);
  37. public List<String[]> line = new ArrayList<String[]>();
  38. String lines = "";
  39. public String thisLine;
  40. public List<String> getAllRegPlates()
  41. {
  42. String currentLine = "";
  43. List<String> allRegPlates = new ArrayList<>();
  44. String[] tempRegPlates;
  45. try (BufferedReader br = new BufferedReader(new FileReader("Cars.csv"))) {
  46. while ((currentLine = br.readLine()) != null) {
  47. tempRegPlates = currentLine.split(",");
  48. System.out.println(tempRegPlates[0]);
  49. // Add all found plates to your collection
  50. allRegPlates.add(tempRegPlates[0]);
  51. }
  52. } catch (IOException e) {
  53. e.printStackTrace();
  54. }
  55. return allRegPlates;
  56. }

图形用户界面.java

  1. Cars cars = new Cars();
  2. List<String> plates = cars.getAllRegPlates(); // get all the plates as list
  3. String[] platesAsArray = (String[]) plates.toArray();
  4. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  5. setBounds(500, 200, 1000, 600);
  6. contentPane = new JPanel();
  7. contentPane.setBackground(Color.BLACK);
  8. contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
  9. setContentPane(contentPane);
  10. GridBagLayout gbl_contentPane = new GridBagLayout();
  11. gbl_contentPane.columnWidths = new int[]{0, 124, 123, 0, 0, 0, 0, 0, 0, 0, 0, 275, 189, 7, 0};
  12. gbl_contentPane.rowHeights = new int[]{0, 0, 36, 0, 0, 0, 0, 0, 0, 0, 0, 0, 155, 0};
  13. 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};
  14. 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};
  15. contentPane.setLayout(gbl_contentPane);
  16. JComboBox comboBox = new JComboBox(platesAsArray);
  17. GridBagConstraints gbc_comboBox = new GridBagConstraints();
  18. gbc_comboBox.insets = new Insets(0, 0, 5, 5);
  19. gbc_comboBox.fill = GridBagConstraints.HORIZONTAL;
  20. gbc_comboBox.gridx = 1;
  21. gbc_comboBox.gridy = 2;
  22. contentPane.add(comboBox, gbc_comboBox);

这就是我的gui显示的内容
我也得到这个错误:

  1. regPlate
  2. la1 abc
  3. la6 5gb
  4. rifol1
  5. SX72 44
  6. 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')
  7. at prototype.GUI.<init>(GUI.java:98)
  8. at prototype.GUI$1.run(GUI.java:43)
  9. at java.desktop/java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:316)
  10. at java.desktop/java.awt.EventQueue.dispatchEventImpl(EventQueue.java:770)
  11. at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:721)
  12. at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:715)
  13. at java.base/java.security.AccessController.doPrivileged(AccessController.java:391)
  14. at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:85)
  15. at java.desktop/java.awt.EventQueue.dispatchEvent(EventQueue.java:740)
  16. at java.desktop/java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:203)
  17. at java.desktop/java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:124)
  18. at java.desktop/java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:113)
  19. at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:109)
  20. at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
  21. at java.desktop/java.awt.EventDispatchThread.run(EventDispatchThread.java:90)

任何帮助都将不胜感激。

vpfxa7rd

vpfxa7rd1#

正如在评论中已经指出的,第一件事就是修正你的输入。在getallregplates()中,实际上并没有将所有输入汇总到一个集合或数组中。此外,你还返回颜色,这甚至没有定义在那里。
这是你的 getAllRegPlates() 这将返回一个 List 包含在csv文件中找到的所有图版的字符串

  1. public List<String> getAllRegPlates() {
  2. String currentLine = "";
  3. List<String> allRegPlates = new ArrayList<>();
  4. String[] tempRegPlates;
  5. try (BufferedReader br = new BufferedReader(new FileReader("Cars.csv"))) {
  6. while ((currentLine = br.readLine()) != null) {
  7. tempRegPlates = currentLine.split(",");
  8. System.out.println(tempRegPlates[0]);
  9. // Add all found plates to your collection
  10. allRegPlates.add(tempRegPlates[0]);
  11. }
  12. } catch (IOException e) {
  13. e.printStackTrace();
  14. }
  15. // finally, return the list of regplates
  16. return allRegPlates;
  17. }

由于输入现在应该起作用,下一步将是更正 JComboBox . 这看起来像这样:

  1. Cars cars = new Cars();
  2. List<String> plates = cars.getAllRegPlates(); // get all the plates as list
  3. String[] platesAsArray = (String[]) plates.toArray(); // convert to String array
  4. JComboBox comboBox = new JComboBox<String>(platesAsArray); // create the combobox
  5. // finally, do layout stuff and add to your frame / panel
展开查看全部

相关问题