-2

I use Visual Studio 8.7.9, Unity 2019.3.13f1

When I try to convert string[] to int[],

string score = reader.ReadToEnd();
string[] scores = score.Split('\n');
int[] scoreSorter = new int[scores.Length + 1];
for(int i =0; i <scores.Length; i++)
{
    scoreSorter[i] = Convert.ToInt32(scores[i]);
    
    foreach(var s in scoreSorter)
    {
        Array.Sort(scoreSorter);
        Debug.Log(s);
        highScore.text = "Рекорды: \n" + scoreSorter[0] + "\n" + scoreSorter[1] + "\n" + scoreSorter[2] + "\n" + scoreSorter[3];
    }
    
}

I get this error in my unity console:

FormatException: Input string was not in a correct format. System.Number.StringToNumber (System.String str, System.Globalization.NumberStyles options, System.Number+NumberBuffer& number, System.Globalization.NumberFormatInfo info, System.Boolean parseDecimal) (at <437ba245d8404784b9fbab9b439ac908>:0) System.Number.ParseInt32 (System.String s, System.Globalization.NumberStyles style, System.Globalization.NumberFormatInfo info) (at <437ba245d8404784b9fbab9b439ac908>:0) System.Int32.Parse (System.String s, System.IFormatProvider provider) (at <437ba245d8404784b9fbab9b439ac908>:0) System.Convert.ToInt32 (System.String value) (at <437ba245d8404784b9fbab9b439ac908>:0) ButtonAction.ButtonClickHighScore () (at Assets/Scripts/UI/ButtonAction.cs:73)

My aim is to get each number in array, sort them and display on the scoreboard. Any help will be appreciated!

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
Tim Markin
  • 21
  • 5
  • 1
    The error happens when some input value cannot be converted to an integer in the call to Convert.ToInt32. I suggest to add a _Debug.Log(scores[i])_ just before the convert line. So you will know which text is causing problems. – Steve Nov 16 '20 at 14:09
  • Please log elements from scores array or use debugger to understand which values it contains. – Dmitry Grebennikov Nov 16 '20 at 14:13
  • Perhaps try TryParse instead, use the debugger to step through the problem, and finally: Try using .Trim() to ensure there are no leading or trailing `" "`. – Austin T French Nov 16 '20 at 14:13
  • 2
    Instead of `\n` try `Environment.NewLine` or try to split on all newline formats `string[] lines = theText.Split( new[] { "\r\n", "\r", "\n" }, StringSplitOptions.None );` - see here: https://stackoverflow.com/questions/1547476/easiest-way-to-split-a-string-on-newlines-in-net – Rand Random Nov 16 '20 at 14:13
  • https://ericlippert.com/2014/03/05/how-to-debug-small-programs/ – SomeBody Nov 16 '20 at 14:26

2 Answers2

1

You probably have a newline character at the end of the input.

Change this line:

string[] scores = score.Split('\n');

To this:

string[] scores = score.Split(new char[] {'\n'}, StringSplitOptions.RemoveEmptyEntries);
Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
JesseChunn
  • 555
  • 3
  • 7
0

One of the strings was not a valid integer. Find out what string it is and fix it.

Strikegently
  • 2,251
  • 20
  • 23
  • I use .txt file to save data. Here is the example of when it gives me error. When I fill it with different numbers, the same mistake. https://drive.google.com/file/d/1Q6xmcMlJNBpO2ukZTkJyjoINzyMqITp5/view?usp=sharing – Tim Markin Nov 16 '20 at 14:07