简体   繁体   English

从文件的数组列表中查找并显示名称

[英]Finding and displaying a name from an array list in a file

I am trying to write a program that uses data from an input file which has been stored into an array so the user can search/find specific names. 我正在尝试编写一个程序,该程序使用存储在数组中的输入文件中的数据,以便用户可以搜索/查找特定名称。 Right now I am working on the findName method which should take a String and search through all the names (not case-sensitive). 现在,我正在研究findName方法,该方法应该使用String并搜索所有名称(不区分大小写)。 If the name is found, the NameRecord is returned, otherwise it returns null. 如果找到名称,则返回NameRecord,否则返回null。 When the user clicks the Find button the event handler (listener) will call findName(), and display the name's ranking for each decade in the JTextArea. 当用户单击“查找”按钮时,事件处理程序(侦听器)将调用findName(),并在JTextArea中显示每十年的名称排名。 However, when I run my program to see if it's working correctly and I push the find button nothing happens. 但是,当我运行程序以查看程序是否正常运行并按“查找”按钮时,没有任何反应。 So I believe my problem is that I have either not written my find method correctly or I have done something wrong with my actionlistener, but I think it's my find method. 因此,我认为我的问题是我要么没有正确编写find方法,要么我的动作侦听器做错了什么,但是我认为这是我的find方法。 However, I've tried numerous things with my find method, but nothing seems to work. 但是,我已经使用find方法尝试了很多事情,但是似乎没有任何效果。 So could someone help/explain what I am doing wrong. 所以有人可以帮助/解释我在做什么错。

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import java.util.Scanner;

public class NameSurfer extends JFrame {

  //Declare fields for GUI components
  private NameRecord[] namesArray;
  private JTextArea displayArea;
  private JPanel buttonPanel;
  private JTextField nameField;
  private JButton findButton;
  private JButton matchButton;
  private ButtonListener listener;
  //private GraphPanel graph;
  private JButton graphButton;
  private JButton clearOneButton;
  private JButton clearAllButton;

  /*
  * Constuctor for NameSurfer class
 */
  public NameSurfer() {

    //Build GUI
    super("Name Surfer");
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

//Set layout
this.setLayout(new BorderLayout());

//Creates the main panel
JPanel mainPanel = new JPanel();

//Creates the JScrollPane
JScrollPane scroller = new JScrollPane();
displayArea = new JTextArea();
displayArea.setEditable(false);
scroller = new JScrollPane(displayArea);
scroller.setPreferredSize(new Dimension(250, 600));

//Create components
nameField = new JTextField(15);
findButton = new JButton("Find");
matchButton = new JButton("Match");

//Add components to the panel
mainPanel.add(nameField);
mainPanel.add(findButton);
mainPanel.add(matchButton);

//Calls the read method
read("names-data.txt");

findButton.addActionListener(new ButtonListener());

//Add the panel to this JFrame
this.add(mainPanel, BorderLayout.SOUTH);

//Add scroller to this JFrame
this.add(scroller, BorderLayout.CENTER);

//Sizes the frame
this.pack();

//Make the JFrame visible on the screen
this.setVisible(true);
 }

  /**
  *  Method to read input from file
  */
   private void read(String fileName)
  {
    StringBuilder sb = new StringBuilder();
     try {
      String inputLine;
  Scanner inFile = new Scanner(new File(fileName));

  int i = 0;
  int num = Integer.parseInt(inFile.nextLine().trim());
  namesArray = new NameRecord[num];

  String inputString;
  while (inFile.hasNextLine()) {
    inputString = inFile.nextLine();
    namesArray[i] = new NameRecord(inputString);
    displayArea.append(namesArray[i].toString() + "\n");
  }
  }
 catch(IOException io){
  System.out.print(io);
  }
 }

  /**
 * Method to find name when the find button is pushed
 */
 private NameRecord findName(String targetName)
  {
  int i = 0;
  if (namesArray[i].getName().indexOf(targetName) >= 0)
  {
    displayArea.append(namesArray[i].getName() + "   " );
    i++;
  }

  return namesArray[i];
  }

  /**
  *
   */
   private class ButtonListener implements ActionListener {
  public void actionPerformed(ActionEvent e) {
    findName(nameField.getText());
   }

  }

  /**
  *  The main method creates...
  */
  public static void main(String[] args) {
   NameSurfer frame = new NameSurfer();
 }
}

Also here is my NameRecord code(if you need it)... 这也是我的NameRecord代码(如果需要)...

 /** Class to hold a name and it's rank over the years
 */
 import java.util.Scanner;

 public class NameRecord
 {
   private String name;
   private int[] rank;
   static final int START = 1900;
   static final int DECADES = 12;

 /** Constructor for NameRecord
 *
 * @param nameData string that contains a name and it's rank over the years
 */
  public NameRecord(String nameData) {
  Scanner scan = new Scanner(nameData);
  name = scan.next();

  //Adds a name's ranked values to the array
  rank = new int[DECADES];
  for (int i = 0; i < rank.length; i++)
  rank[i] = scan.nextInt();
   }

   /** Accessor method for NameRecord object's name
  *
  * @return value of NameRecord object's name
  */
   public String getName() {
   return name;
   }

  /** Accessor method for NameRecord object's rank
  *
  * @return value of NameRecord object's rank
  */
  public int getRank(int decade) {
   int decadeRank = rank[decade];
    return decadeRank;
  }

  /** returns the best decade
  *
  * @return the best decade
  */
  public int bestDecade() {
    int best = rank[0];
   for(int i=1; i<DECADES; i++)
    if(rank[i] > best)
    best = rank[i];
  return best;
  }

 /**
 * toString method for NameRecord class
 * @return String representation of the NameRecord object
 */
 public String toString() {
 String out = getName();

  //Add name's ranked values to the toString method
  for (int i = 0; i < rank.length; i++)
  out +=" " + rank[i];

  return out;
   }
   }

you always find the first one because whenever you press the button then the i will be 0 (i=0) 您总是会找到第一个,因为只要您按一下按钮,i就会为0(i = 0)

you should do a for loop to go through all the values when you click the button 当您单击按钮时,应进行for循环以遍历所有值

for(int i = 0;i<namesArray.length;i++){
 if (namesArray[i].getName().indexOf(targetName) >= 0)
  {
    displayArea.append(namesArray[i].getName() + "   " );
    return namesArray[i];
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM