配列に格納された入力ファイルのデータを使用して特定の名前を検索/検索できるプログラムを作成しようとしています。今私は文字列を取得し、すべての名前(大文字小文字を区別しない)を検索するfindNameメソッドに取り組んでいます。名前が見つかった場合はNameRecordが返され、そうでない場合はnullが返されます。ユーザーがFindボタンをクリックすると、イベントハンドラ(リスナー)はfindName()を呼び出し、JTextAreaに各10年の名前のランクを表示します。しかし、プログラムが正常に動作しているかどうかを確認するために、検索ボタンを押すと何も起こりません。だから私は私の問題は、私は正しく私の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();
}
}
ボタンを押すたび、私は0になりますので、私のNameRecordコードiは=(あなたは常に最初のものを見つける
/** 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;
}
}
同じ過ちをクリックすると、すべての値を通過するループのために行う必要があります。ファイル上のwhileは増分しません。したがって、最初のセルはinitだけです。 – AxelH
findNamesメソッドでこれを試してみると、return文が見つからないというエラーメッセージが表示されますが、namesArray [i] return文ではありませんか?なぜこれが起こっているのか説明できますか? – user6259845
メソッドの最後にnullを返す必要があります。エラーは消えます。 – Arctigor