Welcome to Dream.In.Code
Become a VB.NET Expert!

Join 149,578 VB.NET Programmers for FREE! Get instant access to thousands of VB.NET experts, tutorials, code snippets, and more! There are 1,753 people online right now. Registration is fast and FREE... Join Now!




Reading & displaying words from a dictionary.txt file

2 Pages V  1 2 >  
Reply to this topicStart new topic

Reading & displaying words from a dictionary.txt file

WizX
13 Oct, 2007 - 11:58 PM
Post #1

New D.I.C Head
*

Joined: 13 Oct, 2007
Posts: 9


My Contributions
Hi, I'm new to the forums and they're great! Anyway, I'm trying to get it where in VB.net: I want to enter 3 letters in a textbox, click a button, and it generate all possible words in a listbox that I can create with those three letters. I have a dictionary.txt file it can refer to, as well. For example, if I type in "eat" in the textbox and hit my button, the listbox should display "ATE" "EAT" and "TEA". Here is the code I have been working on:

CODE
   lstWords.Items.Clear()
        Dim allwords As IO.StreamReader = IO.File.OpenText("c:\dictionary.txt")
        Dim y As String
        Dim temp As String
        Do While allwords.Peek <> -1
            y = allwords.ReadLine
            y = y.ToUpper()
            temp = y
            temp.Contains("A" Or "B" Or "C" Or "D" Or "E" Or "F" Or "G" Or "H" Or "I" Or "J" Or "K" Or "L" Or "M" Or "N" Or "O" Or "P" Or "Q" Or "R" Or "S" Or "T" Or "U" Or "V" Or "W" Or "X" Or "Y" Or "Z")
            If y = temp Then
                lstWords.Items.Add(allwords.ReadToEnd.Contains(temp))
            End If
            
        Loop

Before I added the temp.Contains line, it was just return "True" in my listbox when I click my button. Once I added that long line, it's giving me the error (once I click the button): "Conversion from string "A" to type 'Long' is not valid"

Thanks in advance for any help,
~ Lonnie

This post has been edited by WizX: 14 Oct, 2007 - 12:00 AM
User is offlineProfile CardPM
+Quote Post

aceofspades686
RE: Reading & Displaying Words From A Dictionary.txt File
14 Oct, 2007 - 12:29 AM
Post #2

D.I.C Regular
Group Icon

Joined: 8 Oct, 2007
Posts: 261


Dream Kudos: 100
My Contributions
Well, first thing I notice is that you're if then statement is always going to return true because in this code, y will be the same thing as temp because you set it equal two likes above your conditional. The string.contains method doesn't put all strings in a variable that contain the argument, it returns a boolean saying that they are(not) contained inside that string.

(Example from the MSDN Library)
CODE

' This example demonstrates the String.Contains() method
Imports System

Class Sample
   Public Shared Sub Main()
      Dim s1 As String = "The quick brown fox jumps over the lazy dog"
      Dim s2 As String = "fox"
      Dim b As Boolean
      b = s1.Contains(s2)
      Console.WriteLine("Is the string, s2, in the string, s1?: {0}", b)
   End Sub 'Main
End Class 'Sample
'
'This example produces the following results:
'
'Is the string, s2, in the string, s1?: True
'

You can read more on it here.

As for your error, I'm fairly certain (but someone correct me if I'm wrong) that you can't use Or within a value passed to a method, so that would probably be what throws it. Typically that code would be written more along the lines of If string.contains("A") orelse string.contains("B")... and so forth. Also, if I'm reading correctly What this code is currently doing (or attempting to do rather) is reading the entire word list into y, putting that word list again into temp, checking to see if temp contains any letter from A-Z, and if it does, then output the entire file.

