I'm attempting to make a simple realtime search using a streamreader to read from a txt file and search and display the results in a listview, problem is I can only search for 1 letter, so searching for "1" will show me results for everything starting with 1, example search 1 results in "123", but searching for "12" or "123" wont show the same result. Easier explained with this code I've tried.
Edit, text-file I'm reading from has this structure:
123;asd;asd;asd;asd;asd;asd <- example of a row
public static string[] testtt(string sökord)
{
StreamReader asd = new StreamReader("film.txt");
string temp;
string[] xd;
while (asd.Peek() >= 0) // if I can read another row (I.E next row isnt empty)
{
temp = asd.ReadLine();
xd = temp.Split(';');
for (int i = 0; i < xd.Length; i++)
{
// this should check if my searchword is equal to any member of "xd"
// but this is where the problem occurs when the input is more than 1
// character, will post error message from debugger below this code.
if (xd[i].Substring(0, sökord.Length).ToLower() == sökord.ToLower())
return xd;
}
}
return null;
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
try
{
listView1.Items.Clear();
ListViewItem item = new ListViewItem(testtt(textBox1.Text)[0]);
item.SubItems.Add(testtt(textBox1.Text)[1]);
item.SubItems.Add(testtt(textBox1.Text)[2]);
item.SubItems.Add(testtt(textBox1.Text)[3]);
item.SubItems.Add(testtt(textBox1.Text)[4]);
item.SubItems.Add(testtt(textBox1.Text)[5]);
item.SubItems.Add(testtt(textBox1.Text)[6]);
listView1.Items.Add(item);
if (textBox1.Text == "")
listView1.Items.Clear();
}
catch (Exception ex)
{
//MessageBox.Show(ex.Message);
}
}
ex
{"Index and length must refer to a location within the string.\r\nParameter name: length"} System.Exception {System.ArgumentOutOfRangeException}
This is fairly simple. This error will always appear when the line you have read form the stream reader, and you split and store the value in xd. Say the length of xd is n. And the sokord string you entered has say m length. Now when you write:
(xd[i].Substring(0, sökord.Length)
whenever the length of xd that is n is less than m, the Substring function would be trying to make a substring of m letters from only n letters. And hence it gives the error you mentioned.
In any case just a simple check should do ok:
String sString = null;
if(xd[i].length>=sokord.length){
sString = xd[i].SubString(0,sokord.length).toLower();
if(sString.equals(sokord.toLower()))
return xd;
}
Digvijay
PS: To be honest I have written the answer from what best I could understand of what is trying to be done, so the code might be a little offtrack in one scenario. But in any case the error i have described above is 100% correct. So it would be best if you just look into that and follow the track. =)
Still dont know if I understood the question correctly, but wouldnt this be much easier to read and understand?
private String[] FindSome(String searchword)
{
foreach (String s in System.IO.File.ReadLines("myfile.txt"))
{
String[] tmp = s.Split('c');
foreach (String t in tmp)
{
if (t.StartsWith(searchword,StringComparison.CurrentCultureIgnoreCase)) return tmp;
}
}
return null;
}
Related
I am working o a project that turns message into ascii decimal values... this side is not important, the problem is it needs to read it back so the translation is basically like this:
if (textBox1.Text.Contains("a"))
{
textBox3.Text = textBox3.Text.Replace("a", "97");
}
if (textBox1.Text.Contains("b"))
{
textBox3.Text = textBox3.Text.Replace("b", "98");
}
.
.
.
if (textBox1.Text.Contains("Ğ"))
{
textBox3.Text = textBox3.Text.Replace("Ğ", "286");
}
if (textBox1.Text.Contains("ş"))
{
textBox3.Text = textBox3.Text.Replace("ş", "351");
}
this translation works perfect.
but translating back the output is the problem.
my translating back method in a nutshell:
if (sonmesajBinary.Text.Contains("97"))
{
okunanMesaj.Text = okunanMesaj.Text.Replace("97", "a");
}
if (sonmesajBinary.Text.Contains("98"))
{
okunanMesaj.Text = okunanMesaj.Text.Replace("98", "b");
}
if (sonmesajBinary.Text.Contains("99"))
{
okunanMesaj.Text = okunanMesaj.Text.Replace("99", "c");
}
and the problem is lets say the output is 140
but it also includes "40"
so pc gets it wrong. That's my problem, and i require your kind help:).
i am kinda noob so sorry for my mistakes and i am 17 also english is not my native language.
note: ascii values might not be the real ones, these are just for example.
There are many problems with your code there. Checking Contains will return true for any number of occurrences of a character at any location. You're checking in textBox1 and replacing in textBox3. You're checking each character known to you but it is possible there are more! There are easier ways of getting the byte/int/number equivalent of your character based on the encoding of your input.
Here's a rudimentary solution based on comments following the question. You however need to read more about code pages and then encodings. This is only part of the Encrypt operation. I'm sure you can figure out how to replace the contents and later also Decrypt to usable format. Cheers! Happy coding.
static void Main(string[] args)
{
string fileContents = "";
int encryptKey = 3; // Consider getting this from args[0], etc.
using (FileStream fs = File.OpenRead(#"C:\Users\My\Desktop\testfile.txt"))
using (TextReader tr = new StreamReader(fs))
{
fileContents = tr.ReadToEnd();
}
byte[] asciiBytesOfFile = Encoding.ASCII.GetBytes(fileContents);
int[] encryptedContents = Encrypt(encryptKey, asciiBytesOfFile);
}
private static int[] Encrypt(int encryptKey, byte[] asciiBytesOfFile)
{
int[] encryptedChars = new int[asciiBytesOfFile.Length];
for (int i = 0; i < asciiBytesOfFile.Length; i++)
{
encryptedChars[i] = encryptKey ^ asciiBytesOfFile[i];
}
return encryptedChars;
}
It was fixed thanks to Tom Blodget, all I needed to do was delimit. So I added 0 to beginning of every 2 digit values:D
if (textBox1.Text.Contains("a"))
{
textBox3.Text = textBox3.Text.Replace("a", "097");
}
So I need to count lines in a textbox, to do this i use:
if (subject.Length <= 20)
{
bool moreLines = true;
int onLine = 2;
int count = 0;
while (moreLines)
{
textBody[count] = TextBox_Text.Text.Split('\n')[onLine];
Console.WriteLine("Line saved: " + textBody[count]);
onLine++;
count++;
try
{
if (TextBox_Text.Text.Split('\n')[onLine] == null)
{
}
}
catch (IndexOutOfRangeException)
{
moreLines = false;
}
}
return true;
}
I insert the split strings into textBody[] array but once I approach the last lines where there is no text I want the loop to stop. I tried to do an if statement which checks if the next line is null, and if yes stop the loop. However, I kept getting an IndexOutOfRangeException so I just put the whole thing in a try catch, but I feel like there would be an easier way to do this?
I think you might have over complicated things massively.
The String.Split method have multiple overloads, some of them takes as an argument a member of the StringSplitOptions enum - one of it's members is called None, and the other is called RemoveEmptyEntries - so as far as I understand, all you need is this:
var textBody = TextBox_Text.Text.Split(
new char[] {'\n'},
StringSplitOptions.RemoveEmptyEntries);
An easy way to do this would just to use the following:
TextBox_Text.Text.Split('\n').Length
The Length property returns the length of the array.
so I just used the LineCount property instead and done a compare to the onLine
if (TextBox_Text.LineCount >= onLine)
{
moreLines = false;
}
So I am so fresh into the world of programming, starting new, I decided to start messing around in C# to create simple apps from ideas that come to mind, with this little app, I'm trying to have multiple TextBoxes named d1,d2,d3,d4,etc... the user inserts numbers into the textboxes then clicks button1, which begins the process in the code below creating a new list which contains all of the values of the textboxes and then the list is converted to an array and the array is then converted into an int array, etc....
BUT, when starting the application and I add values to the textboxes and then click button1, it shows 2 error like shows in the //gray code line below
Please help.
private void button1_Click(object sender, EventArgs e)
{
List<string> dodo = new List<string>();
dodo.Add(d1.Text); dodo.Add(d2.Text); dodo.Add(d3.Text); dodo.Add(d4.Text); dodo.Add(d5.Text);
dodo.Add(d6.Text); dodo.Add(d7.Text); dodo.Add(d8.Text); dodo.Add(d9.Text); dodo.Add(d10.Text);
dodo.Add(d11.Text); dodo.Add(d12.Text); dodo.Add(d13.Text); dodo.Add(d14.Text); dodo.Add(d15.Text);
dodo.Add(d16.Text); dodo.Add(d17.Text); dodo.Add(d18.Text); dodo.Add(d19.Text); dodo.Add(d20.Text);
foreach(string numb in dodo)
{
if (numb == "")
numb = "0"; //numb word has a red underline
}
string[] terms = dodo.ToArray();
int[] valv = {};
int x = 0;
for(int i=0;i<=19;i++)
{
valv[i] = int.Parse(terms[i]); //the ; in the end has a red underline and shows "FormatException was unhandled" error
i++;
x = x + valv[i];
}
string myString;
myString = x.ToString();
Result1.Text = myString;
}
you can't change the iteration variable which is numb in your case. Please change in the List container instead
List<string> dodo = new List<string>();
dodo.Add(d1.Text); dodo.Add(d2.Text); dodo.Add(d3.Text); dodo.Add(d4.Text); dodo.Add(d5.Text);
dodo.Add(d6.Text); dodo.Add(d7.Text); dodo.Add(d8.Text); dodo.Add(d9.Text); dodo.Add(d10.Text);
dodo.Add(d11.Text); dodo.Add(d12.Text); dodo.Add(d13.Text); dodo.Add(d14.Text); dodo.Add(d15.Text);
dodo.Add(d16.Text); dodo.Add(d17.Text); dodo.Add(d18.Text); dodo.Add(d19.Text); dodo.Add(d20.Text);
int k = 0;
foreach (string numb in dodo)
{
if (numb == "")
{
//numb = "0"; //numb word has a red underline
dodo[k] = "0";
}
k++;
}
Now your code on parsing into integer won't give any runtime error.
The first line "tells" you that you are not able to assign a new value to the variable which is used as a foreach iteration variable.
The second line, "tells" you that you have string value which is not able to be parsed correctly (e.g. user put string which is not a number). To avoid this you can use Int32.TryParse method instead, which will safely try to parse the given string.
The best and easiest way to achieve what you need is using LINQ methods, here is the example based on few things/assumptions:
Since you are converting empty strings into zeros, you could simply skip those entries from counting
To avoid FormatException, you should use TryParse method instead. Since TryParse method will safely parse the given string, you don't even have to filter empty strings at all (they will be skipped). However, I deliberately left filtering part, to get you a better overview of a solution.
You can use list initializer to make list initialization more readable
Solution:
List<string> dodo = new List<string>()
{
d1.Text, d2.Text //...others
};
int sum = dodo
.Where(item => !String.IsNullOrEmpty(item))
.Sum(item =>
{
if (Int32.TryParse(item, out int parsedItem))
{
return parsedItem;
}
return 0;
});
You can get more familiar with LINQ and used methods on following link
I have been working on a program that can calculate molecularweight from a chemical formula.
So far it works pretty well, until this error occured. I want to make an array that split the chemical formula (It works fine). From here I want to make a method call, that send the array to another method. Where it is used to calculate the molecularweight from the elements in the chemical formula, and add it to a new list. Then return the list to the first method. Here's is the code (Only showing for 1 element or code gets too large);
private void Textbox1_TextChanged(object sender, EventArgs e)
{
string input = Textbox1.Text;
string pattern = #"([A-Z][a-z]?\d*|[A-Z]?\d*)";
string[] substrings = Regex.Split(input, pattern);
MoleculeweightCalculator(substrings);
Textbox2.Text = Convert.ToString(MoleculeWeightList.Sum());
}
private static MoleculeweightCalculator(string[] substrings)
{
List<double> MoleculeWeightList = new List<double>();
foreach (string match in substrings)
{
if (match == "H")
{
MoleculeWeightList.Add(1.008);
}
}
return MoleculeWeightList;
}
The problem is that it won't allow me to send the array to the other method and returning the list to the first method again. Any ideas what is wrong? Or how to solve it?
There are many mistakes in your code, return has to be lowercase and you should place it out of the loop, then the return type is missing. Moreover you aren't storing the returned data.
This should at least compile:
private void Textbox1_TextChanged(object sender, EventArgs e){
string input = Textbox1.Text;
string pattern = #"([A-Z][a-z]?\d*|[A-Z]?\d*)";
string[] substrings = Regex.Split(input, pattern);
var result = MoleculeweightCalculator(substrings);
Textbox2.Text = Convert.ToString(result.Sum());
}
private static List<double> MoleculeweightCalculator(string[] substrings){
List<double> MoleculeWeightList = new List<double>();
foreach (string match in substrings){
if (match == "H")
MoleculeWeightList.Add(1.008);
}
return MoleculeWeightList;
}
You aren't actually using the return value. The MoleculeWeightList variable is local to that function, and more specifically, to that scope and can't be used outside of it. But because you return it, you can do this:
List<double> weightList = MoleculeweightCalculator(substrings);
Textbox2.Text = Convert.ToString(weightList.Sum());
That assigns the return value to a local variable in the calling method, which you can then use later in the method.
I'm working on a Hangman project that requires me to change characters "-" to "a to z". Since I'm learning how to code with C#, I have no clue how to do it.
I obviously need to use position because of the case where the word as duplicated letters (EX.: C oo kies)
Here's the code I developed, it makes my thing crashes and it's obviously incomplete.
private void chkA_Checked(object sender, RoutedEventArgs e)
{
if (motRechercher.Contains("a"))
{
int indexDemotRechercher = motRechercher.IndexOf("a");
int k = indexDemotRechercher;
var StringBuilderOP = new StringBuilder(motRechercher);
StringBuilderOP.Remove(indexDemotRechercher, indexDemotRechercher);
StringBuilderOP.Insert(k, "A");
}}
motRechercher is a STRING that I can use everywhere that I randomly pick from a list of 27 words. If this bother, it's a check-box and where I write the text is a Text-box(called txtMot).
Feel free to use other variables, I'll re-adapt after for my own comprehension. I would just like some explanation/examples to help my learning experience.
Here is the code of the randomiser if you really feel like this can help you understand[It works] :
private void btnDemarrer_Click(object sender, RoutedEventArgs e)
{
Random rdn = new Random();
int nbreAleatoire = rdn.Next(0, 27);
motRechercher = lesMots[nbreAleatoire];
if (motRechercher.Length > 0)
{
String str = new String('-', motRechercher.Length);
txtMot.Text = str;
}
}
QUESTION : How do I make a thing that detects duplicate and that will change the "-" to "a-z"?
Ask questions and I'll try to answer them if you think it's unclear.
Here is a quick sample... I have two strings... one for the hidden word the user does NOT see, and another for the one presented, using "-" or even "_" as place-holders for the actual characters.
I have a simple function "IsThereA" which expects a single letter as to the guess of a letter in the word. I then just call for all the letters including a few random ones. The function returns boolean so you can draw the hangman as each failure occurs.
In the "IsThereA" method, I am looking one character at a time for the guessed letter. If found, I replace it by using substring instead of the "-". So once updated, you can use the "WordUserCanSee" property however you need to.
This version doesn't do case-sensitive, but you can adjust as needed.
public class Hangman
{
string HangmanWord = "cookies";
string WordUserCanSee = "-------";
public Hangman()
{
IsThereA("o");
IsThereA("f");
IsThereA("k");
IsThereA("w");
IsThereA("i");
IsThereA("c");
IsThereA("s");
IsThereA("e");
}
public bool IsThereA(string guessLetter)
{
bool anyMatch = false;
for (int i = 0; i < HangmanWord.Length; i++)
{
if (HangmanWord.Substring(i, 1).Equals(guessLetter))
{
anyMatch = true;
WordUserCanSee = WordUserCanSee.Substring(0, i) + guessLetter + WordUserCanSee.Substring(i + 1);
}
}
return anyMatch;
}
}
motRechercher = motRechercher.Replace("-", "a-z");