Posts Tagged ‘int’

Convert String to Int in Java

Converting a string variable to an integer variable in Java is easy. There are actually two different approaches, both equally simply. Below is an example of both approaches, with both use the same string variable to output to various integer variables.

String myVariable = “12345″;

Approach 1:
int approach1 = Integer.parseInt(myVariable);

Approach 2:
int approach2 = Integer.valueOf(myVariable);

If you have any opinion on why you use one approach over the other, let the community know!

Switch Statement in C#

When possible, a programmer should choose to use a switch statement over an if statement. Note that it is not always possible to replace an if statement with a switch statement. The right time to use a switch statement is when the if statement is constantly comparing the same variable. In our example, we are constantly comparing the text in the Label control lblDayOfWeek. To make it easy, I have made blue the string text that will be used in our comparison.

switch (lblDayOfWeek.Text)
{
   case "1":
      lblDayOfWeek.Text = "Sunday";
      break;
   case "2":
      lblDayOfWeek.Text = "Monday";
      break;
   case "3":
      lblDayOfWeek.Text = "Tuesday";
      break;
   case "4":
      lblDayOfWeek.Text = "Wednesday";
      break;
   case "5":
      lblDayOfWeek.Text = "Thursday";
      break;
   case "6":
      lblDayOfWeek.Text = "Friday";
      break;
   case "7":
      lblDayOfWeek.Text = "Saturday";
      break;
   default:
      lblDayOfWeek.Text = "";
      break;
}

The final default is equivalent to else, which means if none of the cases match, the default route will be taken. The greatest advantage to using the switch statement is efficiency. When a swtich statement is called, only one comparison is made.

Note that the switch statement is not restricted to string or integer. It is also possible to use enum or virtually any other comparable variable.

How to Convert a String of Integers to an Int in C#

Though very simple once you see the example, it may not be obvious right off the bat. Note that a simple cast from a string to int will not convert a string of integers to an int!

string stringVariable = "1234";
int integerVariable = (int)stringVariable;

Therefore, to accomplish converting from a string to int, following the sample code below:

string stringVariable = "1234";
int integerVariable = Convert.ToInt32(stringVariable);

The Convert class is part of the System namespace, which is included in nearly every project by default. If for any reason your string of integers contains invalid characters, the function ToInt32 will throw a FormatException.

Another, though less popular way is to convert a string of integers to an int variable is to use the function Parse in the Int32 class. An example is below:

string stringVariable = "1234";
int integerVariable = Int32.Parse(stringVariable);

The reason most programmers use the Convert class over the Int32 class is because the Convert class has the power to convert to and from a wide selection of variable types. In the end of the day, it really is a matter of preference, but I suggest first time programmers to get in the habit of using the Convert class.