none
Null input validation and boundary case RRS feed

  • 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 re-enter.  (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

    Sunday, June 30, 2013 5:23 AM

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...

    Sunday, June 30, 2013 7:38 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

    Sunday, June 30, 2013 10:01 AM
    Answerer

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...

    Sunday, June 30, 2013 7:38 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

    Sunday, June 30, 2013 10:01 AM
    Answerer
  • 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


    Wednesday, July 3, 2013 4:08 AM
    Moderator