Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Uitableview 递归方法不增加TableView GUI(JavaFX)中的字符串分析计数器_Uitableview_Recursion_Javafx - Fatal编程技术网

Uitableview 递归方法不增加TableView GUI(JavaFX)中的字符串分析计数器

Uitableview 递归方法不增加TableView GUI(JavaFX)中的字符串分析计数器,uitableview,recursion,javafx,Uitableview,Recursion,Javafx,编辑计数器增量需要递归 我有一个GUI,它将输入的字段添加到ListView中,但也将对象的名称添加到它旁边的TableView中,并显示四个子字符串计数。它们是Plant对象,应该显示在ListView中(确实如此),然后在出现某个子字符串时显示名称: 请看这里: 我不明白我遗漏了什么,因为在addButtonClick方法中,我调用了递归方法。该方法中的逻辑对我来说似乎是正确的,因此我必须在整数属性或构造函数中缺少某些内容 以下是控制器中“添加”按钮单击的方法。有一个组合框,用户可以在其中

编辑计数器增量需要递归

我有一个GUI,它将输入的字段添加到ListView中,但也将对象的名称添加到它旁边的TableView中,并显示四个子字符串计数。它们是Plant对象,应该显示在ListView中(确实如此),然后在出现某个子字符串时显示名称:

请看这里:

我不明白我遗漏了什么,因为在addButtonClick方法中,我调用了递归方法。该方法中的逻辑对我来说似乎是正确的,因此我必须在整数属性或构造函数中缺少某些内容

以下是控制器中“添加”按钮单击的方法。有一个组合框,用户可以在其中选择花、真菌、杂草或药草。当用户点击相应的植物类型时,相应的单选按钮就会出现,这些单选按钮与某些特征有关。所有这些都有效。我将显示代码的花部分:

    /*
Adds respective plant type and resets controls
 */
public void handleAddButtonClick(ActionEvent event) {
    if (idInput != null && nameInput != null & colorInput != null) {
        if (plantType.getValue().equals("Flower")) {
            Flower flower = new Flower(ID, idNum, name, color, smell, thorns, edible, poisonous, flavor, medicine, seasonal);
            flower.setID(idInput.getText());
            flower.setName(nameInput.getText());
            flower.setColor(colorInput.getText());
            flower.setSmell(scentedRadio.isSelected());
            flower.setThorns(thornyRadio.isSelected());
            observablePlantList.add(flower);

            //this is where the table information gets added.
            //it adds it, just displays 0's.

            flower.setPlantName(nameInput.getText());
            Plant.substringCounter(name, "e"); //tried flower instead
            Plant.substringCounter(name, "ar");//of Plant. still nothing.
            Plant.substringCounter(name, "er");
            Plant.substringCounter(name, "o");
            observableAnalysisList.add(flower);

            //just doing some debug printing. this prints 0 
            System.out.println(Plant.substringCounter(name, "e"));

            //more debugging. this prints fine because of toString() method
            System.out.println(flower);
以下是主类和Flower类中的相关代码:

//Plant Table properties ("e", "o", "ar", "er")
public StringProperty plantName = new SimpleStringProperty(this, "plantName", "");
public String getPlantName() {return plantName.get(); }
public StringProperty plantNameProperty() {return plantName; }
public void setPlantName(String plantName) {this.plantName.set(plantName); }

public IntegerProperty countLetterE = new SimpleIntegerProperty(this, "countLetterE", 0);
public int getLetterE() {return countLetterE.get();}
public IntegerProperty eProperty() {return countLetterE; }
public void setCountLetterE(int countLetterE) {this.countLetterE.set(countLetterE);}

public IntegerProperty countLetterO = new SimpleIntegerProperty(this, "countLetterO", 0);
public int getLetterO() {return countLetterO.get(); }
public IntegerProperty oProperty() {return countLetterO; }
public void setCountLetterO(int countLetterO) {this.countLetterO.set(countLetterO);}

public IntegerProperty countLetterER = new SimpleIntegerProperty(this, "countLetterER", 0);
public int getLetterER() {return countLetterER.get(); }
public IntegerProperty erProperty() {return countLetterER; }
public void setCountLetterER(int countLetterER) {this.countLetterER.set(countLetterER);}

public IntegerProperty countLetterAR = new SimpleIntegerProperty(this, "countLetterAR", 0);
public int getLetterAR() {return countLetterAR.get(); }
public IntegerProperty arProperty() {return countLetterAR; }
public void setCountLetterAR(int countLetterAR) {this.countLetterAR.set(countLetterAR);}
递归方法:

public static int substringCounter(String plantName, String letters) {

    plantName = plantName.toLowerCase();

    if(plantName.isEmpty()) {
        return 0;
    }
    if(plantName.indexOf(letters) == -1) {
        return 0;
    }

    return 1 + substringCounter(plantName.substring(plantName.indexOf(letters) + 1), letters);
}
//toString method for Plant class to display in ListView. Works fine.
public String toString() {

    return "ID: " + this.ID + "-" + this.idNum + ", Name: " + this.name + ", Color: " + this.color;
}
}
花卉类

    public class Flower extends Plant {

public Flower(String ID, int idNum, String name, String color, boolean smell, boolean thorns, boolean edible, boolean poisonous, boolean flavor, boolean medicine, boolean seasonal) {

    super(ID, idNum, name, color, smell, thorns, edible, poisonous, flavor, medicine, seasonal);

}
public void setSmell(boolean smell) {
    this.smell = smell;
}
public void setThorns(boolean thorns) {
    this.thorns = thorns;
}

//toString method for the ListView only. All works fine here

public String toString() {

    return super.toString() + ", Scent? " + this.smell + ", Thorns? " + this.thorns;
}

}

我真诚地希望我要么1)给了你足够的信息,要么2)没有给你太多的信息。感谢大家提供的帮助。

