Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more..
    Dismiss Notice
  3. Dismiss Notice

Help trying to find a string in a text file

Discussion in 'Scripting' started by paulbell1982, Aug 9, 2020.

  1. paulbell1982

    paulbell1982

    Joined:
    Sep 28, 2018
    Posts:
    14
    I am likely missing something silly.

    I need to check if a string is contained within a text file, as the whole word.

    I have set the text file to a list and tested the list (debug the list) and the correct word comes up. I have wrote my own text file. When I check the string (user input, which calls the method from an other script, sending through a string) against the list it does not work. if I code in a word which is in the list it also does not work. If I hard code to add a word to the list it will then work. I am lost as every bit works just not when put together. I have tried uppercase/lowercase. I have also tried both list and arrays.

    Thank you in advance for any help
    Code (csharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5. using System.IO;
    6. public class WordsList : MonoBehaviour
    7. {
    8.     // each word
    9.     private List<string> _wordList = new List<string>();
    10.     // the text file to import from
    11.     [SerializeField]
    12.     private TextAsset _wordListTextFile;
    13.     // one long string for every word
    14.     private string _wordListFileWholeThing;
    15.  
    16.     private string[] _testWordArray;
    17.     // bool to test
    18.     [SerializeField]
    19.     private bool _correct;
    20.     void Start()
    21.     {
    22.         // create one long string
    23.         _wordListFileWholeThing = _wordListTextFile.ToString();
    24.         // split the string into actual words by line break and add to list
    25.         _wordList.AddRange(_wordListFileWholeThing.Split("\n"[0]));
    26.  
    27.         //TEST WORD
    28.        // _wordList.Add("BALE"); // manually adding a test word to the list
    29.  
    30.         _testWordArray = new string[_wordList.Capacity];
    31.         for (int i = 0; i < _wordList.Capacity; i++)
    32.         {
    33.             string test = _wordList.ToString();
    34.  
    35.             _testWordArray = test;
    36.         }
    37.  
    38.      
    39.  
    40.         Debug.Log(_wordList[0]);
    41.         Debug.Log(_wordList[4]);
    42.         Debug.Log(_wordList[12]);
    43.         Debug.Log(_wordList[100]);
    44.         Debug.Log(_wordList.Count);
    45.        
    46.     }
    47.     private void Update()
    48.     {
    49.         if (Input.GetKeyDown(KeyCode.L))
    50.         {
    51.             // test word checker
    52.             _correct = CheckWord(_wordList[12]); // this works
    53.  
    54.         }
    55.         if (Input.GetKeyDown(KeyCode.K)) // this does not work unless I manually add it to the list
    56.         {
    57.          
    58.             // test word checker
    59.             _correct = CheckWordArray("BALE");
    60.         }
    61.     }
    62.     public bool CheckWordArray(string word)
    63.     {
    64.         // bool to check if word is found
    65.         bool wordFound = false;
    66.        
    67.        
    68.         Debug.Log("Word to look up +" + word + "+");
    69.      
    70.  
    71.         // loop through the list of words
    72.         for(int i = 0; i< _testWordArray.Length; i++)
    73.         {
    74.             // check to see if words equal
    75.             if(Equals(_testWordArray, word))
    76.             {
    77.                 Debug.Log("word found in array");
    78.             }
    79.         }
    80.         {
    81.          
    82.         }
    83.         // after loop if word is found return true
    84.         if (wordFound == true)
    85.         {
    86.             return true;
    87.         }
    88.         // if word not found return false
    89.         else
    90.         {
    91.             return false;
    92.         }
    93.        
    94.     }
    95.     public bool CheckWord(string word)
    96.     {
    97.         // bool to check if word is found
    98.         bool wordFound = false;
    99.        
    100.         // loop through the list of words
    101.         foreach (string str in _wordList)
    102.         {
    103.             // only check if word not already found
    104.             if (wordFound == false)
    105.             {
    106.                 str.Trim();
    107.                 str.TrimEnd();
    108.                 str.TrimStart();
    109.                 //if word is found set bool true
    110.              
    111.                 if (Equals(str, word))
    112.                 {
    113.                     Debug.Log("Word found in F***ing list!");
    114.                     // set the bool true if word is found
    115.                     wordFound = true;
    116.                     // break out of loop as no need to run anyfurther
    117.                     // break;
    118.                 }
    119.             }
    120.         }
    121.         // after loop if word is found return true
    122.         if (wordFound == true)
    123.         {
    124.             return true;
    125.         }
    126.         // if word not found return false
    127.         else
    128.         {
    129.             return false;
    130.         }
    131.     }
    132. }
    133.  
     
    Last edited: Aug 9, 2020
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,769
    billybob1978 likes this.
  3. paulbell1982

    paulbell1982

    Joined:
    Sep 28, 2018
    Posts:
    14
  4. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,723
    Line 75:
    Code (CSharp):
    1. if(Equals(_testWordArray, word))
    Why are you comparing the string to the entire array. I think you probably meant this:
    Code (CSharp):
    1. if(Equals(_testWordArray[i], word))
    And while you're at it don't use the generic object.Equals method so you get compiler errors if you mess up:
    Code (CSharp):
    1. if(_testWordArray[i] == word)
     
    paulbell1982 and Kurt-Dekker like this.
  5. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,769
    It is!

    I recommend a couple of things:

    1. make a TINY list of words (like 3 different words)
    2. at line 100 above, print out the two things you're comparing

    That should cause you to immediately move to the next step of the problem. Whenever you have code that behaves weirdly as a result of data, PRINT OUT THAT DATA! It actually works 100% of the time.
     
    paulbell1982 and PraetorBlue like this.
  6. paulbell1982

    paulbell1982

    Joined:
    Sep 28, 2018
    Posts:
    14
    Thank you for your replies, I have changed the code numerous times, and I had tried the code above. I have changed it as above and will link it (correctly this time) below. I have debugged the words on a small list and yet the bool is still returning false. With the following in the console.

    just a quick note its line 82 that I am trying to get return( Debug.Log("word found in array");)

    Test Word BALE
    = BALE
    UnityEngine.Debug:Log(Object)
    WordsList:CheckWordArray(String) (at Assets/Scripts/WordsList.cs:94)
    WordsList:Update() (at Assets/Scripts/WordsList.cs:75)

    Code (csharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5. using System.IO;
    6. public class WordsList : MonoBehaviour
    7. {
    8.     // each word
    9.     private List<string> _wordList = new List<string>();
    10.     // the text file to import from
    11.     [SerializeField]
    12.     private TextAsset _wordListTextFile;
    13.     // one long string for every word
    14.     private string _wordListFileWholeThing;
    15.  
    16.     private string[] _testWordArray;
    17.     // bool to test
    18.     [SerializeField]
    19.     private bool _correct;
    20.     void Start()
    21.     {
    22.         // create one long string
    23.         _wordListFileWholeThing = _wordListTextFile.ToString();
    24.         // split the string into actual words by line break and add to list
    25.         _wordList.AddRange(_wordListFileWholeThing.Split("\n"[0]));
    26.  
    27.         //TEST WORD
    28.        // _wordList.Add("garbed");
    29.  
    30.         _testWordArray = new string[_wordList.Capacity];
    31.         for (int i = 0; i < _wordList.Capacity; i++)
    32.         {
    33.             string test = _wordList[i].ToString();
    34.  
    35.             _testWordArray[i] = test;
    36.         }
    37.  
    38.      
    39.  
    40.         Debug.Log(_wordList[0]);
    41.         Debug.Log(_wordList[1]);
    42.         Debug.Log(_wordList[2]);
    43.         Debug.Log(_wordList[3]);
    44.         Debug.Log(_wordList.Count);
    45.        
    46.     }
    47.     private void Update()
    48.     {
    49.         if (Input.GetKeyDown(KeyCode.L))
    50.         {
    51.             // test word checker
    52.             _correct = CheckWord(_wordList[12]);
    53.  
    54.         }
    55.         if (Input.GetKeyDown(KeyCode.K))
    56.         {
    57.             string testWord = "garbed";
    58.             testWord.Trim();
    59.             testWord.TrimEnd();
    60.             testWord.TrimStart();
    61.             testWord.ToLower();
    62.             // test word checker
    63.             _correct = CheckWordArray("BALE");
    64.         }
    65.     }
    66.     public bool CheckWordArray(string word)
    67.     {
    68.         // bool to check if word is found
    69.         bool wordFound = false;
    70.        
    71.        
    72.         Debug.Log("Word to look up +" + word + "+");
    73.      
    74.  
    75.         // loop through the list of words
    76.         for(int i = 0; i< _testWordArray.Length; i++)
    77.         {
    78.             Debug.Log("Test Word " + _testWordArray[i] + " = " + word);
    79.             // check to see if words equal
    80.             if(_testWordArray[i] == word)
    81.             {
    82.                 Debug.Log("word found in array");
    83.             }
    84.         }
    85.         {
    86.          
    87.         }
    88.         // after loop if word is found return true
    89.         if (wordFound == true)
    90.         {
    91.             return true;
    92.         }
    93.         // if word not found return false
    94.         else
    95.         {
    96.             return false;
    97.         }
    98.        
    99.     }
    100.  
     
  7. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,769
  8. paulbell1982

    paulbell1982

    Joined:
    Sep 28, 2018
    Posts:
    14
    Would this be better, or do I have to scrap it completely

    Code (csharp):
    1.  
    2. _wordList.AddRange(_wordListFileWholeThing.Split("\n\r"[0]));
    3.  
     
  9. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,769
    As this is engineering, you're welcome to try your own stuff out, let us know if it works. What you wrote above should not work any different, since you're just passing the 0th character in, and ignoring the 1th character.

    Alternatively if you just want to get this done and move forward, you might want to try one of the split options suggested in the stackoverflow post I linked above. :)
     
    paulbell1982 likes this.
  10. paulbell1982

    paulbell1982

    Joined:
    Sep 28, 2018
    Posts:
    14
    Thank you for your help. It is all sorted now, just a simple formatting error or the line space. In the end I just replaced the line space.