Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
I need help with this part of my code.
I'm checking the occurences of char within a string:
public string Obsahuje(string slovo, char pismeno)
{
if (slovo.Contains(pismeno))
{
char[] array = uhodnute.ToCharArray();
int index;
int zacatek = 0;
do
{
index = slovo.IndexOf(pismeno, zacatek);
array[index] = pismeno;
zacatek = index;
} while (slovo.IndexOf(pismeno, zacatek + 1) != -1);
uhodnute = new string(array);
return uhodnute;
}
--zivot;
return uhodnute;
}
It works fine with character that occurs just once, but when I try another that occurs twice or more times, the program just freezes. No error, it just stops responding. I know it's probably something very stupid, but I'm doing this for so long that I just can't see it. I'd appreciate some help.
Edit: It works with:
do
{
index = slovo.IndexOf(pismeno, index);
array[index] = pismeno;
index++;
} while (slovo.IndexOf(pismeno, index) != -1);
The problem is that you don't update the value of index, and the code freezes because it remains in the while loop for ever.
int index;
int zacatek = 0;
do
{
index = slovo.IndexOf(pismeno, zacatek);
array[index] = pismeno;
index++;
zacatek = index;
} while (slovo.IndexOf(pismeno, zacatek + 1) != -1);
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
How can I check if an array index is out of range? Or prevent it happening.
int index = 25;
if(index >= 0 && index < array.Length)
{
//it exists
}
Source: Does Index of Array Exist
Another way of checking if an array is out of bounds is to make a function. This will check if the index is "in bounds". If the index is below zero or over the array length you will get the result false.
private bool inBounds (int index, int[] array)
{
return (index >= 0) && (index < array.Length);
}
Correct way would be
int index = 25;
if (index >= 0 && index < array.Length)
{}
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
string text1 = "abcde";
string text2 = "fgchi";
I want to check if the 2 strings have the same characters at the same index and if they do then let the place where they are the same be printed.
for (int i = 0; i < text1.Length; i++)
if (text1[i] == text2[i])
Console.WriteLine("Character {0} at index {1}", text1[i], i);
Considering your strings have the same length.
Edit: if I should not give answers to trivial tasks such as this and instead encourage the user to find it by himself, then please point out that to me. I'm new around. [I suppose it's obvious, so i'm just not gonna do it, and adjust]
Maybe something like the following code helps. And it shouldn´t be important how long each string is with that. Maybe the string.Format isn´t needed.
private string charMatch(string str_a, string str_b)
{
int char_a = str_a.Count();
int char_b = str_b.Count();
int runs = 0;
StringBuilder sb = new StringBuilder();
if (char_a <= char_b) { runs = char_a; }
else { runs = char_b; }
for (int i = 0; i < runs; i++)
{
if (str_a[i] == str_b[i])
{
sb.Append(string.Format("Match found at {0} \n", i));
}
}
return sb.ToString();
}
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
Stuck for hours on this task. I really appreciate help, it is not shown in the literature how this is to be done. I can create the vector but after that I'm totally blocked.
Create a string vector with five elements. The user will then enter 5 names via a for loop. The program then writes these names via another for loop.
Please type out the code so I can understand. Without solving this first task I can't do the other ones.
Here's my futile attempt so far
int [] namn = new int [5];
for (int i = 0; i < 5; i++);
{
Console.Write("Ange fem namn");
string str = Console.ReadLine();
int names = Convert.ToInt32(str);
}
It seems that the main (and the most difficult error to find) is ; after for loop
for (int i = 0; i < 5; i++);
this loop does nothing five times.
//DONE: "string[]" - Create a STRING vector with five elements...
string [] namn = new string [5];
//DONE: namn.Length - no magic numbers (5)
for (int i = 0; i < namn.Length; i++) // !!! no ";" !!!
{
Console.Write("Ange fem namn");
//DONE: put into array, not to a local variable
namn[i] = Console.ReadLine();
}
// Print out the names
foreach(var item in namn)
Console.WriteLine(item);
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I am working on a MinHeap implementation for school and I have encountered a problem. The code typically works well but sometimes generates an argument out of range exception in my heapify method. I have tried to isolate the problem but I am a terrible debugger.
Here is my code for the function:
private void Heapify(int i)
{
int least;
int leftchild = 2 * (i + 1) - 1;
int rightchild = 2 * (i + 1);
if (leftchild < heap.Count && (heap[rightchild].CompareTo(heap[i]) < 0))
{
least = 1;
}
else
{
least = i;
}
if (rightchild < heap.Count && (heap[rightchild].CompareTo(heap[least]) < 0))
{
least = rightchild;
}
if (least != i)
{
T temp = heap[i];
heap[i] = heap[least];
heap[least] = temp;
this.Heapify(least);
}
Out of range exceptions are generally easy to track down. Basically, you have to make sure that whenever you're accessing an item in an array via indexes, said array's count / length is greater than the index. In other words, ensure that in every call to heap[#index], #index < heap.Count (either by straight checking it or by your method's logic)
if (leftchild < heap.Count && (heap[rightchild].CompareTo(heap[i]) < 0))
If rightchild >= heap.Count, this will give you an exception.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I want to match the values from reader and checkbox to change selected values of item of checkboxlist. But it does not work and I don't know what to do? Thanks.
while (reader.Read())
{
CheckBoxList1.Items.FindByValue(reader["malzeme_id"].ToString()).Selected = true;
}
I tried also,
while (reader.Read())
{
for (int i = 0; i < CheckBoxList1.Items.Count; i++)
{
if (CheckBoxList1.Items[i].Value.Equals(reader["malzeme_id"].ToString()))
{
CheckBoxList1.Items[i].Selected = Convert.ToBoolean( reader["isSelected"]);
}
}
This is the first thing i found when I googled how to programaticly select a item in the list.
Assuming that the items in your CheckedListBox are strings:
for (int i = 0; i < checkedListBox1.Items.Count; i++)
{
if ((string)checkedListBox1.Items[i] == value)
{
checkedListBox1.SetItemChecked(i, true);
}
}
Or
int index = checkedListBox1.Items.IndexOf(value);
if (index >= 0)
{
checkedListBox1.SetItemChecked(index, true);
}
This awnser was found on this post, and posted by wdavo.