Reputation: 73
Code:
[int]$s = Read-Host "Enter number from 1-3"
switch ($s) {
1 { $s = 'Apple' }
2 { $s = 'Melon' }
3 { $s = 'Mango' }
}
$s
Output:
Cannot convert value "Apple" to type "System.Int32". Error input was not in correct format.
So my question is: How do I check if my input is a number and at the same time select from my switch?
Upvotes: 5
Views: 18313
Reputation: 15
function Verify-InputInteger {
Param (
$Question="Saisir un nombre",
$Min=0,
$Max=100
)
try {
[int]$string_Input = Read-Host $Question;
if($string_Input -LT $Min)
{
Throw "ErrorMin"
}
if($string_Input -GT $Max)
{
Throw "ErrorMax"
}
return $string_Input;
}
catch {
if($_.FullyQualifiedErrorId -EQ 'InvalidCastFromStringToInteger')
{
Write-Host 'You did not provide a number as input' -ForegroundColor Red;
Verify-InputInteger -Question $Question -Min $Min -Max $Max;
}
if($_.Exception.Message -EQ 'ErrorMin')
{
Write-Host 'You have entered a number lesser than the minimum limit' -ForegroundColor Red;
Verify-InputInteger -Question $Question -Min $Min -Max $Max;
}
if($_.Exception.Message -EQ 'ErrorMax')
{
Write-Host 'You have entered a number greater than the maximum limit' -ForegroundColor Red;
Verify-InputInteger -Question $Question -Min $Min -Max $Max;
}
}
}
Upvotes: 0
Reputation: 3350
How do I check if my input is a number and at the same time select from my switch?
How do you check if your input is a number?
- I think you are already doing that since you are declaring you variable $s
as [int]
at the very beginning -
[int]$s = Read-Host "Enter number from 1-3"
Selecting from your switch
- The error which you are getting is due to the type mismatch of your input to the read-host
and the variable $s
. $s
is clearly an integer where as you are assigning it a string. Hence, the error You have to type-cast that again in order to correct that -
[int]$s = Read-Host "Enter number from 1-3"
switch ($s) {
1 { [string]$s = 'Apple' }
2 { [string]$s = 'Melon' }
3 { [string]$s = 'Mango' }
}
$s
If you enter any number other than 1, 2 or 3, then you $s
will store that number in it. For example if you enter 5, $s
will store 5 in it since the switch
statement hadn't been executed.
Upvotes: 0
Reputation: 780
You don't need to use the integer to declare the variable:
$s = Read-Host "Enter number from 1-3"
switch ($s) {
1 { $s = 'Apple' }
2 { $s = 'Melon' }
3 { $s = 'Mango' }
}
$s
And if you check the type of variable:
Upvotes: 2
Reputation: 8442
When you have a predefined set of options like this, consider using a multiple-choice menu, like this:
$title = "Select Fruit"
$prompt = "Which fruit is your favorite?"
$apple = New-Object System.Management.Automation.Host.ChoiceDescription "&Apple","Apple"
$melon= New-Object System.Management.Automation.Host.ChoiceDescription "&Melon","Melon"
$mango= New-Object System.Management.Automation.Host.ChoiceDescription "Man&go", "Mango"
$options = [System.Management.Automation.Host.ChoiceDescription[]]($apple, $melon, $mango)
$selectedFruit = $host.ui.PromptForChoice($title, $prompt, $options, 0)
switch($selectedFruit)
{
0 {Write-Host "You chose Apple"}
1 {Write-Host "You chose Melon"}
2 {Write-Host "You chose Mango"}
}
In the ISE, the user will see a GUI prompt, with buttons to click on, and at the console, a menu with specific allowed letters to select (A, M and G in this case).
This method has the benefit that it looks like a typical prompt from PowerShell and will check and re-prompt if the user enters an invalid value. You can add a 'quit' option, so the user has an easy way to skip all options.
Upvotes: 1
Reputation: 23385
The problem with your code is that after you define $s
as an integer, you then later try to assign a string value to it.
You could instead do this:
[int]$s = Read-Host "Enter number from 1-3"
$result = switch ($s) {
1 { 'Apple' }
2 { 'Melon' }
3 { 'Mango' }
}
$result
Note that i'm also simplifying your code here by returning the result of the switch to $result
instead of assigning it inside each condition.
This works because $result
is an undefined variable that becomes a string when you assign one to it.
If you want to validate that the input is an integer, you could also consider doing something like this:
$input = Read-Host "Enter number from 1-3"
if (($input -isnot [int])) { Throw 'You did not provide a number as input' }
$result = switch ($input) {
1 { 'Apple' }
2 { 'Melon' }
3 { 'Mango' }
}
$result
Upvotes: 4