Question int.parse string Exception

Portimations

Member
Joined
Mar 7, 2022
Messages
5
Programming Experience
Beginner
I' fairly new to coding, and I've been in my C# class for a little bit over a month, and I though I would try and take a crack at making a game. Code pretty much goes as follows for TLDR:
Program.cs:
int ParVal = int.Parse(ParValInput);

Class.Method(ParVal);

Console.ReadLine(ParValInput);
Class.cs:
int ParVal1 = ParVal;

if (ParVal1 > 40)
{
    etc......
CODE POSTED BELOW:
Program.cs:
string HealthValInput = "";
string AttackValInput = "";
string StrengthValInput = "";
string SpeedValInput = "";
string DexValInput = "";
string IntelValInput = "";

int HealthVal = int.Parse(HealthValInput);      <--------["Exception Unhandled"]
int AttackVal = int.Parse(AttackValInput);
int StrengthVal = int.Parse(StrengthValInput);
int SpeedVal = int.Parse(SpeedValInput);
int DexVal = int.Parse(SpeedValInput);
int IntelVal = int.Parse(IntelValInput);

PlayerStats1.StatAssignment(HealthVal, AttackVal, StrengthVal, SpeedVal, DexVal, IntelVal);
Console.Write("Out 40 stat points, how many would you like to put into Health? Enter Health: ");
Console.SetCursorPosition(4, 21);
HealthValInput = Console.ReadLine();
ClearText();
Console.Write("Next...");
Console.ReadKey();
ClearText();
etc...
PlayerStats.cs:
public void StatAssignment(int Health, int Attack, int Strength, int Speed, int Dex, int Intel)
{
    int TotStats = 40;
    int Health1 = Health;

    if (Health1 > 40)
    {
        ClearText();
        Console.Write($"Please stay below {TotStats}. Enter stat for Health please: ");
        Health = int.Parse(Console.ReadLine());
    }
    else
    {
        ClearText();
        Console.Write($"Thank you, your Health was set to {Health1}");
    }

    int Attack1 = Attack;
    
    if (Attack1 > 40)
    {
        ClearText();
        Console.Write($"Please stay below {TotStats}. Enter stat for Health please: ");
        Attack = int.Parse(Console.ReadLine());
    }
    else
    {
 
Last edited by a moderator:
Please always format your code snippets for readability. The post editor has a tool bar for a reason. Unformatted code is much harder to read, especially because it has no indenting.
 
As for the issue, you haven't told us what the actual error message was, so we have to guess. It's probably a safe bet what is happening but it won't always be, so you need to provide ALL the relevant information. That would include the error message and what data was in use at the time. For you, that means what the result of Console.ReadLine() was. Did you even look to see for yourself? I'm guessing not. Presumably it is not a number, in which case it should be obvious why int.Parse won't work. If your file is supposed to have a specific format then you need to write code that conforms to that format and you should validate every input as you go if there is any chance that the file doesn't conform. You can call int.TryParse to validate and convert (if valid) in one go.
 
Please always format your code snippets for readability. The post editor has a tool bar for a reason. Unformatted code is much harder to read, especially because it has no indenting.
Understood, I didn't know that considering I just made an account 30 minutes ago, and made this post right after I did. Did you even look for yourself? Probably not. I will look into that moving forward.
 
As for the issue, you haven't told us what the actual error message was, so we have to guess. It's probably a safe bet what is happening but it won't always be, so you need to provide ALL the relevant information. That would include the error message and what data was in use at the time. For you, that means what the result of Console.ReadLine() was. Did you even look to see for yourself? I'm guessing not. Presumably it is not a number, in which case it should be obvious why int.Parse won't work. If your file is supposed to have a specific format then you need to write code that conforms to that format and you should validate every input as you go if there is any chance that the file doesn't conform. You can call int.TryParse to validate and convert (if valid) in one go.
I apologize for not providing all relevant information, I did include the only error message I was getting after it was crashing my code, I have it in quotes and a literal arrow pointing where the issue was. Funny enough, I DID look for myself, it could convert the string to an int, which is why I used int.parse. As for the formatting and int.TryParse, I didn't know about that as I'm fairly new to coding but I will look into it. I appreciate the passive aggressive response though.
 
I did include the only error message I was getting after it was crashing my code, I have it in quotes and a literal arrow pointing where the issue was.
Nope. That simply tells us that an exception was thrown. When an exception is thrown, that exception contains an error message and various other information. That information is a diagnostic aid, thus you should provide it to us if want us to diagnose the issue. The error message gives the reason for the exception, although there aren't many reasons that int.Parse could fail.
Funny enough, I DID look for myself, it could convert the string to an int, which is why I used int.parse.
Looking for yourself doesn't mean just looking at the file. That won't help you if the code you wrote is wrong for whatever reason. It means actually using the debugger to look at the data that your app is using as it's using it. If Console.ReadLine were actually the issue then that would mean looking at the data that was returned by the call to that method, which is what is being passed to int.Parse.

That said, it doesn't even look like that's an issue because you're not even getting that far. If the issue is here:
C#:
string HealthValInput = "";
string AttackValInput = "";
string StrengthValInput = "";
string SpeedValInput = "";
string DexValInput = "";
string IntelValInput = "";

int HealthVal = int.Parse(HealthValInput);
then what exactly are you expecting to happen? You assign an empty string to HealthValInput and then pass that to int.Parse. What are you expecting the result of that to be?
 
Nope. That simply tells us that an exception was thrown. When an exception is thrown, that exception contains an error message and various other information. That information is a diagnostic aid, thus you should provide it to us if want us to diagnose the issue. The error message gives the reason for the exception, although there aren't many reasons that int.Parse could fail.

Looking for yourself doesn't mean just looking at the file. That won't help you if the code you wrote is wrong for whatever reason. It means actually using the debugger to look at the data that your app is using as it's using it. If Console.ReadLine were actually the issue then that would mean looking at the data that was returned by the call to that method, which is what is being passed to int.Parse.

That said, it doesn't even look like that's an issue because you're not even getting that far. If the issue is here:
C#:
string HealthValInput = "";
string AttackValInput = "";
string StrengthValInput = "";
string SpeedValInput = "";
string DexValInput = "";
string IntelValInput = "";

int HealthVal = int.Parse(HealthValInput);
then what exactly are you expecting to happen? You assign an empty string to HealthValInput and then pass that to int.Parse. What are you expecting the result of that to be?
My understanding is that strings are passed through Readline(), so if I assign a string variable to whatever the user passes through Readline() (a number example "13"), then if I use int.Parse on the variable holding the string value "13" it would make it into an int value, 13, then pass that through the method parameters.

I also wanted somebody with 10+ years experience to help someone new to coding, and not be a dick about it. Like I said, I'm new to this so I'm asking for feedback, not a smartass response. This is my first impression of the coding community, I sure hope this isn't like the rest.

reevaluate how you respond to people on the internet, especially on here where new people will be posting issues all the time, even if they don't make sense for some reason. And as an "experienced" coder like yourself, and a staff member of this site (forum moderator) you should compose yourself better and not come across as you did.
 
Unfortunately, he's not being a dick in this case. Believe me. We've seen him when he is. He's just explaining things as he would treat someone interested in learning. You are being overly sensitive like when you thought I was treating you like a kid.
 
Unfortunately, he's not being a dick in this case. Believe me. We've seen him when he is. He's just explaining things as he would treat someone interested in learning.
That's a pretty weird way to treat someone or talk to someone who he's never met before. I'm not some kid asking a million questions. Whether that single question I asked seem dumb to someone who's been coding for a while, a simple explanation or even just "Hey, can you give us more information" would be fine.

He needs to work on his social communication skills. It's too easy for these kids now a days to sit in front of the computer screen and lose skills on how to talk to someone.
 
It's too easy for these kids now a days to sit in front of the computer screen and lose skills on how to talk to someone.
You just broke my irony meter. Complains about being treated like a kid one day, calls someone 52 years old who's been doing this the same way for about 17 years a kid the next.

In one extreme case, someone actually came back to a site years later and thanked me for what I said and how I said it because they took it to heart and it made them a better developer. I'm not changing the way I do things now. You can either take the point and make an effort to avoid the same mistakes in future or you can keep spending more time complaining than working on the code. It's obviously your choice.
 
As for the code, if you expect to convert user input into an int then you actually have to get the user input first and then convert it to an int. That's not what you're doing. Here are three lines from your code:
C#:
string HealthValInput = "";

// ...

int HealthVal = int.Parse(HealthValInput);      <--------["Exception Unhandled"]

// ...

HealthValInput = Console.ReadLine();
If the logic you're trying to implement is:
  1. Get input from user
  2. Convert input to number
do you see the issue with the code you wrote, which does the conversion before getting the input?

Programming doesn't exist in a vacuum. Code is simply an implementation of logic. If the logic says do A then B and the code does B then A, it's never going to work.
 
Programming doesn't exist in a vacuum. Code is simply an implementation of logic. If the logic says do A then B and the code does B then A, it's never going to work.
To be absolutely clear, the point of that was not to say "you're dumb for not doing it right" as I fear you may be inclined to think. Rather, it was to say that you should work to attain a clear understanding of the logic you're trying to implement first, then write code that explicitly implements that logic. If things don't work, going back through the code and confirming that it does indeed implement the intended logic is the very first step in fixing it. This is why programming courses often teach people to write out their logic as a formal algorithm first, then reimplement it in pseudocode and, finally, reimplement that in actual code. If you do that then you're far less likely to do something like reverse the order of the required steps. Of course, most people ignore that advice because they just want to do the sexy part, i.e. writing the code, but then they expect others to explain where they went wrong when they would see for themselves if they did it the "proper" way. I can't say that I wasn't guilty of a bit of that myself when starting out. When it was pointed out to me though, my reaction was not tell that person that there was an issue with their communication.
 
Back
Top Bottom