java - 根据用户输入从LinkedList获取值

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

我的实验室代码面临一些问题 我已经完成了故障排除,发现我的文件读取器/缓冲读取器、车辆方法和 LinkedList 值都没有问题

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

主要方法

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); 
    }
}

最佳答案

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

关于java - 根据用户输入从LinkedList获取值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37482491/

相关文章:

java - 在邻接矩阵中运行 Dijkstra 算法后,线程 "main"java.lang.StackOverflowError 中出现异常

java - 无法解决 websphere 异常

sql - PostgreSQL "IF"语法错误

c# - 在 if/else 中找不到按钮的变量

java - 将单链表转换为双链表

java - 如何创建通用 <T> 类以便 T 扩展 Collections<String>?

java - 推荐的字节码操作库,用于重写类文件以更改类型 e。 G。领域?

python - 在if条件下使用两个任意功能时出错

c - 无法在Linux中使用C创建链表

mysql - 对表中的增量求和的sql查询