Reading & displaying words from a dictionary.txt file

  • (2 Pages)
  • +
  • 1
  • 2

15 Replies - 4454 Views - Last Post: 03 December 2007 - 08:11 PM Rate Topic: -----

#1 WizX  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 14-October 07

Reading & displaying words from a dictionary.txt file

Posted 14 October 2007 - 12:58 AM

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:

   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 October 2007 - 01:00 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Reading & displaying words from a dictionary.txt file

#2 aceofspades686  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 6
  • View blog
  • Posts: 334
  • Joined: 08-October 07

Re: Reading & displaying words from a dictionary.txt file

Posted 14 October 2007 - 01:29 AM

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)
' 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 October 2007 - 01:30 AM

Was This Post Helpful? 0
  • +
  • -

#3 WizX  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 14-October 07

Re: Reading & displaying words from a dictionary.txt file

Posted 14 October 2007 - 02:31 AM

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:

 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
Was This Post Helpful? 0
  • +
  • -

#4 aceofspades686  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 6
  • View blog
  • Posts: 334
  • Joined: 08-October 07

Re: Reading & displaying words from a dictionary.txt file

Posted 14 October 2007 - 03:16 AM

Well, at least you got rid of the error, that's getting somewhere. ;)

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.
 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.
Was This Post Helpful? 0
  • +
  • -

#5 WizX  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 14-October 07

Re: Reading & displaying words from a dictionary.txt file

Posted 14 October 2007 - 04:41 AM

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

 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 October 2007 - 04:44 AM

Was This Post Helpful? 0
  • +
  • -

#6 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1637
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: Reading & displaying words from a dictionary.txt file

Posted 14 October 2007 - 08:10 AM

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

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 :)
Was This Post Helpful? 0
  • +
  • -

#7 aceofspades686  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 6
  • View blog
  • Posts: 334
  • Joined: 08-October 07

Re: Reading & displaying words from a dictionary.txt file

Posted 14 October 2007 - 01:49 PM

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.
Was This Post Helpful? 0
  • +
  • -

#8 WizX  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 14-October 07

Re: Reading & displaying words from a dictionary.txt file

Posted 14 October 2007 - 11:50 PM

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.
Was This Post Helpful? 0
  • +
  • -

#9 aceofspades686  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 6
  • View blog
  • Posts: 334
  • Joined: 08-October 07

Re: Reading & displaying words from a dictionary.txt file

Posted 14 October 2007 - 11:52 PM

You'll need to change txtInput.text to reference to whatever you have your input textbox named. I used txtInput as an example.
Was This Post Helpful? 0
  • +
  • -

#10 WizX  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 14-October 07

Re: Reading & displaying words from a dictionary.txt file

Posted 16 October 2007 - 12:32 AM

View Postaceofspades686, on 15 Oct, 2007 - 01:52 AM, said:

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)
Was This Post Helpful? 0
  • +
  • -

#11 aceofspades686  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 6
  • View blog
  • Posts: 334
  • Joined: 08-October 07

Re: Reading & displaying words from a dictionary.txt file

Posted 16 October 2007 - 01: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?
Was This Post Helpful? 0
  • +
  • -

#12 WizX  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 14-October 07

Re: Reading & displaying words from a dictionary.txt file

Posted 16 October 2007 - 10:00 AM

View Postaceofspades686, on 16 Oct, 2007 - 03:06 AM, said:

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
Was This Post Helpful? 0
  • +
  • -

#13 aceofspades686  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 6
  • View blog
  • Posts: 334
  • Joined: 08-October 07

Re: Reading & displaying words from a dictionary.txt file

Posted 16 October 2007 - 10:10 AM

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 = new IO.StreamReader("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
Do While allwords.Peek <> -1
	   Dim file As String = allwords.ReadLine
		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.Items.Add(file)
		End If
Loop
End Sub 


This code should work then. This will read each line into the file string, check to see if it is contained (in any order) within the word, and if so, output it to your listbox (I assume that's what lstWords is). Then, loop back and check the next line in the file. Lather, rinse, repeat until the end of file is reached.

EDIT: Quick edit because I noticed something else a little odd. If you caught this before the edit you'll need to replace the 3rd line before it will work properly.

This post has been edited by aceofspades686: 16 October 2007 - 10:14 AM

Was This Post Helpful? 0
  • +
  • -

#14 WizX  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 14-October 07

Re: Reading & displaying words from a dictionary.txt file

Posted 16 October 2007 - 11:29 AM

Ok, as I was analyzing the error I've posted in my last two posts, I noticed this:

 Dim strInput As String '= txtInput.Text  'The input 

There was an extra comment-out before the =txtInput.Text. I took that out, and it finally go rid of the NullException error I was getting on the next line with strInput.

I tried running the code, and... it's weird. No matter what I type in, it only gives me results from the first ten or so AA words... even if I type in "clown" or "friend", etc. Any thoughts?

Thanks,
~ Lonnie
Was This Post Helpful? 0
  • +
  • -

#15 aceofspades686  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 6
  • View blog
  • Posts: 334
  • Joined: 08-October 07

Re: Reading & displaying words from a dictionary.txt file

Posted 16 October 2007 - 11:37 AM

Hm, good catch on the comment thing, as for the odd logic error, I'm not sure. I'm going to actually load up VS this time and play around with it for a sec, if you want to attach your dictionary.txt file I'll use it while testing.

EDIT: Nevermind, caught the bug

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 = new IO.StreamReader("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
		'This is used to test and see if the character is there
Do While allwords.Peek <> -1
	   Dim testcount As Integer = 0 
	   Dim file As String = allwords.ReadLine
		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.Items.Add(file)
		End If
Loop
End Sub



testcount Needed to be reset to 0 each time the loop ran and I had it sitting outside the loop.

This post has been edited by aceofspades686: 16 October 2007 - 12:04 PM

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2