根据用户输入从LinkedList获取值

时间:2016-05-27 11:35:44

标签: java if-statement linked-list user-input contains

面对我的实验室代码的一些问题 我已经完成了故障排除,发现我的filereader / bufferedreaders,Vehicle方法和LinkedList值都没有错误

我发现我在让if语句生效时遇到了问题 我不知道如何比较使用tokenizer从我的file.txt中提取的当前链表数据,使用if / else传递给userinput的给定字段?

主要方法

package test6;


// import packages
import java.util.LinkedList;
import java.io.*;
import java.util.Scanner;
import java.util.StringTokenizer;

public class Lab6 {

/**
 * @param args the command line arguments
 */
public static void main(String[] args) {
    // TODO code application logic here

    // Declare variables for reading file 
    FileReader fr = null;
    BufferedReader br = null;
    String inFile = "Vehicle_Records.txt";
    final String INPUT_PROMPT = "\nPlease enter the search word " + "that you would like to obtain more information on:";
    String line;
    StringTokenizer tokenizer;

    // Declare variables to contain the record fields

   String group; 
   String brand; 
   String model; 
   double rate; 

    // Declare and instantiate a new LinkedList
   LinkedList<Vehicle> list = new LinkedList<Vehicle>();

    try {
        // Instantiate FileReader & BufferedReader objects
        fr = new FileReader(inFile);
        br = new BufferedReader(fr);

        //read a line from the file
        line = br.readLine();

        // While line is not null
        while (line != null) {
            // Tokenize the records
            tokenizer = new StringTokenizer(line, ",");
            group = tokenizer.nextToken();
            brand = tokenizer.nextToken();
            model = tokenizer.nextToken();
            rate = Double.parseDouble(tokenizer.nextToken());
            // Create a new Vehicle object of the record
            Vehicle newVehicle = new Vehicle(group, brand, model, rate);
            System.out.println(newVehicle);
             // Add this item object into the LinkedList
            list.add(newVehicle);
            // Read another line from file
            line = br.readLine();

        }
        // Close BufferedReader
        br.close();
    } 
    catch (FileNotFoundException e) 
    {
        System.out.println("The file" + inFile + "was not found");
    } 
    catch (IOException e) 
    {
        System.out.println("Reading error!" + e);
    } 
    finally 
    {
        //Check if FileReader is opened
        if (fr != null) {
            try {
                //close FileReader
                fr.close();

            } catch (IOException e) {
                System.out.println("Error closing file!");
            }
        }
    }
      // Print out the input prompt

        System.out.println(INPUT_PROMPT);

    try 
    {

       // Create readers to read from user input
        //FileReader ufr = new FileReader(INPUT_PROMPT);
        BufferedReader ubr = new BufferedReader(new InputStreamReader (System.in));

       // Read one line from user input
       String uline=ubr.readLine();
        // Loop through all the records in the LinkedList

        for(int i = 0; i< list.size(); i++)
        {
        // if the record is the same as the input from user 
        // (Hint: use contains() in String class to check whether 
        // search word is found in the records
         String temp = new String(uline);

        if(list.get(i)== uline.contains(temp))
        {
            //print out the information of the vehicle that match user input
            System.out.println(list.get(i));

        }          


        }

    } 
    catch(IOException e)
    {
        System.out.println(e);
    }

    catch (Exception e) 
    {
        System.out.println("Input error!" + e);
    }
    }
}//main

车辆类

package lab6;


public class Vehicle {
    // Declare all the variables to contain the fields of a record 
    String group; 
    String brand; 
    String model; 
    double rate; 
// Creates a constructor to store all the fields into the variables 
    public Vehicle(String group, String brand, String model, double rate) 
    { 
        this.group=group; this.brand=brand; this.model=model; this.rate=rate; 
    } 
// Create a toString() method to return string in the same delimited 
// format as the input record 
    public String toString() 
    { 
        return(group+","+brand+","+model+","+rate); 
    }
}

3 个答案:

答案 0 :(得分:1)

您的代码不在方法内,因此您遇到了问题。

答案 1 :(得分:0)

我认为,因为您正在查看车辆对象,试图找到其四个变量之一的匹配。您的方法是错误的,因为您正在将对象与String进行比较。

相反,您可以在Vehicle类中使用Comparable接口,您只需比较多个字符串。

修改

public class Vehicle implements Comparable<String>{

/* This method returns 0 if the search matches
 * Else it return a negative or a positive number*/
@Override
public int compareTo(String o) {
    int cmp = this.getBrand().compareToIgnoreCase(o);
    if(cmp == 0) return cmp;

    cmp = this.getGroup().compareToIgnoreCase(o);
    if(cmp == 0) return cmp;

    cmp = this.getModel().compareToIgnoreCase(o);
    if(cmp == 0) return cmp;
    /* Edited this part to work with doubles */
    try{
        cmp = (int)(this.getRate() - Double.parseDouble(o));
    }
    catch(NumberFormatException e){
        return cmp;
    }

    return cmp;

}
}

以下是你如何循环它:

for(int i = 0; i< list.size(); i++){
            if(list.get(i).compareTo(uline) == 0)
            {
                System.out.println(list.get(i));

            }   
        }

希望有所帮助。

PS。我也是新手:)

答案 2 :(得分:0)

AAAAAAAND我最终与我的其他朋友一起制作了它 尽管如此,我还是要感谢所有人伸出援助之手:&#39;)

请将解决方案发布到我的问题

//-etc-
   // Create readers to read from user input
    //FileReader ufr = new FileReader(INPUT_PROMPT);
    BufferedReader ubr = new BufferedReader(new InputStreamReader (System.in));

   // Read one line from user input
   String uline=ubr.readLine();
    // Loop through all the records in the LinkedList


    for(int i = 0; i< list.size(); i++)
    {
    // if the record is the same as the input from user 
    // (Hint: use contains() in String class to check whether 
    // search word is found in the records

     Vehicle vehicle = list.get(i);

    if(vehicle.group.contains(uline) || 
       vehicle.brand.contains(uline) ||
       vehicle.model.contains(uline))
    {
        //print out the information of the vehicle that match user input
        System.out.println(list.get(i));

    }