从文件

时间:2016-05-17 07:40:06

标签: java arrays

我正在尝试编写一个程序,该程序使用已存储到数组中的输入文件中的数据,以便用户可以搜索/查找特定名称。现在我正在研究findName方法,该方法应该使用String并搜索所有名称(不区分大小写)。如果找到名称,则返回NameRecord,否则返回null。当用户单击“查找”按钮时,事件处理程序(侦听器)将调用findName(),并在JTextArea中显示每个十年的名称排名。但是,当我运行我的程序以查看它是否正常工作时,我按下查找按钮没有任何反应。所以我相信我的问题是我要么没有正确编写我的find方法,要么我的actionlistener做错了,但我认为这是我的find方法。但是,我用find方法尝试过很多东西,但似乎没什么用。那么有人可以帮助/解释我做错了什么。

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

此处还有我的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;
   }
   }

1 个答案:

答案 0 :(得分:0)

你总能找到第一个,因为无论何时按下按钮,i都将为0(i = 0)

单击按钮

时,应该执行for循环以查看所有值
for(int i = 0;i<namesArray.length;i++){
 if (namesArray[i].getName().indexOf(targetName) >= 0)
  {
    displayArea.append(namesArray[i].getName() + "   " );
    return namesArray[i];
  }
}