【问题标题】:Search Array Error搜索数组错误
【发布时间】:2014-04-16 22:51:10
【问题描述】:

我在这个程序中使用 netbeans 时遇到问题。该程序用于通过数组从导入的文本文件中搜索,文本文件有几列和几行,与汽车的几个特征(即车牌号)有关。您需要为每个选项创建对话框来搜索您要查找的内容。问题出在以 one.setPlate 开头的代码 sn-p 的末尾。所有 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

【问题讨论】:

  • “所有的 one.set(x) 都被标记”是什么意思?你能解释一下吗?
  • 例如 one.setPlate、one.setYear、one.setMfg、one.setStyle、one.setColor。
  • 是的,很清楚。但是你说的标记是什么意思?
  • NetBeans 错误:在 vehicleerach.VehicleSearch 中创建方法“setPlate(java.lang.String)”。 setYear、setMfg、setStyle 和 setColor 也会出现同样的错误。
  • 你能把vehicleserach.VehicleSearch代码也显示一下吗?

标签: java arrays swing netbeans


【解决方案1】:

我在那里做了一些更正。现在它正在工作。我用盘子检查过。你能检查一下吗。我认为代码可以进一步改进。

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

【讨论】:

  • 我已经添加了带有 setter 和 getter 的车辆类,以及整个主类代码。
  • 你也想要文本文件吗?
  • 我根据其他成员的请求稍微更改了代码,我还添加了文本文件。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-03-25
  • 2016-10-03
  • 1970-01-01
  • 2012-04-25
  • 2018-09-14
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多