我在这个程序中使用netbeans时遇到问题。该程序用于从导入的文本文件中通过数组进行搜索,该文本文件有几列和几行,与汽车的几个特征(即车牌号)有关。您需要为每个选项创建对话框来搜索您要查找的内容。问题出现在以one.setPlate开头的代码段的末尾。所有的one.set(x)都被标记,原因是什么?
package vehiclesearch;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.*;
import javax.swing.JOptionPane;
public class VehicleSearch {
private static String[] sliced;
public static void main
(String[] args) throws FileNotFoundException {String NameOfFile =
JOptionPane.showInputDialog("Where is the File Path to Import Located at?");
File textFile = new File((NameOfFile));
Scanner in = new Scanner (textFile);
//Imports the appropriate text file named "vehicles.txt"//
ArrayList <String> carsArrayList = new ArrayList <>();
while(in.hasNextLine()){
String line = in.nextLine();
carsArrayList.add(line);
}
in.close();
ArrayList<VehicleSearch> vehicleObs = new ArrayList<>();
for (int i= 0; i <carsArrayList.size(); i++){
String temp = carsArrayList.get(i);
VehicleSearch VehicleClass = new VehicleSearch ();
String[] sliced = temp.split("\s+");
vehicleClass.setPlate(sliced[0]);
int a = Integer.parseInt(sliced[1]);
vehicleClass.setYear(a);
vehicleClass.setMfg(sliced[2]);
vehicleClass.setStyle(sliced[3]);
vehicleClass.setColor(sliced[4]);
vehicleObs.add(vehicleClass);
boolean stepTwo = true;
while(stepTwo) {
String search;
search = JOptionPane.showInputDialog("Please choose one of the following search options" + "1 Plate" + "n" +"2 Year" + "3 Manufacturer" + "n" + "4 Body Style"+ "n"+ "5 Color");
int searchOption = Integer.parseInt(search);
String searchVal;
searchVal = JOptionPane.showInputDialog("Enter a value to search for" + "n" + "All values must be exact in wording, please be careful when using Caps or check for Caps Lock");
searchDialogs(vehicleObs, searchOption, searchVal);
String c;
c = JOptionPane.showInputDialog("Type S to search | E to exit");
String b = "S";
if(c.equalsIgnoreCase(b)){
stepTwo = true;
}
else{
stepTwo= false;
}
}
}
}
public static String plateCheck(ArrayList list, String search){
ArrayList<String> as = new ArrayList<>();
for (int i = 0; i< list.size(); i++){
String temp = list.get(i).toString();
if(temp.contains(search)){
as.add(list.get(i).toString());
}
}
return alFormat(as);
}
public static String yearCheck(ArrayList list, String search){
ArrayList<String> as = new ArrayList<>();
for (int i = 0; i< list.size(); i++){
String temp = list.get(i).toString();
as.add(list.get(i).toString());
}
return alFormat(as);
}
public static String mfgCheck(ArrayList list, String search){
ArrayList<String> as = new ArrayList<>();
for (int i = 0; i< list.size(); i++){
String temp = list.get(i).toString();
as.add(list.get(i).toString());
}
return alFormat(as);
}
public static String StyleCheck(ArrayList list, String search){
ArrayList<String> as = new ArrayList<>();
for (int i = 0; i< list.size(); i++){
String temp = list.get(i).toString();
as.add(list.get(i).toString());
}
return alFormat(as);
}
public static String colorCheck(ArrayList list, String search){
ArrayList<String> as = new ArrayList<>();
for (int i = 0; i< list.size(); i++){
String temp = list.get(i).toString();
as.add(list.get(i).toString());
}
return alFormat(as);
}
public static void searchDialogs(ArrayList vehicleObs, int searchOption, String
searchVal){
if(searchOption == 1){
JOptionPane.showMessageDialog(null, "Your following search yielded the following
results" + "n" + plateCheck(vehicleObs, searchVal));
}//Outputs data (Plate Numbers) from the text file imported
else if(searchOption == 2){
JOptionPane.showMessageDialog(null, "Your following search yielded the following
results" + "n" + yearCheck(vehicleObs, searchVal));
}//Outputs data (Year) from the text file imported
else if(searchOption == 3){
JOptionPane.showMessageDialog(null, "Your following search yielded the following
results" + "n" + mfgCheck(vehicleObs, searchVal));
}//Outputs data (Manufacturer) from the text file imported
else if(searchOption == 4){
JOptionPane.showMessageDialog(null, "Your following search yielded the following
results" + "n" + styleCheck(vehicleObs, searchVal));
}//Outputs data (Style) from the text file imported
else if(searchOption == 5){
JOptionPane.showMessageDialog(null, "Your following search yielded the following
results" + "n" + colorCheck(vehicleObs, searchVal));
}//Outputs data (Color) from the text file imported
else{
JOptionPane.showMessageDialog(null, "Sorry no results have been found");
}//will only output this message only if nothing is found,//
}
public static String alFormat(ArrayList too){
String tmpString;
String full = "";
for( Object aValue | too){
tmpString = aValue + "n";
full = full + tmpString;
}
return null;
}
}
//Vehicle Class//
public class VehicleClass {
protected String plate;//car plate
protected int year;//car model year
protected String mfg;//manufacturer
protected String style;// car style
protected String color;//color of car
public VehicleClass(){
plate = null;
year = 0000;
style = null;
color = null;
}
public VehicleClass (String e , int r, String g, String y, String c){
plate = e;
year = r;
mfg = g;
style = y;
color = c;
}
public VehicleClass copyVehicle(VehicleClass k){
VehicleClass k1 = new VehicleClass();
k1.setPlate(k.getPlate ());
k1.setYear(k.getYear());
k1.setMfg(k.getMfg());
k1.setStyle(k.getStyle());
k1.setColor(k.getColor());
return k1;
}
public void setPlate(String e){
plate= e;
}
public void setYear(int r){
year = r;
}
public void setMfg(String g){
mfg = g;
}
public void setStyle(String y){
style = y;
}
public void setColor(String c){
color = c;
}
public String getPlate(){
return plate;
}
public int getYear(){
return year;
}
public String getMfg(){
return mfg;
}
public String getStyle(){
return style;
}
public String getColor(){
return color;
}
@Override //need this?
public String toString(){
return (plate + "t" + year + "t" + mfg +"t"+ style + "t" + color);
}
}
//Text File to import//
A3245D 2009 Ford sedan white
B3396 2011 GMC pickuup blue
S214X 2010 Toyota sedan white
TR3396 2009 BMW sedan black
XR295 2011 Honda pickuup red
Z2349A 2012 Toyota suv silver
IMAQT 2009 Honda suv blue
我在那里做了一些更正。现在它正在发挥作用。我用盘子检查了一下。你能检查一下吗?我认为代码可以进一步改进。
VehicleSearch.java
public class VehicleSearch {
private static String[] sliced;
public static void main(String[] args) throws FileNotFoundException {
String NameOfFile = JOptionPane.showInputDialog("Where is the File Path to Import Located at?");
File textFile = new File((NameOfFile));
Scanner in = new Scanner (textFile);
//Imports the appropriate text file named "vehicles.txt"//
ArrayList <String> carsArrayList = new ArrayList <String>();
while(in.hasNextLine()){
String line = in.nextLine();
carsArrayList.add(line);
}
in.close();
ArrayList<VehicleClass> vehicleObs = new ArrayList<VehicleClass>();
for (int i= 0; i <carsArrayList.size(); i++){
String temp = carsArrayList.get(i);
VehicleClass one = new VehicleClass ();
String[] sliced = temp.split("\s+");
one.setPlate(sliced[0]);
int a = Integer.parseInt(sliced[1]);
one.setYear(a);
one.setMfg(sliced[2]);
one.setStyle(sliced[3]);
one.setColor(sliced[4]);
vehicleObs.add(one);
boolean stepTwo = true;
while(stepTwo) {
String search;
search = JOptionPane.showInputDialog("Please choose one of the following search options" + "1 Plate" + "n" +"2 Year" + "3 Manufacturer" + "n" + "4 Body Style"+ "n"+ "5 Color");
int searchOption = Integer.parseInt(search);
String searchVal;
searchVal = JOptionPane.showInputDialog("Enter a value to search for" + "n" + "All values must be exact in wording, please be careful when using Caps or check for Caps Lock");
searchDialogs(vehicleObs, searchOption, searchVal);
String c;
c = JOptionPane.showInputDialog("Type S to search | E to exit");
String b = "S";
if(c.equalsIgnoreCase(b)){
stepTwo = true;
}
else{
stepTwo= false;
}
}
}
}
public static String plateCheck(ArrayList list, String search){
ArrayList<String> as = new ArrayList<String>();
for (int i = 0; i< list.size(); i++){
String temp = list.get(i).toString();
if(temp.contains(search)){
as.add(list.get(i).toString());
}
}
return alFormat(as);
}
public static String yearCheck(ArrayList list, String search){
ArrayList<String> as = new ArrayList<String>();
for (int i = 0; i< list.size(); i++){
String temp = list.get(i).toString();
as.add(list.get(i).toString());
}
return alFormat(as);
}
public static String mfgCheck(ArrayList list, String search){
ArrayList<String> as = new ArrayList<String>();
for (int i = 0; i< list.size(); i++){
String temp = list.get(i).toString();
as.add(list.get(i).toString());
}
return alFormat(as);
}
public static String styleCheck(ArrayList list, String search){
ArrayList<String> as = new ArrayList<String>();
for (int i = 0; i< list.size(); i++){
String temp = list.get(i).toString();
as.add(list.get(i).toString());
}
return alFormat(as);
}
public static String colorCheck(ArrayList list, String search){
ArrayList<String> as = new ArrayList<String>();
for (int i = 0; i< list.size(); i++){
String temp = list.get(i).toString();
as.add(list.get(i).toString());
}
return alFormat(as);
}
public static void searchDialogs(ArrayList vehicleObs, int searchOption, String
searchVal){
if(searchOption == 1){
JOptionPane.showMessageDialog(null, "Your following search yielded the following results" + "n" + plateCheck(vehicleObs, searchVal));
}//Outputs data (Plate Numbers) from the text file imported
else if(searchOption == 2){
JOptionPane.showMessageDialog(null, "Your following search yielded the following results" + "n" + yearCheck(vehicleObs, searchVal));
}//Outputs data (Year) from the text file imported
else if(searchOption == 3){
JOptionPane.showMessageDialog(null, "Your following search yielded the following results" + "n" + mfgCheck(vehicleObs, searchVal));
}//Outputs data (Manufacturer) from the text file imported
else if(searchOption == 4){
JOptionPane.showMessageDialog(null, "Your following search yielded the following results" + "n" + styleCheck(vehicleObs, searchVal));
}//Outputs data (Style) from the text file imported
else if(searchOption == 5){
JOptionPane.showMessageDialog(null, "Your following search yielded the following results" + "n" + colorCheck(vehicleObs, searchVal));
}//Outputs data (Color) from the text file imported
else{
JOptionPane.showMessageDialog(null, "Sorry no results have been found");
}//will only output this message only if nothing is found,//
}
public static String alFormat(ArrayList too){
String tmpString;
String full = "";
for( Object aValue : too){
tmpString = aValue + "n";
full = full + tmpString;
}
return full;
}
}
VehicleClass.java
public class VehicleClass {
protected String plate;//car plate
protected int year;//car model year
protected String mfg;//manufacturer
protected String style;// car style
protected String color;//color of car
public VehicleClass(){
plate = null;
year = 0000;
style = null;
color = null;
}
public VehicleClass (String e , int r, String g, String y, String c){
plate = e;
year = r;
mfg = g;
style = y;
color = c;
}
public VehicleClass copyVehicle(VehicleClass k){
VehicleClass k1 = new VehicleClass();
k1.setPlate(k.getPlate ());
k1.setYear(k.getYear());
k1.setMfg(k.getMfg());
k1.setStyle(k.getStyle());
k1.setColor(k.getColor());
return k1;
}
public void setPlate(String e){
plate= e;
}
public void setYear(int r){
year = r;
}
public void setMfg(String g){
mfg = g;
}
public void setStyle(String y){
style = y;
}
public void setColor(String c){
color = c;
}
public String getPlate(){
return plate;
}
public int getYear(){
return year;
}
public String getMfg(){
return mfg;
}
public String getStyle(){
return style;
}
public String getColor(){
return color;
}
@Override //need this?
public String toString(){
return (plate + "t" + year + "t" + mfg +"t"+ style + "t" + color);
}
}
车辆信息文件
AB-2323 1900 Japan Sport Black
AB-2323 1990 German Business White
AB-2323 2000 USA Army Blue