How to check user input for integer?

He tells me that he cannot convert int to bool. Tried TryParse, but for some reason the argument list is not valid.

code:

private void SetNumber(string n) { // if user input is a number then if (int.Parse(n)) { // if user input is negative if (h < 0) { // assign absolute version of user input number = Math.Abs(n); } else { // else assign user input number = n; } } else { number = 0; // if user input is not an int then set number to 0 } } 
+4
source share
10 answers

You probably used TryParse very closely, but I assume you forgot the out keyword on the parameter:

 int value; if (int.TryParse(n, out value)) { } 
+8
source

Just use this:

 int i; bool success = int.TryParse(n, out i); 

if the parsing was successful, success is true .

If this case i contains a number.

You probably had an out argument modifier before. It has an out modifier to indicate that it is a value that is initialized in the called method.

+5
source
  private void SetNumber(string n) { int nVal = 0; if (int.TryParse(n, out nVal)) { if (nVal < 0) number = Math.Abs(nVal); else number = nVal; } else number = 0; } 
+3
source

You can try with a simple simple expression:

  bool IsNumber(string text) { Regex regex = new Regex(@"^[-+]?[0-9]*\.?[0-9]+$"); return regex.IsMatch(text); } 
+2
source

You can try something like below using int.TryParse ..

  private void SetNumber(string n) { int parsed = -1; if (int.TryParse(n, out parsed)) //if user input is a number then ... 

The reason for complaints that it cannot convert int to bool is that the return type is int.Parse() is an int , not a bool , and in C # values ​​it is necessary to evaluate the bool values.

+1
source

int.Parse will return you an integer, not a boolean.

You can use int.TryParse as you suggested.

 int parsedValue; if(int.TryParse(n, out parsedValue)) { } 
+1
source

There are many problems in this code:

  • Using VB (') style string comments instead of C # braids
  • Parse for integer returns int, not bool
  • You must use a TryParse with a missing value.
  • h seems to be invalid. Is this type for n?
  • Are there variables that do not appear to be defined in the scope of functions (number), are they defined in the scope of classes?

But try the following:

 private void SetNumber(string n) { int myInt; if (int.TryParse(n, out myInt)) //if user input is a number then { if (myInt < 0) //if user input is negative number = Math.Abs(n); //assign absolute version of user input else //else assign user input number = n; } else number = 0; //if user input is not an int then set number to 0 } 
+1
source

int.Parse converts a string to an integer. The current one you have is in the if statement, so its handling of the return value is int.Parse like bool, that its not.

Something like this will work:

 private void SetNumber(string n) { int num = 0; try{ num = int.Parse(n); number = Math.Abs(num); }catch(Exception e){ number = 0; } } 
0
source

Well, firstly, the internal if statement has "h" instead of "n" if (h <0). But TryParse should work there, assuming that "number" is a class variable.

  private void SetNumber(string n) { int temp; bool success = Int32.TryParse(n, out temp); // If conversion successful if (success) { // If user input is negative if (temp < 0) number = Math.Abs(temp); // Assign absolute version of user input else // Assign user input number = temp; } else { number = 0; } } 
0
source
 // vinojash@gmail.com //In my knowledge i did this in simple way thanks for watch my code static void Main(string[] args) { string a, b; int f1, f2, x, y; Console.WriteLine("Enter two inputs"); a = Convert.ToString(Console.ReadLine()); b = Console.ReadLine(); f1 = find(a); f2 = find(b); if (f1 == 0 && f2 == 0) { x = Convert.ToInt32(a); y = Convert.ToInt32(b); Console.WriteLine("Two inputs r number \n so tha additon of these text box is= " + (x + y).ToString()); } else Console.WriteLine("One or tho inputs r string \n so tha concadination of these text box is = " + (a + b)); Console.ReadKey(); } static int find(string s) { string s1 = ""; int f; for (int i = 0; i < s.Length; i++) for (int j = 0; j <= 9; j++) { string c = j.ToString(); if (c[0] == s[i]) { s1 += c[0]; } } if (s==s1) f= 0; else f= 1; return f; } 
0
source

Source: https://habr.com/ru/post/1344815/


All Articles