Need help with int.TryParse()

  • (2 Pages)
  • +
  • 1
  • 2

26 Replies - 1410 Views - Last Post: 19 November 2015 - 01:06 PM Rate Topic: -----

#16 Hydroify   User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 127
  • Joined: 23-July 15

Re: Need help with int.TryParse()

Posted 19 November 2015 - 09:36 AM

Another quick question would it be best to use a If statement or a while loop?
Was This Post Helpful? 0
  • +
  • -

#17 andrewsw   User is offline

  • no more Mr Potato Head
  • member icon

Reputation: 6957
  • View blog
  • Posts: 28,696
  • Joined: 12-December 12

Re: Need help with int.TryParse()

Posted 19 November 2015 - 09:48 AM

They are not comparable. If is a single conditional statement, while is a loop structure.
Was This Post Helpful? 1
  • +
  • -

#18 Hydroify   User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 127
  • Joined: 23-July 15

Re: Need help with int.TryParse()

Posted 19 November 2015 - 09:56 AM

I know it is not the cleanest but this is what i got so far. But, it only puts the invalid data once then it goes through upon next line

        private static int Month()
        {
            int month = 0;
            string input = Console.ReadLine();
            bool isSuccess = int.TryParse(input, out month);

            if(isSuccess == false)
            {
                    Console.Write("Invalid value please try again >> ");
                    input = Console.ReadLine();
                   
            } 
            else
            {
                while (month < MIN_MONTH || month > MAX_MONTH)
                {
                    Console.Write("Invalid value. Please enter month again >> ");
                    input = Console.ReadLine();
                    
                }
            }

Was This Post Helpful? 0
  • +
  • -

#19 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 7915
  • View blog
  • Posts: 26,425
  • Joined: 05-May 12

Re: Need help with int.TryParse()

Posted 19 November 2015 - 10:13 AM

I suggest testing your code instead of having us debug by proxy. Just eyeballing that you've got issues with an infinite loop in one case, or unexpected results in another.
Was This Post Helpful? 1
  • +
  • -

#20 Hydroify   User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 127
  • Joined: 23-July 15

Re: Need help with int.TryParse()

Posted 19 November 2015 - 10:27 AM

I tried it usually skips over the while loop for the MIN_MONTH and MAX_MONTH
Was This Post Helpful? 0
  • +
  • -

#21 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 7915
  • View blog
  • Posts: 26,425
  • Joined: 05-May 12

Re: Need help with int.TryParse()

Posted 19 November 2015 - 10:30 AM

Try entering 26.
Was This Post Helpful? 1
  • +
  • -

#22 Hydroify   User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 127
  • Joined: 23-July 15

Re: Need help with int.TryParse()

Posted 19 November 2015 - 10:34 AM

it catches it and gives me the invalid data message but when i hit a it stuck in a infinite loop lol.
Was This Post Helpful? 0
  • +
  • -

#23 Hydroify   User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 127
  • Joined: 23-July 15

Re: Need help with int.TryParse()

Posted 19 November 2015 - 10:48 AM

Thanks everyone it works now everything i mean.
Was This Post Helpful? 0
  • +
  • -

#24 andrewsw   User is offline

  • no more Mr Potato Head
  • member icon

Reputation: 6957
  • View blog
  • Posts: 28,696
  • Joined: 12-December 12

Re: Need help with int.TryParse()

Posted 19 November 2015 - 11:00 AM

Good. You could post your final version for anyone interested.
Was This Post Helpful? 0
  • +
  • -

#25 Hydroify   User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 127
  • Joined: 23-July 15

Re: Need help with int.TryParse()

Posted 19 November 2015 - 11:12 AM

        private static int Month()
        {
            int month = 0;
            string input = Console.ReadLine();
       
            // If value is not numaric then it throws this error
            while(!int.TryParse(input, out month))
            {
                    Console.Write("Value is not numaric. Please enter month again >> ");
                    input = Console.ReadLine();
                   
            }

            // If value is lower than 1 or higher than 12 it throws this error
            while (month < MIN_MONTH || month > MAX_MONTH)
            {
                   Console.Write("Invalid value. Please enter month again >> ");
                   month = Convert.ToInt32(Console.ReadLine());
                   
            }
            return month;
        }


This does not work if you happen to type a number to high for instance '26' in first then a letter.

This post has been edited by Hydroify: 19 November 2015 - 01:07 PM

Was This Post Helpful? 0
  • +
  • -

#26 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 7915
  • View blog
  • Posts: 26,425
  • Joined: 05-May 12

Re: Need help with int.TryParse()

Posted 19 November 2015 - 12:38 PM

LOL! So what happens if the user first enters "26" and then later enters "I Love Hydroify" ?
Was This Post Helpful? 1
  • +
  • -

#27 Hydroify   User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 127
  • Joined: 23-July 15

Re: Need help with int.TryParse()

Posted 19 November 2015 - 01:06 PM

updated code works now


        private static int Month()
        {
            int month = 0;
            string userInput = Console.ReadLine();
            bool isSuccess = false;

            while (!isSuccess)
            {
                if (int.TryParse(userInput, out month))
                    // If value is lower than 1 or higher than 12 it throws this error
                    if(month < MIN_MONTH || month > MAX_MONTH)
                {
                    Console.Write("Invalid value. Please enter month again >> ");
                    userInput = Console.ReadLine(); 
                }
                else
                    isSuccess = true;
                else
                {
                    // If value is not numaric then it throws this error
                    Console.Write("Value is not numaric. Please enter month again >> ");
                    userInput = Console.ReadLine();
                }
            }
            return month;
        }


And Skydiver if '26' is entered it says Invalid value. Please enter month again. and if 'I Love Hydroify' is entered it says Value is not numeric. Please enter month again.
Was This Post Helpful? 1
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2