Results 1 to 4 of 4

Thread: Make a "simple dictionary", what is the best to way store word ?

Hybrid View

  1. #1

    Thread Starter
    New Member
    Join Date
    Jan 2018
    Posts
    3

    Make a "simple dictionary", what is the best to way store word ?

    So i want to make a simple dictionary using console where the user can 1. AddWord,2. ReplaceWord 3.PrintAllWord

    Example input
    1. Stung
    1. Still
    1. And
    2. And>Pork
    1. Star
    3
    Output
    S : Stung, Still, Star
    P : Pork

    So what is the most efficient and best way store word ? ArrayList, List of string, Dictionary or Jagged Array ?
    Already tried using list of string and dictionary but it doesn't work

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: Make a "simple dictionary", what is the best to way store word ?

    There are many, many ways it could be done but all your suggestions will only work while your app is running. If you expect the data to persist between sessions then you need a plan for how to so that, e.g. text file or database.

    As for your suggestions, I would first point out that a list of words is not a dictionary. If all you want is a list of words then a List(Of String) is the obvious choice. If you want an actual dictionary, i.e. a list of words and their definitions, then a Dictionary(Of String, String) seems a good place to start.

    Telling us that something "doesn't work" is all but useless. A List or a Dictionary work perfectly, i.e. they do exactly what they do. If the way you used them didn't achieve your goal then the way you used them was wrong. If you don't show us what you did then we can't tell you what was wrong with it.

    It is always the wrong approach to come here and basically say "I tried, I failed, you do it for me". It's always important that you show us what you did. It may be that you are 99% right and just made one small mistake, in which case it would be a waste of our time to provide a solution from scratch. It's also possible that seeing what you did can provide us with insights into how you were thinking about the problem and if we can help to correct faulty thinking then we can help you avoid similar problems later. Both cases are more beneficial to you than simply copying and pasting something that someone else wrote for you.

  3. #3

    Thread Starter
    New Member
    Join Date
    Jan 2018
    Posts
    3

    Re: Make a "simple dictionary", what is the best to way store word ?

    Sorry not to include my code

    This is with List of
    Code:
    Module Module1
        Sub Main()
            Dim pilihan As Integer
            Dim i As Integer
            Dim input As String
            Dim dic As New List(Of String)
            Do
                input = Console.ReadLine
                Dim word As String() = input.Split("#")
                pilihan = (0)
                Select Case pilihan
                    Case 1
                        If Not dic.Contains(word(1)) Then
                            dic.Add(word(1))
                        End If
                    Case 2
                        If dic.Contains(word(1)) Then
                            i = dic.IndexOf(word(1))
                            dic(i) = word(2)
                        Else
                            dic.Add(word(1))
                        End If
                    Case 3
                        For i = 0 To dic.Count - 1
                            Console.WriteLine(dic.Item(i))
                        Next
                    Case Else
                        Exit Select
                End Select
            Loop
            Console.ReadKey()
        End Sub
    End Module
    With dictionary
    Code:
    Module Module2
        Sub AddWord(ByVal input As String)
            Dim prefix As String = input.Substring(0, 1)
            Dim exist As Boolean = GlobalVar.wordcollection.ContainsKey(prefix)
            If exist = False Then
                GlobalVar.wordcollection.Add(prefix, input)
            Else
            End If
        End Sub
        Sub UpdateWord(ByVal oldword As String, ByVal newword As String)
            Dim prefix As String = oldword.Substring(0, 1)
            If GlobalVar.wordcollection.ContainsKey(prefix) = True Then
                Dim temp As String = GlobalVar.wordcollection.Item(prefix)
                Dim result As String = temp.Replace(oldword, newword)
                GlobalVar.wordcollection.Item(prefix) = result
            End If
        End Sub
        Sub Main()
            Dim pilihan As Integer
            Dim input As String
            Do
                input = Console.ReadLine
                Dim word As String() = input.Split("#")
                pilihan = word(0)
                Select Case pilihan
                    Case 1
                        AddWord(word(1))
                    Case 2
                        UpdateWord(word(1), word(2))
                    Case 3
                        For Each s In GlobalVar.wordcollection
                            Console.WriteLine(s)
                        Next
                    Case Else
                        Exit Select
                End Select
            Loop
            Console.ReadKey()
        End Sub
    End Module

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: Make a "simple dictionary", what is the best to way store word ?

    And what actually happened when you executed that code? Don't make us guess when you already know.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width