Not to say that I don't think you're on the right track with the contains thing, but you will need to rework a few things in order for this code to work. What I might suggest (though, there are others on this board that can probably tell you a better method) since you're wanting to search for words no matter the order of the letters, is to look into the String.ToCharArray method and then possibly a For Each...Next loop. If you have trouble understanding or implementing any of this, feel free to post in this same thread and ask. Hope this helps!

This post has been edited by aceofspades686: 14 Oct, 2007 - 12:30 AM
User is offlineProfile CardPM
+Quote Post

WizX
RE: Reading & Displaying Words From A Dictionary.txt File
14 Oct, 2007 - 01:31 AM
Post #3

New D.I.C Head
*

Joined: 13 Oct, 2007
Posts: 9


My Contributions
Hi, thanks for the fast response. I won't lie and claim I understand everything you told me, but I have been working on different methods. Here is my new updated code:

CODE
Private Sub btnGo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGo.Click
        lstWords.Items.Clear()
        Dim allwords As IO.StreamReader = IO.File.OpenText("c:\dictionary.txt")
        Dim y As String
        Dim temp As String
        Do While allwords.Peek <> -1
            y = allwords.ReadLine
            y = y.ToUpper()
            temp = y
            If temp.Contains("A") OrElse temp.Contains("B") OrElse temp.Contains("C") OrElse temp.Contains("D") OrElse temp.Contains("E") _
OrElse temp.Contains("F") OrElse temp.Contains("G") OrElse temp.Contains("H") OrElse temp.Contains("I") OrElse temp.Contains("J") _
OrElse temp.Contains("C") OrElse temp.Contains("C") OrElse temp.Contains("C") OrElse temp.Contains("C") OrElse temp.Contains("C") _
OrElse temp.Contains("K") OrElse temp.Contains("L") OrElse temp.Contains("M") OrElse temp.Contains("N") OrElse temp.Contains("O") _
OrElse temp.Contains("P") OrElse temp.Contains("Q") OrElse temp.Contains("R") OrElse temp.Contains("S") OrElse temp.Contains("T") _
OrElse temp.Contains("U") OrElse temp.Contains("V") OrElse temp.Contains("W") OrElse temp.Contains("X") OrElse temp.Contains("Y") _
OrElse temp.Contains("Z") Then
                lstWords.Items.Add(allwords.ToString.Contains(temp))
            End If

        Loop


When I click my btnGo button, after typing in a string in the textbox, it generates about 60,000 "False" strings in my listbox. Btw, 60,000 is the number of words in my dictionary.txt file, it's like it just replaced every line with "False" rather than the word. It never comes up "True" no matter what I type in my textbox. Also, I know you mentioned above about the arrays, I tried replacing all of the temp.Contains as temp.ToCharArray.ToString.Contains("A"), but that didn't make any difference. Sorry that I don't understand arrays yet...

~ Lonnie

User is offlineProfile CardPM
+Quote Post

aceofspades686
RE: Reading & Displaying Words From A Dictionary.txt File
14 Oct, 2007 - 02:16 AM
Post #4

D.I.C Regular
Group Icon

Joined: 8 Oct, 2007
Posts: 261


Dream Kudos: 100
My Contributions
Well, at least you got rid of the error, that's getting somewhere. wink2.gif

Okay, first off, you have to get what's in your textbox before you can compare anything to it. From what I can see, you read from your txt file and write to your listbox, but no input from your textbox.

