7 Replies - 734 Views - Last Post: 09 November 2015 - 07:19 PM Rate Topic: -----

#1 mohan93  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 31
  • Joined: 24-May 14

int.Try parse not working

Posted 06 November 2015 - 06:40 AM

Hi Guys,

Am trying to check my input to be ONLY "numbers". using int.Try parse but not working.

in the line: bool isNumeric = int.TryParse(salary, out n);



class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Please enter salary");
            int salary = Convert.ToInt32(Console.ReadLine());

            int n;
            bool isNumeric = int.TryParse(salary, out n);
        
            int Output = UserCode.Bonus(salary);

            Console.WriteLine("Output is: {0}", Output);
            Console.ReadLine();
        }

        class UserCode
        {
            public static int Bonus(int salary)
            {
                int b = 0;
                if (salary < 0)
                {
                    b = -1;
                }
                else
                {
                    if (salary > 1000000)
                    {
                        b = -2;
                    }
                    else if (salary > 15000 && salary < 20001)
                    {
                        b = 1500 + ((salary * 17) / 100);
                    }
                }

                return b;
            }

This post has been edited by mohan93: 06 November 2015 - 06:43 AM


Is This A Good Question/Topic? 0
  • +

Replies To: int.Try parse not working

#2 andrewsw  Icon User is online

  • the case is sol-ved
  • member icon

Reputation: 6380
  • View blog
  • Posts: 25,775
  • Joined: 12-December 12

Re: int.Try parse not working

Posted 06 November 2015 - 06:49 AM

Not working in what way? If there is an error then post the error details.

What value are you inputting? It seems likely that the error would occur on line 6 with the Convert attempt.
Was This Post Helpful? 0
  • +
  • -

#3 mohan93  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 31
  • Joined: 24-May 14

Re: int.Try parse not working

Posted 06 November 2015 - 06:53 AM

Thanks for replying.

I dont have any error with line 6. Only error is am trying to check my int value is numbers or not, it should not be alphabets.

int n;
bool isNumeric = int.TryParse(salary, out n);

The best overloaded method match for 'int.TryParse(string, out int)' has some invalid arguments
Was This Post Helpful? 0
  • +
  • -

#4 andrewsw  Icon User is online

  • the case is sol-ved
  • member icon

Reputation: 6380
  • View blog
  • Posts: 25,775
  • Joined: 12-December 12

Re: int.Try parse not working

Posted 06 November 2015 - 06:54 AM

Quote

The best overloaded method match for 'int.TryParse(string, out int)' has some invalid arguments

What do you think this means?

salary is an integer. The first argument for TryParse should be a string.
(It doesn't make sense to pass an integer to int.TryParse, it will always succeed.)

This post has been edited by andrewsw: 06 November 2015 - 06:55 AM

Was This Post Helpful? 1
  • +
  • -

#5 mohan93  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 31
  • Joined: 24-May 14

Re: int.Try parse not working

Posted 06 November 2015 - 07:04 AM

Sorry about that, What is the alternate method to check for the int values to be only numbers.

If i make salary as String, i face issues with method below (with arithmetic operations)

Please suggest.
Was This Post Helpful? 0
  • +
  • -

#6 andrewsw  Icon User is online

  • the case is sol-ved
  • member icon

Reputation: 6380
  • View blog
  • Posts: 25,775
  • Joined: 12-December 12

Re: int.Try parse not working

Posted 06 November 2015 - 07:07 AM

You are using both Convert.ToInt32 and int.TryParse. You only need to use one, and TryParse is preferable. Convert will raise an Exception that will need to be handled.

This post has been edited by andrewsw: 06 November 2015 - 07:08 AM

Was This Post Helpful? 0
  • +
  • -

#7 xnn  Icon User is offline

  • D.I.C Head

Reputation: 37
  • View blog
  • Posts: 238
  • Joined: 10-February 10

Re: int.Try parse not working

Posted 06 November 2015 - 12:35 PM

Snippet to read input from console and process if numeric.

   Int32 numericInput;
   Console.Write("Please enter a number: ");  
  
   if (Int32.TryParse(Console.ReadLine(), out numericInput)
   {
      //Input was numeric - Do normal processing using numericInput variable to retrieve number entered by user here.
   }
   else
   {
      //Input was not numeric display message
      Console.WriteLine("You did not enter a number.");
   }   



Links to MSDN for usage of Int32.TryParse and loops. A way to enhance this code would be to use a loop so that the user can re-enter the number without having to relaunch the program. See the link on While loops for help on achieving that.

MSDN - Int32.TryParse
MSDN - While Loop
Was This Post Helpful? 0
  • +
  • -

#8 zerocool1  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 10
  • View blog
  • Posts: 54
  • Joined: 31-August 15

Re: int.Try parse not working

Posted 09 November 2015 - 07:19 PM

Here is a basic example of using try parse. This is probably a little better for error handling.

    public class Program
    {
        public static void Main(string[] args)
        {
            
            string s = "10"; //Try putting different ints and strings in and see the different results
            int i = 0; 
            
           bool isSuccess = Int32.TryParse(s, out i);
            
            if(isSuccess)
                
            {
             
                Console.WriteLine("Successfull Conversion");
                Console.WriteLine(i); // Do whatever when succeeds
            }
            
            else 
            {
                Console.WriteLine("Failed Conversion"); // Do whatever when fails 
            }
        }
    }


}
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1