none
array.containsvalue() RRS feed

  • Question

  • Hi

    Firstly, I'm having problems posting. Whenever I post, I can't seem to find the post in the forum...weird...but my problem is that I'm trying to do check a value in an array, but the array is created when input is captured from a textwindow.

    Steps:

    1. Enter letter
    2. store letter
    3. Enter another letter
    4. check letter against first letter
    5. if letter matches,
     then discard letter and display message "letter already entered"
    6. else
     store letter
    7. return to Line 3

    8.Repeat this 10 times
    9.Display letters stored in array

     

    Code:

    TextWindow.WriteLine("Enter a letter")


    For i = 1 To 10
      Letter = TextWindow.Read()
      'Entered[i] = Letters[i]
      If Letter = Letters[i] then
        TextWindow.WriteLine("You've already entered" + Entered[i])
      EndIf
     
    EndFor

    For a = 1 To 10
      TextWindow.Write(Letters[a])
    EndFor

    Thanks in advance...

    I'm having problems with the comparison of the first letter, storing in the array and checking subsequent letters in entered with stored letters in the array...

    Wendy

    Saturday, March 10, 2012 12:21 AM

Answers

  • Perhaps using While loops:

    charCount = 0
    readNewChar = "True"
    While (readNewChar)
      TextWindow.Write("Please enter a character (Space to finish): ")
      char = TextWindow.Read()
      char = Text.GetSubText(char,1,1) 'Just the first character entered
      If (char = " ") Then
        readNewChar = "False"
      Else
        'Check for duplicates
        newChar = "True"
        For i = 1 To charCount
          If (char = chars[i]) Then
            newChar = "False"
            TextWindow.WriteLine("The character '" + char + "' is already in use!")
            TextWindow.WriteLine("")
            TextWindow.Write("Please enter a new character and press enter: ")
          EndIf
        EndFor
        If (newChar) Then
          charCount = charCount+1
          chars[charCount] = char
        EndIf
      EndIf
    EndWhile
    
    TextWindow.WriteLine("Your characters are: ")
    For i = 1 To charCount
      TextWindow.WriteLine(chars[i])
    EndFor

    • Proposed as answer by GAniman Sunday, March 11, 2012 6:51 AM
    • Marked as answer by paz67 Saturday, March 24, 2012 5:46 AM
    Saturday, March 10, 2012 8:40 PM
    Moderator

All replies

  • This works well

    For i = 1 To 10
       Retry:
       TextWindow.Write(i+" : ")
       Letter = TextWindow.Read()
       dumy=text.GetLength(Letter)+Letter

       If Text.IsSubText(sumLetter,dumy) Then   '  avoid  same Letter
         TextWindow.WriteLine("You've already entered --->" + Letter+" ; "+sumLetter)
          Goto Retry
        EndIf
       sumLetter=sumLetter+dumy+":"       'sum up Letters
        Letters[i] = Letter
     EndFor

    TextWindow.WriteLine("  ")
    For a = 1 To 10
       TextWindow.WriteLine(a+" : "+Letters[a])
    EndFor





    Saturday, March 10, 2012 1:54 AM
    Answerer
  • Thanks NaochanON,

    But does anyone have a way of doing this a little more elegantly than using GOTO routines?

    Code:

    For y = 1 To 10
      TextWindow.Write("Please enter a character: ")
      start:
      char = TextWindow.Read()
      For z = 1 To y
          If char = charr[z] Then
          TextWindow.WriteLine("The character '" + char + "' is already in use!")
          TextWindow.WriteLine("")
          TextWindow.Write("Please enter a new character and press enter: ")
          Goto start
        EndIf
      EndFor
      charr[y] = char
    EndFor

    TextWindow.WriteLine("Your characters are: ")
    For z = 1 To 10
      TextWindow.WriteLine(charr[z])
      EndFor

     



    Wendy

    Saturday, March 10, 2012 9:12 AM
  • Perhaps using While loops:

    charCount = 0
    readNewChar = "True"
    While (readNewChar)
      TextWindow.Write("Please enter a character (Space to finish): ")
      char = TextWindow.Read()
      char = Text.GetSubText(char,1,1) 'Just the first character entered
      If (char = " ") Then
        readNewChar = "False"
      Else
        'Check for duplicates
        newChar = "True"
        For i = 1 To charCount
          If (char = chars[i]) Then
            newChar = "False"
            TextWindow.WriteLine("The character '" + char + "' is already in use!")
            TextWindow.WriteLine("")
            TextWindow.Write("Please enter a new character and press enter: ")
          EndIf
        EndFor
        If (newChar) Then
          charCount = charCount+1
          chars[charCount] = char
        EndIf
      EndIf
    EndWhile
    
    TextWindow.WriteLine("Your characters are: ")
    For i = 1 To charCount
      TextWindow.WriteLine(chars[i])
    EndFor

    • Proposed as answer by GAniman Sunday, March 11, 2012 6:51 AM
    • Marked as answer by paz67 Saturday, March 24, 2012 5:46 AM
    Saturday, March 10, 2012 8:40 PM
    Moderator