您正在对字符串调用一个静态方法,并返回一个数字,当您这样调用它时,它实际上不会产生任何结果:

Plant.substringCounter(name, "e");
如果希望执行某些“操作”或至少处理substringCounter返回的结果,则需要修改属性

此外,我不知道为什么要使用递归。这是同样的:

String text = "dandelion";
String search = "d";

int count = 0;
int pos = 0;

if (!search.isEmpty()) {
    while ((pos = text.indexOf(search, pos)) != -1) {
        count++;
        pos++;
    }
}

System.out.println(count);
基本上是这样的:

import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;

public class Main extends Application {
    @Override
    public void start(Stage primaryStage) {

        HBox box = new HBox();
        box.setSpacing(10);

        TextField sourceTextField = new TextField( "Dandelion");
        Label result = new Label();
        TextField searchTextField = new TextField();

        // add listener for counting the number of substrings
        searchTextField.textProperty().addListener(new ChangeListener<String>() {

            @Override
            public void changed(ObservableValue<? extends String> observable, String oldValue, String newValue) {

                String sourceText = sourceTextField.getText().toLowerCase();
                String searchText = newValue.toLowerCase();

                int count = 0;
                int pos = 0;

                if( !searchText.isEmpty()) {
                    while( (pos = sourceText.indexOf( searchText, pos)) != -1) {
                        count++;
                        pos++;
                    }
                }

                result.setText( String.valueOf(count));
            }
        });

        box.getChildren().addAll( new Label( "Search:"), searchTextField, new Label( "Text:"), sourceTextField, new Label( "Count:"), result);

        Scene scene = new Scene(box, 600, 200);
        primaryStage.setScene(scene);
        primaryStage.show();

    }

    public static void main(String[] args) {
        launch(args);
    }
}
导入javafx.application.application;
导入javafx.beans.value.ChangeListener;
导入javafx.beans.value.observeValue;
导入javafx.scene.scene;
导入javafx.scene.control.Label;
导入javafx.scene.control.TextField;
导入javafx.scene.layout.HBox;
导入javafx.stage.stage;
公共类主扩展应用程序{
@凌驾
公共无效开始(阶段primaryStage){
HBox box=新的HBox();
框。设置间隔(10);
TextField sourceTextField=新TextField(“蒲公英”);
标签结果=新标签();
TextField searchTextField=新建TextField();
//添加用于计算子字符串数的侦听器
searchTextField.textProperty().addListener(新的ChangeListener(){
@凌驾

更改公众假期(Observalevalue抱歉,递归是必需的。我可以在编辑中注意到这一点。为什么需要它?在当前上下文中它没有任何意义。它是必需的,因为我们不允许使用任何类型的循环来计算子字符串的频率。这是为了理解递归。我制作了递归方法,我做了一些小的递归在命令行中,我只是在尝试将它全部连接到TableView中的属性时迷失了方向。
import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;

public class Main extends Application {
    @Override
    public void start(Stage primaryStage) {

        HBox box = new HBox();
        box.setSpacing(10);

        TextField sourceTextField = new TextField( "Dandelion");
        Label result = new Label();
        TextField searchTextField = new TextField();
        searchTextField.textProperty().addListener(new ChangeListener<String>() {


            @Override
            public void changed(ObservableValue<? extends String> observable, String oldValue, String newValue) {

                String sourceText = sourceTextField.getText().toLowerCase();
                String searchText = newValue.toLowerCase();

                result.setText( String.valueOf( substringCounter( sourceText, searchText)));
            }
        });

        box.getChildren().addAll( new Label( "Search:"), searchTextField, new Label( "Text:"), sourceTextField, new Label( "Count:"), result);

        Scene scene = new Scene(box, 600, 200);
        primaryStage.setScene(scene);
        primaryStage.show();


    }

    public static int substringCounter(String plantName, String letters) {

        plantName = plantName.toLowerCase();

        if(letters.isEmpty()) {
            return 0;
        }

        if(plantName.isEmpty()) {
            return 0;
        }

        if(plantName.indexOf(letters) == -1) {
            return 0;
        }

        return 1 + substringCounter(plantName.substring(plantName.indexOf(letters) + 1), letters);
    }

    public static void main(String[] args) {
        launch(args);
    }
}