Run FOR cycle within another FOR cycle

Asked

Viewed 250 times

0

for(int g=0;g<vl.VerbsList.length;g++){ // primeiro loop ok
    for(int h=0;h<TextParts.length;h++){ //segundo loop deveria ser executada por inteiro
        for(int j =0;j<BaseWord.length();j++){
              try{
                  if(BaseWord.charAt(j) == TextParts[h].charAt(j)){
                      System.out.println("BaseWord "+BaseWord);
                      System.out.println("TextParts "+TextParts[h]);
                      System.out.println("h "+h);
                      TempPoints += 1;
                  }
              } catch(Exception e){
                  TempPoints = 0;
                  j = BaseWord.length();
              }
          }

        //Ciclo Text Parts
        if(TempPoints > Points) {
            Word = TextParts[h];
            TempPosition = h;
            Points = TempPoints;
            TempPoints = 0;
            BaseWord = "";
            System.out.println("Points: "+Points);
        } else {
            TempPoints = 0;
            BaseWord = "";
        }
    }
    //Ciclo de Verbal List
}

I want in an execution of that cycle:

for(int g=0;g<vl.VerbsList.length;g++){

The program runs this whole other cycle:

for(int h=0;h<TextParts.length;h++){

(The first is has to wait for the second to finish to continue)

So I can compare the characters of a text with the characters of the other text.

But the first is not waiting for the second to end

Testing: inserir a descrição da imagem aqui

The value of "h" always and 0. That means she’s always starting over.

Where I went wrong?

(The topic code is not the complete code)

2 answers

1

I figured it out. Apparently the error was time to reset the variable "Baseword"

inserir a descrição da imagem aqui

    /*
 * 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 dino.ai;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Matheus Markies
 */
public class VerbDetector extends Thread{

    String Text;
    String[] TextParts;

    List<String> SimilarVerbs = new ArrayList<String>();
    List<String> TextVerbs = new ArrayList<String>();

    List<Integer> VerbsPositions = new ArrayList<Integer>();
    int TempPosition;

    boolean Analyze;

    int TempPoints;
    int Points;

    int b;

    String Word;
    String BaseWord;
    int Proof;

    public void run(){

        while (Analyze) {            

    Verbs vl = new Verbs();
    TextParts = Text.split(" ");

    for(int g=0;g<vl.VerbsList.length;g++){
        BaseWord = "";
        if(vl.VerbsList[g].length() > 2){
       for(int i = 0;i<vl.VerbsList[g].length();i++){
                if(i<(vl.VerbsList[g].length()-2)){
                    BaseWord = BaseWord + vl.VerbsList[g].charAt(i);
                }
            }
        }else{
            BaseWord = vl.VerbsList[g];
        }

        for(int h=0;h<TextParts.length;h++){


            //if(TextParts[h].length()>vl.VerbsList[g].length()){
                for(int j =0;j<BaseWord.length();j++){

                  try{
                  if(BaseWord.charAt(j) == TextParts[h].charAt(j)){
                      //System.out.println("BaseWord "+BaseWord);
                      //System.out.println("TextParts "+TextParts[h]);
                      //System.out.println("Tamanho TextParts: " + TextParts.length);
                      //System.out.println("Tamanho VerbsList: "+vl.VerbsList.length);

                      //System.out.println("h "+h);
                       TempPoints += 1;
                  }else{

                  }
                  }catch(Exception e){
                      TempPoints = 0;
                      j++;
                  }

                }

            //}else{
             //   for(int j =0;j<TextParts[h].length();j++){
               //   try{
                //    if(TextParts[h].charAt(j) == vl.VerbsList[g].charAt(j)){
                //       TempPoints += 1; 
                 // }
                 // }catch(Exception e){
                   //   TempPoints = 0;
                  //    j++;
                  //}
                //}
            //}
            //Ciclo Text Parts

            if(TempPoints > Points){
            Word = TextParts[h];
            TempPosition = h;
            Points = TempPoints;
            TempPoints = 0;
            //BaseWord = "";
            System.out.println("Points: "+Points);
            }else{
            TempPoints = 0;
            //BaseWord = "";
            }
             //System.out.println("h "+h);


       }

        //Ciclo de Verbal List
        if(vl.VerbsList[g].length() > 2){
        Proof = (100*Points)/(vl.VerbsList[g].length() - 2);
        }else{
        Proof = (100*Points)/(vl.VerbsList[g].length());   
        }
        if(Proof >= 90){
            System.out.println("Proof: "+Proof);
            for(int i = 0;i<SimilarVerbs.size();i++){
                if(SimilarVerbs.get(i) == vl.VerbsList[g]){
                    b +=1;
                }
            }
            if(b == 0){
            SimilarVerbs.add(vl.VerbsList[g]);
            TextVerbs.add(Word);
            VerbsPositions.add(TempPosition);

            System.out.println("SimilarVerbs "+SimilarVerbs);

            }else{
             b = 0;
            }
            Points = 0;
        }else{
            Points = 0;
        }

    }

    for(int i=0;i<SimilarVerbs.size();i++){
    LanguageProcessingManager.CA.SetSimilarWord(SimilarVerbs.get(i));
    LanguageProcessingManager.CA.SetVerb(TextVerbs.get(i));   
    }

    LanguageProcessingManager.GOSI.SetText(Text);
    int size = VerbsPositions.size();
    int[] result = new int[size];
    Integer[] temp = VerbsPositions.toArray(new Integer[size]);
    for (int n = 0; n < size; ++n) {
    result[n] = temp[n];
    }
    LanguageProcessingManager.GOSI.SetVerbsPositions(result);
    Analyze = false;

            try {
                Thread.sleep(10);
            } catch (InterruptedException ex) {
                Logger.getLogger(VerbDetector.class.getName()).log(Level.SEVERE, null, ex);
            }

    }//Analyze

    }

    public void SetTextAnalyze(String SetValue){
        Text = SetValue;
        //SimilarVerbs.clear();
        //TextVerbs.clear();
        Analyze = true;
    }
    public List<String> GetSimilarVerbs(){
    return SimilarVerbs;
    }
    public List<String> GetVerbs(){
    return TextVerbs;
    }
    public int[] GetVerbsPosition(){

    int size = VerbsPositions.size();
    int[] result = new int[size];
    Integer[] temp = VerbsPositions.toArray(new Integer[size]);
    for (int n = 0; n < size; ++n) {
    result[n] = temp[n];
    }

    return result;
    }

}

That and the entire code for anyone who wants to take a look.

Thank you!

0

Theoretically, being nested work the way you want, i.e., for each external iteration there are all possible iterations of the internal is. However, seemingly strange things can occur if the conditional parts of the are not well designed. Are you sure that the size defined by Verbslist.length is larger than one (1)? And of Textparts.length? What it looks like is that Verbslist is larger than one (1) - appears to be ten (10) in size, but Textparts is one (1), hence it is always bringing the index 0 (one iteration). Apply a System.out.println("Size: " + Textparts.lenght) to see the size of this array. Anything update your question by clarifying better where these arrays come from.

Note: it is good practice to name variables with lower case letter.

  • https://imgur.com/PmGw6gT (Textparts.length = 11)(Vl.VerbsList.length = 5000)

  • Look at these parts: Verbslist.length, Textparts.length, Baseword.length(). Can you see any difference?

  • Post the entire code so we can test.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.