Click here to Skip to main content
15,881,248 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have several text boxes which will have a String input.
I need to convert all of the text from all of the text boxes to int format.

I have created a function convertToInt() however I am unsure how the function would be created.

I have the following code:

MIDL
week.addDay(1, (convertToInt(txtHrsMon.getText())), (convertToInt(txtMinsMon.getText())));
        week.addDay(2, (convertToInt(txtHrsTues.getText())), (convertToInt(txtMinsTues.getText())));
        week.addDay(3, (convertToInt(txtHrsWeds.getText())), (convertToInt(txtMinsWeds.getText())));
        week.addDay(4, (convertToInt(txtHrsThurs.getText())), (convertToInt(txtMinsThurs.getText())));
        week.addDay(5, (convertToInt(txtHrsFri.getText())), (convertToInt(txtMinsFri.getText())));
Posted
Updated 16-Jan-11 21:49pm
v2
Comments
Dalek Dave 17-Jan-11 3:50am    
Edited for Readability.

You can use Integer.parseInt[^]. :)
 
Share this answer
 
Comments
Dalek Dave 17-Jan-11 3:50am    
Good Call.
Nuri Ismail 17-Jan-11 4:11am    
Thank you DD!
Blesson Mathew 17-Jan-11 4:33am    
Gud Link..So i voted 5
Nuri Ismail 17-Jan-11 4:37am    
Thanks Blesson.
just a look on the below link


http://www.devx.com/tips/Tip/14662[^]
 
Share this answer
 
v2
Comments
Nuri Ismail 17-Jan-11 4:06am    
Blesson, the question is about Java (see the tags). I don't think this C# example will help the OP.
Blesson Mathew 17-Jan-11 4:29am    
i am sorry..nw i changed the link..
Nuri Ismail 17-Jan-11 4:30am    
This is one is better! :)
Blesson Mathew 17-Jan-11 4:39am    
Thanks Nuri...
C#
static void Main(string[] args)
       {
           string ipValue = Console.ReadLine();
           int nmbr = 0;
           if (!string.IsNullOrEmpty(ipValue))
               nmbr = ConvertToInteger(ipValue);
           Console.WriteLine(nmbr);
           Console.ReadLine();

       }

       private static int ConvertToInteger(string p_ipValue)
       {
           int l_value = 0;
           string l_valueStr = string.Empty;
           try
           {
               foreach (var item in p_ipValue)
               {

                   try
                   {
                       if (p_ipValue[0] == item && (p_ipValue.StartsWith("-") || p_ipValue.StartsWith("+")))
                           l_valueStr = item.ToString();
                       else
                           l_valueStr += int.Parse(item.ToString()).ToString();
                   }
                   catch
                   {
                       if (p_ipValue.Length > 1 && p_ipValue[1] == item)
                       {
                           if (l_valueStr.StartsWith("-") || l_valueStr.StartsWith("+"))
                               l_valueStr = "0";
                       }
                       else if (p_ipValue[0] == item)
                           l_valueStr = "0";

                       break;
                   }
               }
           }
           catch
           {
               return 0;
           }
           return l_value = int.Parse(l_valueStr);
       }
 
Share this answer
 
Use Integer.ParseInt("Your String")
and use it inside a try - catch block.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900