Home > Software design >  Compare the text array and the input array
Compare the text array and the input array

Time:04-03

I have an array of text and an array of fields, I need to implement so that when a user enters a value in a field, the value is compared with the text for this field. I.e., for example, the number "12" is written in one text field, and the user entered "13" and "Not correct" was output to the log, and there are 6 such fields and text. (this is the solution of the number 6 6=12) Please tell me how to do it better, I started doing this:

    public InputField[] Inputfield1;
    public Text[] text1;

    public void CurBtn()
    {
  foreach (InputField inputField in Inputfield1)
            {
                for (int i = 0; i < text1.Length; i  )
                {
                    for (int j = 0; j < Input_Numer_1.Length; j  )
                    {
        
                        if (text1[i].ToString() == Inputfield1[j].text)
                        {
                             Debug.LogFormat($"OK");
                        }
else{
 Debug.LogFormat($"Not correct");
                    }
                }
            }
        }

CodePudding user response:

Text is a UnityEngine.Object and Object.ToString basically returns the GameObjects name like Text.name.

You rather would want it's UI.Text.text

if(text1[i].text == Inputfield1[j].text)

However, why not rather actually store int values and use int.TryParse to convert your user input to a numeric values and rather compare int based which is way more efficient.


You also have one loop to much there .. currently you iterate the inputs twice.

To me it sounds like you don't even want to compare each input to each text but rather have certain pairs of both you want to compare like

[Serializable]
public class CheckPair
{
    public InputField Input;
    public Text Text;

    public bool Compare()
    {
        var correct = Text.text == Input.text;

        Debug.Log($"{correct ? "" : "NOT "} correct", Input);

        return correct;
    }
}

And in the Inspector rather assign these in a single

public CheckPair pairs;

and then

public void CurBtn()
{
    foreach (var pair in pairs)
    {
        pair.Compare();
    }
}

or if you e.g. want to know if all are correct

public void CurBtn()
{
    foreach (var pair in pairs)
    {
        if(!pair.Compare())
        {
            Debug.Log($"{pair.Input} is not correct!");
            return;
        }
    }

    Debug.Log("All are correct!");
}

CodePudding user response:

You can simplify your code by using Array.IndexOf to make it easier to compare values. If your Text[] and InputField[] arrays both contain the same types (both contain only ints or only strings) you can remove the .ToString()

        public static InputField[] Inputfield1 = { 3, 4 };
        public static Text[] text1 = { 6, 4 };
        static void CurBtn()
        {
            foreach (int inputField in Inputfield1)
            {
                if (inputField.ToString() == text1[Array.IndexOf(Inputfield1, inputField)].ToString())
                {
                    Console.WriteLine("Ok");
                }
                else
                {
                    Console.WriteLine("Incorrect");
                }
            }
        }
  • Related