0

As you can see, these are my three classes I use for this program .. I can not find the way to let me return the two parameters in the sentence even if I do not write the keyword that I have in the array .. i would like to find the way i allow me to write input: "From roma to milano" or "to roma da milano" and have reset the parameters as they are in the code I have already developed .. according to you it is possible to achieve this, in addition to what already does the code which I have proposed to you in vision? Thanks for the attention, and for the time spent for me. Good day !

package eubot.controller;

import eubot.dictionary.EubotDictionary;

public class EubotController {
    public EubotDictionary getDictionary(String stringInput) {
        EubotDictionary dictionary = null;
        String found = null;

        for (String string1 : EubotDictionary.keyWord) {
            if (stringInput.contains(string1)) {
                dictionary = new EubotDictionary();
                dictionary.setTesto(stringInput);
                System.out.println("the string contains : " + string1);
                found = string1;
                String splittable = stringInput.substring(stringInput.indexOf(found) + found.length(),
                        stringInput.length());

                // System.out.println(splittable);
                for (String string2 : EubotDictionary.parameter) {
                    if (splittable.contains(string2)) {
                        dictionary = new EubotDictionary();
                        dictionary.setTesto(splittable);
                        found = string2;
                        String splittable1 = string2.valueOf(string2);
                        System.out.println("the parameters are : " + splittable1);
                    }
                }
                break;
            }
        }
        return dictionary;
    }

    public EubotDictionary findParametro(String stringInput) {
        EubotDictionary dictionary = null;
        for (String string2 : EubotDictionary.parameter) {
            if (stringInput.contains(string2)) {
            }
            dictionary = new EubotDictionary();
            dictionary.testo = stringInput;
        }
        return dictionary;
    }
}

package eubot.dictionary;

public class EubotDictionary {

        public String testo;

        public String getTesto() {
            return testo;
        }

        public void setTesto(String testo) {
            this.testo = testo;
        }

        public static String[] keyWord = { "devo andare", "voglio andare", "vorrei andare", "devo recarmi"};

        public static String[] parameter = { "bari", "roma", "milano", "pisa", "firenze", "napoli", "como", "torino" };

        public static String[] req;
}
package eubot.test;

import java.util.Scanner;

import eubot.controller.*;
import eubot.dictionary.*;
public class Test {
    public static void main(String[] args) {

        System.out.println("<<-|-|-|-|-|-|-|-|-|<<<Welcome In EuBot>>>|-|-|-|-|-|-|-|-|->>");
        EubotController controller = new EubotController();
        Scanner input = new Scanner(System.in);
        String string;
        while (true) {
            string = input.nextLine();
            EubotDictionary dictionary = controller.getDictionary(string);
        }
    }
}
Serra
  • 1
  • 2
  • Not sure what exactly you want to do, but to get the parameters from your inputString you could write `String[] input = inputString.split(" "); for(String i : input) { for (String p : EubotDictionary.parameter) { if(i.equals(p)) { System.out.println("Found parameter: " + p); } } } ` – pma Oct 30 '17 at 14:00
  • Thank you so much for your answer and sorry if I did not understand ... I try to be clearer ...This is the running program that shows me this output in console what i would like is that without writing the initial phrase "i have to go" and just inserting the cities i will return these as parameters .. just without using the keyword exactly .. it's possible ? <<-|-|-|-|-|-|-|-|-|<<>>|-|-|-|-|-|-|-|-|->> I have to go roma from bari The string contains : I have to go The parameters are : bari The parameters are : roma – Serra Oct 30 '17 at 14:41

1 Answers1

0

Based on your comment:

public class EubotController {

    private String[] parameter = { "bari", "roma", "milano", "pisa", "firenze", "napoli", "como", "torino" };

    public void processInputString(String inputString) {

        String[] input = inputString.split(" ");
        boolean prefixPrinted = false;

        for (String i : input) {
             for (String p : parameter) {
                if (i.equals(p)) {
                    if(!prefixPrinted) {
                        printPrefix(inputString, p);    //call when the first parameter is found
                        prefixPrinted = true;
                    }
                    System.out.println("The parameters are: " + p);
                }
            }
        }
    }

    private void printPrefix(String inputString, String parameterAfterPrefix) {
        String prefix = inputString.substring(0, 
inputString.indexOf(parameterAfterPrefix)).trim();
        System.out.println(prefix);
    }
 }

I renamed getDictionary, so you need to call now controller.processInputString(string) in your main-method.

pma
  • 860
  • 1
  • 9
  • 26
  • Thanks a lot for the proposed method .. but I have a problem: I modified the code as it has suggested .. now in the main making the appropriate changes, when I call controller.processInputString (string) eclipse tells me: Type mismatch: cannot convert from void to EubotDictionary – Serra Oct 30 '17 at 16:05
  • I solved .. I removed the part EubotDictionary dictionary = method name .. and so it works! Thousands and thousand thanks pma ! – Serra Oct 30 '17 at 16:38
  • Yes, I left out the EubotDictionary, because it did not make much sense for me in the context. If you wanted to store all parameters in this object, `testo` better be a `ArrayList`, instead of a `String`. – pma Oct 31 '17 at 09:22