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

Cannot convert string to char

Discussion in 'Scripting' started by Mega_Jimmy, Sep 19, 2022.

  1. Mega_Jimmy

    Mega_Jimmy

    Joined:
    May 12, 2015
    Posts:
    9
    I've had a look round but nothing i see seems to answer. Here's my code to start with;

    Code (CSharp):
    1.     void readDictionary()
    2.     {
    3.         string[] data = wordData.text.Split(new string("," , "\n"), StringSplitOptions.None);
    4.  
    5.         int tableSize = data.Length / 3 - 1;
    6.         MyWordList.words = new Words[tableSize];
    7.  
    8.         for(int i = 0; i < tableSize; i++)
    9.         {
    10.             MyWordList.words[i] = new Words();
    11.             MyWordList.words[i].word = data[3 * (i + 1)];
    12.             MyWordList.words[i].assignment = data[3 * (i + 1) + 2];
    13.             MyWordList.words[i].meaning = data[3 * (i + 1) + 3];
    14.         }
    15.     }
    So the idea is that I'm importing a spreadsheet which is three columns wide. This is meant to be like a dictionary of sorts (So there's the word itself, a letter assignment for when I call the word into the game and a short definition which comes into player later) but for the life of me I can't figure this out.
    The three are stored as strings but when I use the Split code above, it comes back saying 'cannont convert string to char'
    I know I need a char or something in the Split code but apparently it won't take two char's.

    Anything anyone can suggest to help would be appreciated.

    For the curious I was following this tutorial;
     

    Attached Files:

  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,748
    That function takes a single character (char data type), but if you use "double quotes" (even around a single letter) it sends a string. Use single quotes ',' instead.
     
  3. Mega_Jimmy

    Mega_Jimmy

    Joined:
    May 12, 2015
    Posts:
    9
    How do I get it to recognise I want it to split via 'new line'? It won't take /n.
     
  4. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,385
  5. Mega_Jimmy

    Mega_Jimmy

    Joined:
    May 12, 2015
    Posts:
    9
    To be clear I've tried

    Code (CSharp):
    1. string[] data = wordData.text.Split(',', '\n');
    But that doesn't work as it does this;

    upload_2022-9-19_18-10-12.png

    I'm trying to get it so the Word is one word, the Assignment is one letter and the meaning is a string of X length
     
  6. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,385
    What do you mean?

    What is your input data? Where does 'wordData' come from and how is it formatted?
     
  7. Mega_Jimmy

    Mega_Jimmy

    Joined:
    May 12, 2015
    Posts:
    9
    I'll keep it simple and show the whole code.
    I'm puling wordData from a CSV file where I have a list of words stored. it's a spreadsheet with three columns.

    Code (CSharp):
    1. using UnityEngine;
    2. using System;
    3. using System.Collections;
    4. using System.Collections.Generic;
    5.  
    6. public class Dictionary : MonoBehaviour
    7. {
    8.     public TextAsset wordData;
    9.  
    10.     [System.Serializable]
    11.  
    12.     public class Words
    13.     {
    14.         public string word;
    15.         public string assignment;
    16.         public string meaning;
    17.     }
    18.  
    19.     [System.Serializable]
    20.  
    21.     public class WordList
    22.     {
    23.         public Words[] words;
    24.     }
    25.  
    26.     public WordList MyWordList = new WordList();
    27.  
    28.     // Use this for initialization
    29.     void Start()
    30.     {
    31.         readDictionary();
    32.     }
    33.     void readDictionary()
    34.     {
    35.         string[] data = wordData.text.Split;
    36.  
    37.         int tableSize = data.Length / 3 - 1;
    38.         MyWordList.words = new Words[tableSize];
    39.  
    40.         for(int i = 0; i < tableSize; i++)
    41.         {
    42.             MyWordList.words[i] = new Words();
    43.             MyWordList.words[i].word = data[3 * (i + 1)];
    44.             MyWordList.words[i].assignment = data[3 * (i + 1) + 2];
    45.             MyWordList.words[i].meaning = data[3 * (i + 1) + 3];
    46.         }
    47.     }
    48.  
    49. }
     
  8. Mega_Jimmy

    Mega_Jimmy

    Joined:
    May 12, 2015
    Posts:
    9
    Ok so I figured it out
    Broke it down a bit and realised I could just do it this way;

    Code (CSharp):
    1. string[] delimiterChars =
    2.         {
    3.             "," ,
    4.             "\r\n"
    5.         };
    6.  
    7.         string[] data = wordData.text.Split(delimiterChars, StringSplitOptions.None);
    Hopefully this will help you too.
     
  9. Nad_B

    Nad_B

    Joined:
    Aug 1, 2021
    Posts:
    377
    The method you're using is a little bit overcomplicated to be honest. I would just use a StringReader to read the text line by line, then process each line individually by splitting it, something like this:

    Code (CSharp):
    1. using (var reader = new StringReader(wordData.text))
    2. {
    3.   string line = null;
    4.  
    5.   while ((line = reader.ReadLine()) != null) // Will read the text line by line
    6.   {
    7.     var splittedLine = line.Split(' ');
    8.     var words = new Words { word = splittedLine[0], assignment = splittedLine[1], meaning = splittedLine[2] };
    9.     // do whatever you want with your "words" class...
    10.   }
    11.  }
     
    Bunny83 and lordofduct like this.
  10. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    3,572
    Beside all the noise about char and string conversion, you do realise that this does not make much sense, right?

    Code (CSharp):
    1.             MyWordList.words[i].word = data[3 * (i + 1)];
    2.             MyWordList.words[i].assignment = data[3 * (i + 1) + 2];
    3.             MyWordList.words[i].meaning = data[3 * (i + 1) + 3];
    You said that you have 3 columns and you do multiply by 3. However you grab the "word" from offset 0, the "assignment" from offset 2 and the "meaning" from offset 3 which would imply a 4 column layout where the second column is ignored all the time. If you actually have 3 columns, you would always grab the wrong assignment and meaning. Your meaning is always the "word" of the next line.
    3 * (i+1)+3
    is the same as
    3*(i+2)
    .

    Like Nad_B said, you should properly parse the file to begin with. If any line does not have exactly 3 columns you would get completely out of sync. So if one line is missing the meaning or if there's another 4th column, everything would break down. That's not really a robust way of parsing one of the simplest data formats out there :)
     
  11. Mega_Jimmy

    Mega_Jimmy

    Joined:
    May 12, 2015
    Posts:
    9
    Shortly after changing my code like in my last post, I realised the same thing so I changed it and it works the way I want to. I didn’t mention it in my reply because I realised the offset was wrong after I hit reply. Didn’t think to mention it.
    Like I said I was following a tutorial and the person explaining it was using the offset to avoid taking the top row which is the header. His worked in the video which is why I was confused as to why mine didn’t work.