Answered by:
Null input validation and boundary case
Question

Hi:
I am wondering how to handle this. With a programming assignment I have given my class, they have to test for null input i.e. 'enter' entered rather than a number. I have a little routine that tells lets them graciously exit the program or gets them to reenter. (They choose how to do this). BUT how would I adapt this to also handle a '0' entered as a valid number? If they enter a 0 on purpose as part of their data, it also exits the program (albeit gracefully)
Any thoughts would be very much appreciated.
Liz
Answers

This is really a problem. As you mentioned it is impossible to differentiate between a zero standing for no input and a zero meaning the input was 0.
The solution I came up with (GMH674)
reads the input as text,
checks for no input,
checks if the input contains only digits
and repeats the input loop as long as no number was entered.
' array of valid characters For i = 0 To 9 digits[i] = i EndFor is_number = "False" While is_number = "False" ' read input TextWindow.Write("enter a number: ") input = TextWindow.Read() is_number = "True" ' nothing entered ? If Text.GetLength(input) = 0 Then is_number = "False" EndIf ' is it really a number ? For i = 1 To Text.GetLength(input) If Array.ContainsValue(digits, Text.GetSubText(input, i, 1)) = "False" Then is_number = "False" EndIf EndFor ' remind user to enter only a number If is_number = "False" Then TextWindow.WriteLine("please enter a valid number.") EndIf EndWhile '  output  TextWindow.ForegroundColor = "red" TextWindow.WriteLine(input) ' small basic automatically converts a text to a number if needed & possible TextWindow.WriteLine(input + 5)
I hope this helps...
 Proposed as answer by Nonki TakahashiModerator Wednesday, July 3, 2013 4:14 AM
 Marked as answer by Nonki TakahashiModerator Thursday, July 11, 2013 9:11 AM

If you have in your program: input=Textinput.Read(),
you can check for no input with: If input="" Then
or for input with: If input<>"" Then
Jan [ WhTurner ] The Netherlands
 Proposed as answer by Nonki TakahashiModerator Wednesday, July 3, 2013 4:14 AM
 Marked as answer by Nonki TakahashiModerator Thursday, July 11, 2013 9:11 AM
All replies

This is really a problem. As you mentioned it is impossible to differentiate between a zero standing for no input and a zero meaning the input was 0.
The solution I came up with (GMH674)
reads the input as text,
checks for no input,
checks if the input contains only digits
and repeats the input loop as long as no number was entered.
' array of valid characters For i = 0 To 9 digits[i] = i EndFor is_number = "False" While is_number = "False" ' read input TextWindow.Write("enter a number: ") input = TextWindow.Read() is_number = "True" ' nothing entered ? If Text.GetLength(input) = 0 Then is_number = "False" EndIf ' is it really a number ? For i = 1 To Text.GetLength(input) If Array.ContainsValue(digits, Text.GetSubText(input, i, 1)) = "False" Then is_number = "False" EndIf EndFor ' remind user to enter only a number If is_number = "False" Then TextWindow.WriteLine("please enter a valid number.") EndIf EndWhile '  output  TextWindow.ForegroundColor = "red" TextWindow.WriteLine(input) ' small basic automatically converts a text to a number if needed & possible TextWindow.WriteLine(input + 5)
I hope this helps...
 Proposed as answer by Nonki TakahashiModerator Wednesday, July 3, 2013 4:14 AM
 Marked as answer by Nonki TakahashiModerator Thursday, July 11, 2013 9:11 AM

If you have in your program: input=Textinput.Read(),
you can check for no input with: If input="" Then
or for input with: If input<>"" Then
Jan [ WhTurner ] The Netherlands
 Proposed as answer by Nonki TakahashiModerator Wednesday, July 3, 2013 4:14 AM
 Marked as answer by Nonki TakahashiModerator Thursday, July 11, 2013 9:11 AM

This is my sample.
Q = Text.GetCharacter(34) While "True" TextWindow.Write("Number? ") input = TextWindow.Read() CheckInput() Result() EndWhile Sub Result TextWindow.WriteLine("Input is " + Q + input + Q + ".") If isNull Then TextWindow.WriteLine("is null.") Else TextWindow.WriteLine(input + " is not null.") If isNumber Then TextWindow.WriteLine(input + " is number.") Else TextWindow.WriteLine(input + " is not number.") EndIf EndIf EndSub Sub CheckInput len = Text.GetLength(input) If len = 0 Then isNull = "True" Else isNull = "False" isNumber = "True" For i = 1 To len If Text.IsSubText("0123456789", Text.GetSubText(input, i, 1)) = "False" Then isNumber = "False" EndIf EndFor EndIf EndSub
Nonki Takahashi
 Edited by Nonki TakahashiModerator Wednesday, July 3, 2013 4:09 AM minor change