Also, to explain what I mean about the string.tochararray method. The simplest terms I can think to describe an array as is a list (at least in this case, but I won't go into to much detail). I will warn you in advance (unless there's some simpler method that I don't know) this code will get really messy really quick no matter which way you go about it.

I'll give you this snippet to help you out.
CODE

Dim strInput As String = TextBox1.Text  'This would be your input
        Dim inputChar As Char() = strInput.ToUpper.ToCharArray 'This is the character array I was talking about earlier, it would contain all of the characters (letters) in your input string in a list
        Dim testcount As Integer = 0 'This is used to test and see if the character is there, I'll explain why in a moment

        For Each c As Char In inputChar ' Loop that runs through and looks at each character in the array
            If allwords.Contains(c.ToString) Then
                testcount += 1 ' If the word you're currently looking at contains that character, increment the counter by one
            End If
        Next

        If testcount = inputChar.Length Then ' This is where the testcount comes in.  Essentially this takes the number and if all the characters in the array (your input string) were found in the word, then it outputs the word
            Label1.Text = allwords 'I used a label just as a quick test, listbox would be the same principle
        End If


As I said, it gets a little messy, but if you can figure out how to implement this code within your code, then it should work.
User is offlineProfile CardPM
+Quote Post

WizX
RE: Reading & Displaying Words From A Dictionary.txt File
14 Oct, 2007 - 03:41 AM
Post #5

New D.I.C Head
*

Joined: 13 Oct, 2007
Posts: 9


My Contributions
Ok, I've tried implementing the code. Here's what I have:

CODE
Private Sub btnGo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGo.Click
        lstWords.Items.Clear()
        Dim allwords As IO.StreamReader = IO.File.OpenText("c:\dictionary.txt")
        Dim strInput As String = txtInput.Text  'The input
        Dim inputChar As Char() = strInput.ToUpper.ToCharArray 'This is the character array I was talking about earlier, it would contain all of the characters (letters) in your input string in a list
        Dim testcount As Integer = 0 'This is used to test and see if the character is there

        For Each c As Char In inputChar ' Loop that runs through and looks at each character in the array
            If allwords.Contains(c.ToString) Then
                testcount += 1 ' If the word you're currently looking at contains that character, increment the counter by one
            End If
        Next

        If testcount = inputChar.Length Then ' This is where the testcount comes in.  Essentially this takes the number and if all the characters in the array (your input string) were found in the word, then it outputs the word
            lstWords.Text = allwords 'I used a label just as a quick test, listbox would be the same principle
        End If


Two errors: "If allwords.Contains(c.ToString)" -- allwords.Contains is underlined, and it says 'Contains' is not a member of 'System.IO.StreamReader'.
The other error: "lstWords.Text = allwords" -- allwords is underlined, and it says Value of type 'System.IO.StreamReader' cannot be converted to 'String'.

Thanks again for any help, I think I'm starting to understand what an array is.

This post has been edited by WizX: 14 Oct, 2007 - 03:44 AM
User is offlineProfile CardPM
+Quote Post

PsychoCoder
RE: Reading & Displaying Words From A Dictionary.txt File
14 Oct, 2007 - 07:10 AM
Post #6

using DIC.Core;
Group Icon

Joined: 26 Jul, 2007
Posts: 9,482



Thanked: 161 times
Dream Kudos: 9050
Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, Boo.Net

My Contributions
The problem you're having now is because Contains isnt a member of StreamReader, as the error states. What you have to do is
  • Open the file with your StreamReader
  • Read the contents of the file into a string variable
  • Then check that string variable

I made a couple changes to your code that show how this is done

CODE

Private Sub btnGo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnGo.Click
        ' lstWords.Items.Clear()
        Dim allwords As IO.StreamReader = IO.File.OpenText("c:\dictionary.txt")
        Dim file As String = allwords.ReadToEnd
        Dim strInput As String '= txtInput.Text  'The input
        Dim inputChar As Char() = strInput.ToUpper.ToCharArray 'This is the character array I was talking about earlier, it would contain all of the characters (letters) in your input string in a list
        Dim testcount As Integer = 0 'This is used to test and see if the character is there

        For Each c As Char In inputChar ' Loop that runs through and looks at each character in the array
            If file.Contains(c.ToString) Then
                testcount += 1 ' If the word you're currently looking at contains that character, increment the counter by one
            End If
        Next

        If testcount = inputChar.Length Then ' This is where the testcount comes in.  Essentially this takes the number and if all the characters in the array (your input string) were found in the word, then it outputs the word
            lstWords.Text = allwords 'I used a label just as a quick test, listbox would be the same principle
        End If
End Sub


Here I declare a String variable named file, then right below the declaration I have file = allwords.ReadToEnd which tells the StreamReader to read the entire file and dump the contents into the file variable. Then you can do If file.Contains(c.ToString) as you had before.

Hope that helps a little smile.gif
User is offlineProfile CardPM
+Quote Post

aceofspades686
RE: Reading & Displaying Words From A Dictionary.txt File
14 Oct, 2007 - 12:49 PM
Post #7

D.I.C Regular
Group Icon

Joined: 8 Oct, 2007
Posts: 261


Dream Kudos: 100
My Contributions
Ack! Sorry, that was my bad. I wrote the code like allwords was the string containing the line read, not the file reader itself. Either way, PsychoCoder already pointed out the mistake, so that code should work.
User is offlineProfile CardPM
+Quote Post

WizX
RE: Reading & Displaying Words From A Dictionary.txt File
14 Oct, 2007 - 10:50 PM
Post #8

New D.I.C Head
*

Joined: 13 Oct, 2007
Posts: 9


My Contributions
Thanks for the replies. On the last code you gave me, when I tried to run it, it underlined strInput.ToUpper.ToCharArray and says: Variable 'strInput' is used before it has been assigned a value.

I tried running it anyway, and it just gives me a Null Reference Exception error.
User is offlineProfile CardPM
+Quote Post

aceofspades686
RE: Reading & Displaying Words From A Dictionary.txt File
14 Oct, 2007 - 10:52 PM
Post #9

D.I.C Regular
Group Icon

Joined: 8 Oct, 2007
Posts: 261


Dream Kudos: 100
My Contributions
You'll need to change txtInput.text to reference to whatever you have your input textbox named. I used txtInput as an example.
User is offlineProfile CardPM
+Quote Post

WizX
RE: Reading & Displaying Words From A Dictionary.txt File
15 Oct, 2007 - 11:32 PM
Post #10

New D.I.C Head
*

Joined: 13 Oct, 2007
Posts: 9


My Contributions
QUOTE(aceofspades686 @ 15 Oct, 2007 - 01:52 AM) *

You'll need to change txtInput.text to reference to whatever you have your input textbox named. I used txtInput as an example.


My textbox is already named txtInput. Using the exact code that PsychoCoder pasted, the allword on the line: lstWords.Text = allwords is underlined, and it says: Value of type 'System.IO.StreamReader' cannot be converted to 'String'
I tried a few things, but when I do it just underlines the thing I referred to in my last post (strInput)
User is offlineProfile CardPM
+Quote Post

aceofspades686
RE: Reading & Displaying Words From A Dictionary.txt File
16 Oct, 2007 - 12:06 AM
Post #11

D.I.C Regular
Group Icon

Joined: 8 Oct, 2007
Posts: 261


Dream Kudos: 100
My Contributions
Quick question before I answer this. I already see the problem, but how are your words separated within your dictionary.txt file? Are they each on a separate line?
User is offlineProfile CardPM
+Quote Post

WizX
RE: Reading & Displaying Words From A Dictionary.txt File
16 Oct, 2007 - 09:00 AM
Post #12

New D.I.C Head
*

Joined: 13 Oct, 2007
Posts: 9


My Contributions
QUOTE(aceofspades686 @ 16 Oct, 2007 - 03:06 AM) *

Quick question before I answer this. I already see the problem, but how are your words separated within your dictionary.txt file? Are they each on a separate line?


They are all on a separate line, such as:
AA
AAH
AAHED
AAHING
AAHS
AAL
User is offlineProfile CardPM
+Quote Post

2 Pages V  1 2 >
Fast ReplyReply to this topicStart new topic
Time is now: 1/7/09 10:37PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live VB.NET Help!

VB.NET Tutorials

Reference Sheets

VB.NET Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month