-1

i have a small problem with my text-based RPG in c# console.

I have made a method to save and that works but when i want to load it gives me an error..

here is my code for loading :

(the strings don't give a error but the problems start from Level to Agility)

code :

public static void LoadData ()
{
        // create reader & open file
        TextReader tr = new StreamReader("SavedGame.txt");

        // read lines of text
        string xCoordString = tr.ReadLine();
        string yCoordInt= tr.ReadLine();

        //Convert the strings to int
        Name = Convert.ToString(xCoordString);
        PlayerType = Convert.ToString (xCoordString);
        Level = Convert.ToString(xCoordString);
        HP = Convert.ToInt32(yCoordInt);
        Strenght = Convert.ToInt32(yCoordInt);
        Intelligence = Convert.ToInt32(yCoordInt);
        Agility = Convert.ToInt32(yCoordInt);
        // close the stream
        tr.Close();
}
John Saunders
  • 160,644
  • 26
  • 247
  • 397
  • 1
    What is the error message? You may need to step through your code and see the value of each variable you were trying to convert – codingbiz Nov 14 '13 at 19:42
  • Level = Convert.ToInt32 btw instead of ToString – user2936661 Nov 14 '13 at 19:43
  • and error was : System.FormatException: Input string was not in the correct format at System.Int32.Parse (System.String s) [0x00000] in :0 at System.Convert.ToInt32 (System.String value) [0x00000] in :0 at practiceCSHARP.MainClass.LoadData () [0x0002f] in /home/stefano/Projects/practiceCSHARP/practiceCSHARP/Main.cs:396 at practiceCSHARP.MainClass.Main (System.String[] args) [0x002eb] in /home/stefano/Projects/practiceCSHARP/practiceCSHARP/Main.cs:109 – user2936661 Nov 14 '13 at 19:44
  • Can you show what your text file looks like? – Brandon Nov 14 '13 at 19:45
  • what is put into level is the int number 3 – user2936661 Nov 14 '13 at 19:45
  • Stefano Knight 1 100 3 3 3 is the text file – user2936661 Nov 14 '13 at 19:46
  • no each line seperate Brandon – user2936661 Nov 14 '13 at 19:48
  • The code makes no sense. I don't see where you loop through the lines? you are converting the same value (`xCoordString`) into several different variables: `Name`, `PlayerType`, etc. Am I missing somthing? – Floremin Nov 14 '13 at 19:51
  • It looks like you are only calling read line twice, if each value is on a separate line you would need to loop through to get all the values. As your code stands xCoordString = Stefano, yCoordInt = Knight – Brandon Nov 14 '13 at 19:51
  • so how could i fix this?? – user2936661 Nov 14 '13 at 19:53
  • Full saving and Loading methods (SAVING WORKS!) – user2936661 Nov 14 '13 at 19:54
  • public static void SaveData () { TextWriter tw = new StreamWriter("SavedGame.txt"); // write lines of text to the file tw.WriteLine (Name); tw.WriteLine (PlayerType); tw.WriteLine(Level); tw.WriteLine(HP); tw.WriteLine (Strenght); tw.WriteLine(Intelligence); tw.WriteLine (Agility); // close the stream tw.Close(); } – user2936661 Nov 14 '13 at 19:54
  • // create reader & open file TextReader tr = new StreamReader("SavedGame.txt"); // read lines of text string xCoordString = tr.ReadLine(); string yCoordInt= tr.ReadLine(); //Convert the strings to int Name = Convert.ToString(xCoordString); PlayerType = Convert.ToString (xCoordString); Level = Convert.ToInt32(xCoordString); HP = Convert.ToInt32(yCoordInt); Strenght = Convert.ToInt32(yCoordInt); Intelligence = Convert.ToInt32(yCoordInt); Agility = Convert.ToInt32(yCoordInt); // close the stream tr.Close(); } – user2936661 Nov 14 '13 at 19:55

5 Answers5

0

Whatever is being read into your variables is not convertible to an int. You may have to strip newline/carriage return characters, or the data just plain isn't numeric. You should test it and/or sanitize it before you try to convert it.

Crowcoder
  • 11,250
  • 3
  • 36
  • 45
0

I dont know what are you doing

           string xCoordString = tr.ReadLine();
            //Convert the strings to int
           Name = Convert.ToString(xCoordString);

Why you string convert to string ???

So You read string , then you should split it

           string[] s = xCoordString .Split(' ');

then

           var firstVariable=s[0];
           var secondVariable=s[1];

and so on. This should help You C# Splitting Strings?

And one more: use

        int.Parse(string value)  

Hopefully my answer will helps You. Good luck !

Community
  • 1
  • 1
Oleh
  • 447
  • 1
  • 11
  • 30
0

You might want to use TryParse(String, Int32) to check if a parse succeeded, and perform some action (like setting a default value, or notifying the user) if it doesn't succeed.

You can clean the input string then parse it:

private static int ParseNumber(string input)
{
             string cleanedInput = input.Where(c => char.IsDigit(c)).ToString();
             int result;
             if (!Int32.TryParse(cleanedInput, out result))
             {
                Console.WriteLine("An error occured..");
             }
     return result;

}

and just use Agility = ParseNumber(input).

Ethan
  • 85
  • 1
  • 8
0

You're not parsing your save file correctly. If this is the format of your save file:

Stefano 
Knight 
1 
100 
3 
3 
3

then you will need to iteratively read each line and parse the value read into your variables, as such:

string line = string.Empty;

//Convert the strings to int
line = tr.ReadLine();
Name = line;
line = tr.ReadLine();
PlayerType = line;
line = tr.ReadLine();
Level = Convert.ToInt32(line);
line = tr.ReadLine();
HP = Convert.ToInt32(line);
line = tr.ReadLine();
Strenght = Convert.ToInt32(line);
line = tr.ReadLine();
Intelligence = Convert.ToInt32(line);
line = tr.ReadLine();
Agility = Convert.ToInt32(line);

Of course there's better ways to manage your save-file data, but this should show you why your parsing didn't work.

DougEC
  • 357
  • 1
  • 6
0

You can loop through the lines like so, however you would need to determine which attribute it was and treat it accordingly.

 TextReader tr = new StreamReader("SavedGame.txt");


 string charInfo;
 while ((charInfo = tr.ReadLine()) != null)
 {
     //parse the line and put into appropriate variable.
 }
Brandon
  • 339
  • 3
  • 11