|
You want to compare one word to the words in an ArrayList? Then there is no reason to repeat the word in an ArrayList. That won't help you the least bit in comparing them. Just loop though the list of words and compare them to the single word.
Unless you are stuck with framework 1.x, don't use an ArrayList at all. Use a List<string> instead.
Despite everything, the person most likely to be fooling you next is yourself.
|
|
|
|
|
So I don´t need any complicated algorithms?
Could you give an example.
|
|
|
|
|
Doesn't the ArrayList have a Contains method ? So you check if the ArrayList contains each word, one at a time.
Why are you using .NET 1.1 ?
Christian Graus
Please read this if you don't understand the answer I've given you
"also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )
|
|
|
|
|
bool found = false;
string word = richTextBox1.Text;
foreach (string w in al) {
if (w == word) {
found = true;
break;
}
}
Despite everything, the person most likely to be fooling you next is yourself.
|
|
|
|
|
dennycrane wrote: I´m making a spelling test.
If each of the words were spelled in the text box on it's own line, e.g.
[Textbox looks like this]
Foo
Bar
Bat
Baz Then I'd code it up like this:
string[] correctlySpelledWords = { "foo", "bar", "bat", "baz" };
string[] studentSpelledWords = textBox.Text.Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);
bool studentSpelledEverythingCorrectly = ContainsSameElements(correctlySpelledwords, studenSpelledWords);
....
bool ContainsSameElements(string[] first, string[] second)
{
if (first.Length != second.Length)
{
return false;
}
for (int i = 0; i < first.Length; i++)
{
if (!string.Equals(first, second, StringComparison.InvariantCultureIgnoreCase))
{
return false;
}
}
return true;
} That ignores casing (e.g. spelling "Foo" as "foo" is acceptable).
FWIW, you can probably do this in less code using a LINQ query if you're using the latest version of .NET.
|
|
|
|
|
so this is what I have
don´t I need a list to compare though?
private void button1_Click(object sender, EventArgs e)
{
AthugaStafset();
ContainsSameElements(first, second);
}
public void ChkSpell()
{
string[] correctlySpelledWords = { "foo", "bar", "bat", "baz" };
string[] studentSpelledWords = richTextBox1.Text.Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);
bool studentSpelledEverythingCorrectly = ContainsSameElements(correctlySpelledWords, studentSpelledWords);
if (studentSpelledEverythingCorrectly)
{
label1.Text = "Same";
}
else
{
label1.Text = "They differ";
}
}
public bool ContainsSameElements(string[] first, string[] second)
{
if (first.Length != second.Length)
{
return false;
}
for (int i = 0; i < first.Length; i++)
{
if (!string.Equals(first, second, StringComparison.InvariantCultureIgnoreCase))
{
return false;
}
}
return true;
|
|
|
|
|
dennycrane wrote: don´t I need a list to compare though?
You already do. These 2 lines are string arrays (a kind of list):
string[] correctlySpelledWords = { "foo", "bar", "bat", "baz" };
string[] studentSpelledWords = richTextBox1.Text.Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);
One is a list of correctly spelled words, another is th elist of student-spelled words.
|
|
|
|
|
I'd go for:
List<string> correctSpellings;
List<string> userSpellings
correctSpellings.Add("string1");
...
userSpellings.Add(textBox.Text);
...
for(int i=0; i<userspellings.length; i++) {
if(String.Compare(userSpellings[i], correctSpellings[i], true) == 0)
else
}
String.Compare compares two strings, if they are equal then it returns 0. The 'true' part is to ignore case.
If your NOT going to ignore case, then you may as well just use if(userSpellings[i] == correctSpelling[i])
My current favourite word is: Bacon!
-SK Genius
|
|
|
|
|
userSpelling.Length can´t be done.
|
|
|
|
|
What is it then? userSpelling.Count ? So i made a little mistake
Oh yeah, and another mistake i may have made: initialising the lists, im pretty sure it needs to be more like: List<string> myList = new List<string>() Okay, thats all my mistakes fixed, are we all happy? good.
My current favourite word is: Bacon!
-SK Genius
|
|
|
|
|
Yes, but now I only get "Incorrect" even though the spelling is correct.
|
|
|
|
|
Perhaps the wrong strings are being compared? Set a breakpoint on the if statement and debug the code code. When it get to the if statement execution will pause and you'll be able to see which strings are being compared. From that you should be able to come up with a solution.
Or an easier way, show a message box with each of the strings in it, so just before the if statement and in a MessageBox.Show("Users:" + userSpelling[i] +", Correct:" + correctSpelling[i]); or similar.
My current favourite word is: Bacon!
-SK Genius
|
|
|
|
|
The problem is the loop. You can't determine that the spelling is incorrect until you have looped through all the items. Now you are getting there when the first word doesn't match.
Look at the code that I posted elsewhere in the thread.
Despite everything, the person most likely to be fooling you next is yourself.
|
|
|
|
|
Well, i made the assumption that the order of the words would be the same for the list of correct spellings and the text boxes the user would type into. Clearly we had different views on how the program worked, and obviously I got it wrong.
My current favourite word is: Bacon!
-SK Genius
|
|
|
|
|
Well, the only thing different is I used RichTextBox
the text is written in the same order as the string is added.
|
|
|
|
|
|
I'm not sure whats going on.
My current favourite word is: Bacon!
-SK Genius
|
|
|
|
|
Sorry, I meant after the FIRST one.
|
|
|
|
|
hi everybody ..
I m new to networks and i want to block the incomming and outgoing data.
Is there any way to block the network traffic in C#..
thanks to all.
Tahir
|
|
|
|
|
|
Tahir Abbasi wrote: hi everybody ..
I m new to networks and i want to block the incomming and outgoing data.
Is there any way to block the network traffic in C#..
You're not that new. You've been posting the same question over and over again, and getting the same reply time and time again. For the love of the Grand Negus, give it up man. Change to a career flipping burgers before it's too late.
|
|
|
|
|
Pete O'Hanlon wrote: For the love of the Grand Negus, give it up man. Change to a career flipping burgers before it's too late.
"The clue train passed his station without stopping." - John Simmons / outlaw programmer
"Real programmers just throw a bunch of 1s and 0s at the computer to see what sticks" - Pete O'Hanlon
|
|
|
|
|
hy everyone!
i do have a problem. I have to split strings they are like
"variable1", "variable_1", "variable"
i do only have to parse the first line, but sometimes the first line looks like
"variable11", "variable_11", "variable1"
in this line the last "1" represents a color information.
I do only have to parse the first line, in both cases the result should be
variable1, variable_1, variable
example:
my input is
variable1, variable_1, variable
variable11, variable_11, variable1
=> result of first line: variable1, variable_1, variable
if the input is
variable11, variable_11, variable1
variable1, variable_1, variable
=> result of first line: variable1, variable_1, variable
does anyone have an idea of how to code this?
It's quit tricky. I tried myself for some hours but found no solution which works propperly. Because I do not know if the first element has two "11" or just one!
The string could also be in this order:
variable, variable1, variable_1
Maybe someone of you could help me please.
thanks.
Stephan.
|
|
|
|
|
stephan_007 wrote: does anyone have an idea of how to code this?
Yes. Use the keyboard to type syntactically correct code that represents the logic needed to satisfy your requirements.
led mike
|
|
|
|
|
You have to specify which parts of the string that can vary, and how. For example, the repeated part "variable" in your example, is that always the specific word "variable", or could it be anything?
If you have an actual example of some real data, that could help when trying to understand what you are trying to do.
Despite everything, the person most likely to be fooling you next is yourself.
|
|
|
|