-
Help ! with arrays
Hi,
I'm working on a program using visual basic.net where I have to input a sentence in english and translate the sentence to german and french by using corresponding arrays.
I already have inputed the english, german and french arrays.
I need help creating a for statement that will translate the sentence word for word.
The code I have now can only translate one word at a time before it crashes.
I'm in desperate need for help!
-
Post the code you have so far, along with the error message you are getting and the line it is ocurring on.
-
Sub translate()
Dim s As String
s = InputBox("Enter a sentence").Trim()
Dim i As Integer
Dim englisharray() As String = {"", "YES", "TABLE", "THE", "IS", "YELLOW", "FRIEND", "SICK", "MY", "LARGE", "NO", "HAT", "PENCIL", "RED", "ON", "AUTO", "OFTEN"}
Dim frencharray() As String = {"", "OUI", "TABLE", "LA", "EST", "JAUNE", "AMI", "MALADE", "MON", "GROS", "NON", "CHAPPEAU", "CRAYON", "ROUGE", "SUR", "AUTO", "SOUVENT"}
Dim germanarray() As String = {"", "JA", "TISCH", "DEM", "IST", "GELB", "FREUND", "KRANK", "MEIN", "GROSS", "NEIN", "HUT", "BLEISTIFT", "ROT", "AUF", "AUTO", "OFT"}
Dim word As String
Dim t1, ch, t2 As String
Dim cnt As Double
For i = 0 To s.Length() - 1
word = s.Substring(i, 1)
If word = englisharray(i) Then
t1 = frencharray(i)
End If
Next
Output.Items.Add(t1)
End Sub
The error says "argument null exception"
I'm not even sure where to begin to translate word for word.
-
The .Net forums would be a better place for this.
-
Use the dynamic ArrayList instead of string arrays . I think your problem has to do with initializing the array first . In ArrayList you don't have to initialize it .
-
let's say your input sentence is:
"This is the large yellow table"
The logic you have written in your code is this:
You have created a loop, that will run from 0 to s.length -1(being 29 characters for this sample input sentence),
and on each iteration, you are grabbing just one character of the input sentence.
So on the first loop through your for-loop, word = "T".
2nd: "h"
3rd: "i"
4th: "s"
As you can see, your code is flawed already. You are saying:
word = s.Substring(i,1). Which returns a part of the input sentence starting at index i. The 1 means the SubString(i,1) will only return one character, at the position specified by i.
You can never possibly match an english word this way, because you are trying to match only one character against a word, which consists of more than one character. "t" is not equal to "this", so it will always fail.
-
So, instead of looping through the input sentence, you should try looping through the array of english words.
Grab each word out of the array, then, use another for-loop to step through the sentence (like you did above).
Here's the kicker:
The words in the english array are of different lengths.
Let's say the first word is "YES".
That's three characters long.
So we have the word "YES", we know its 3 characters long.
So now, step through the input sentence one character at a time, but return 3 characters at a time, if the two strings equal each other, you have a match. word=s.SubString(i,j) 'where j=the length of the word you are trying to match. .
But you still have a problem. YES could match on a name like KEYES, and replace the YES part with OUI, resulting in KEOUI. Which is wrong.
So we have to check one more thing, to make sure it is a true match. You have to check if the character before and after your possible match, are not letters. So Yes! would match, and so would " YES ", or " YES."
Get my drift? If you find out the character before the "Y" in "YES", is "E" as it would be in "KEYES", your match is invalid, and you should ignore it. And the same applies for the character, after the "S" in "YES" (if there is one or its the end of the sentence).
There are a few functions you will need to use.
String.Length (to get the length of the english word in the english array), so you can determine how many characters you should be pulling back from the input sentence at one time.
Char.IsLetterOrDigit to determine if the character before and after your possible match is not a letter or digit, which would result in a false match. Remember, we don't want KEYES
Good luck..