none
Converting string to int RRS feed

  • Question

  •  

    Hi guys, I'm still learning the basics. 

    I wrote a program not too long ago for strings that would get 2 values for 2 variables using one local variable. 

    I tried to use this method with integers but I'm finding it difficult to understand how to convert the string to an int.

    Any help is appreciated,

    thank you!

    using System;
    
    namespace Test_Environment
    {
        class Program
        {
            static void Main(string[] args)
            {
                int a, b;
                a = getInt("number (1)");
                b = getInt("number (2)");
                
            }//end main
    
    
            static int getInt(int number)
            {
                Console.Write("Enter a whole" + number + " : ");
                return Console.Read();
            }
    
        }//end class
    }//end namespace


    Sunday, November 3, 2019 1:21 PM

Answers

  • Some changes to your getInt:

            static int getInt()
            {
                Console.Write("Enter a whole number: ");
                string s = Console.ReadLine();
                return int.Parse(s);
            }

    Note the call to int.Parse that converts a string into an int.

    Also, you don't have to pass an int into the function. Instead the int is returned out of the function (that's why the declaration of the function starts with "int").

    If you want to pass a text for the prompt, you would pass it as string, not as an int. The way it is currently written, just call it as a = getInt();

    There are other alternatives instead of int.Parse, such as Convert.ToInt32. But both of them will throw an error if the user types something that is not an integer. You can avoid that bu using TryParse. Look it up in the manual, because it is a bit more complex to use (it returns an out parameter).


    Sunday, November 3, 2019 2:28 PM
    Moderator

All replies

  • Some changes to your getInt:

            static int getInt()
            {
                Console.Write("Enter a whole number: ");
                string s = Console.ReadLine();
                return int.Parse(s);
            }

    Note the call to int.Parse that converts a string into an int.

    Also, you don't have to pass an int into the function. Instead the int is returned out of the function (that's why the declaration of the function starts with "int").

    If you want to pass a text for the prompt, you would pass it as string, not as an int. The way it is currently written, just call it as a = getInt();

    There are other alternatives instead of int.Parse, such as Convert.ToInt32. But both of them will throw an error if the user types something that is not an integer. You can avoid that bu using TryParse. Look it up in the manual, because it is a bit more complex to use (it returns an out parameter).


    Sunday, November 3, 2019 2:28 PM
    Moderator
  • Here is a hard coded example bypassing user input

    First try to convert a string with chars and numbers, second, input has only a number

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace ConsoleApp1
    {
        class Program
        {
            static void Main(string[] args)
            {
                string someValue = "number (1)";
                string testValue = System.Text.RegularExpressions
                    .Regex.Replace(someValue, "[^0-9]", "");
    
                Console.WriteLine(int.TryParse(testValue, out var result) ? 
                    $"Number is {result}" : 
                    "No valid number");
    
    
    
                someValue = "11";
                Console.WriteLine(int.TryParse(someValue, out var result1) ? 
                    $"Int value is {result1}" : 
                    "No value");
    
                Console.ReadLine();
    
            }
        }
    }
    


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Sunday, November 3, 2019 2:37 PM
    Moderator
  • Some changes to your getInt:

            static int getInt()
            {
                Console.Write("Enter a whole number: ");
                string s = Console.ReadLine();
                return int.Parse(s);
            }

    Note the call to int.Parse that converts a string into an int.

    Also, you don't have to pass an int into the function. Instead the int is returned out of the function (that's why the declaration of the function starts with "int").

    If you want to pass a text for the prompt, you would pass it as string, not as an int. The way it is currently written, just call it as a = getInt();

    There are other alternatives instead of int.Parse, such as Convert.ToInt32. But both of them will throw an error if the user types something that is not an integer. You can avoid that bu using TryParse. Look it up in the manual, because it is a bit more complex to use (it returns an out parameter).


    Thanks for your help Alberto. 

    I've made the adjustments and it runs now. 

    My code was based off the code given to us by the lecturer for strings, so I was just trying to adapt it. From what I could tell on the strings example, "number" was being passed in, and then used as a placeholder, so that the two variables would input text "first" "second" accordingly. 

    Anyway, thanks again. 
    Sunday, November 3, 2019 3:18 PM