Function of program is already done but I need it to be crea

Function of program is already done, but I need it to be created as a JavaFX program which it\'s not. Something simple is fine. Just as long as it allows the user to input 2 strings and checks to see if the second string is a substring of the other then display at which index like the program below. Thanks in advance

import java.util.Scanner;

public class PatternMatching {
   public static void main(String[] args) {

       Scanner input = new Scanner(System.in);

       System.out.println(\"Enter the first string: \");
       String string1 = input.nextLine();

       System.out.println(\"Enter the second string: \");
       String string2 = input.next();

       int s = stringMatch(string1, string2);

       if (s != 0) {
           System.out.println(\"Matched at index \" + s);
       }
   }

   public static int stringMatch(String string1, String string2) {

       for (int p = 0; p < string1.length(); p++) {
           int k = 0;

           for (int i = p; i < string1.length(); i++) {
               if (k == string2.length()) {
                   return (i - string2.length());
               } else {
                   if (string1.charAt(i) == string2.charAt(k)) {
                       k++;
                   } else {
                       break;
                   }
               }
           }
           if (k == string2.length()) {
               return (string1.length() - string2.length());
           }
       }

       return 0;
   }
}

Solution

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package javafxapplication2;

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.GridPane;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.scene.text.Text;
import javafx.stage.Stage;

/**
*
* @author abhisheka
*/
public class JavaFXApplication2 extends Application {
  
    @Override
    public void start(Stage primaryStage) {
        Button btn = new Button();
        GridPane grid = new GridPane();
grid.setAlignment(Pos.CENTER);
grid.setHgap(10);
grid.setVgap(10);
Scene scene = new Scene(grid, 300, 275);
primaryStage.setScene(scene);
Text scenetitle = new Text(\"Chegg India: PatternMatching\");
scenetitle.setFont(Font.font(\"Tahoma\", FontWeight.NORMAL, 20));
grid.add(scenetitle, 0, 0, 2, 1);

Label firstString = new Label(\"Enter the first string:\");
grid.add(firstString, 0, 1);
TextField myTextField = new TextField();
grid.add(myTextField, 1, 1);
Label pw = new Label(\"Enter the 2nd string:\");
TextField myTextField2 = new TextField();
grid.add(myTextField2, 1, 3);
grid.add(pw, 0, 3);
btn.setText(\"submit\");
grid.add(btn, 0, 4);
Label firstStringOutPut = new Label(\"output:\");
grid.add(firstStringOutPut, 0, 5);
btn.setOnAction(new EventHandler<ActionEvent>() {

            @Override
            public void handle(ActionEvent event) {
              
               String string1 = myTextField.getText();
               String string2 = myTextField2.getText();
               int s = stringMatch(string1, string2);
               if (s != 0) {
            //System.out.println(\" \" + s);
               firstStringOutPut.setText(\"Matched at index :\"+s);
        }else firstStringOutPut.setText(\"Not Matched in index of string.\");
             
            }
        });
        primaryStage.show();
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        launch(args);
    }
    //Below is your code
    public static int stringMatch(String string1, String string2) {

        for (int p = 0; p < string1.length(); p++) {
            int k = 0;

            for (int i = p; i < string1.length(); i++) {
                if (k == string2.length()) {
                    return (i - string2.length());
                } else {
                    if (string1.charAt(i) == string2.charAt(k)) {
                        k++;
                    } else {
                        break;
                    }
                }
            }
            if (k == string2.length()) {
                return (string1.length() - string2.length());
            }
        }

        return 0;
    }
  
}

Function of program is already done, but I need it to be created as a JavaFX program which it\'s not. Something simple is fine. Just as long as it allows the us
Function of program is already done, but I need it to be created as a JavaFX program which it\'s not. Something simple is fine. Just as long as it allows the us
Function of program is already done, but I need it to be created as a JavaFX program which it\'s not. Something simple is fine. Just as long as it allows the us

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site