Using the value found through '.contains' - c#

This is a console unit conversion program which takes in a measurement unit (such as pounds, ounces) specified by the user, followed by a second measurement unit, and finally a value to convert from one unit to the other.
string userUnit1, userUnit2 = "";
int userValue = 0;
The units that can be used are specified in a text file, the contents of which are split into a string array (called 'units') in the program. The text file also contains the 'conversion value' between the two units. In the text file, they appear like so: ounce,gram,28.0 (since there's 28 grams in one ounce, this value is also put into the array).
The units that the user enters are checked with the following snippet of code:
double result = 0;
if (units.Contains(userUnit1) && units.Contains(userUnit2))
{
//??? Something like:
//result = userValue */ value in the array element;
}
The basic IF statement above checks the array for the units that the user enters, and what I want to do is use the conversion value in that array element to convert the number that the user entered. How would I go about this?
EDIT: This is the code for storing the split text into the array.
using (read = new StreamReader("C:/Users/Sam Smith/Desktop/convert.txt"))
{
while (!read.EndOfStream)
{
lineFromFile = read.ReadLine();
lineFromFile.Split(' ');
foreach (int i in lineFromFile)
{
count++;
if (count % 3 == 0)
{
units.Add(lineFromFile);
}
}
}
}

Something like this?
String path = #"C:\Users\Sam Smith\Desktop\convert.txt";
var lines = System.IO.File.ReadLines(path)
.Where(l => l.Contains(userUnit1) && l.Contains(userUnit2));
if(lines.Any())
{
var unit = lines.First();
var parts = unit.Split(new char[]{','}, StringSplitOptions.RemoveEmptyEntries);
var result = userValue * double.Parse(parts[2]);
}
You need a reference to the System.Linq namespace for Enumerable.Where. You can use File.ReadLines to retrieve all lines in a file as IEnumerable<string>.

Use the IndexOf method into unit1Index and unit2Index and compare to -1 to assure they were found, or use the FirstOrDefault extension method and compare to null.
It also sounds like you should make a dictionay out of it, which would simplify this with TryGetValue.
var conversionsDic = File.ReadAllLines("conversions.txt")
.Select(l => l.Split(' '))
.ToDictionary(ls => ls[0] + ls[1], ls => double.Parse(ls[2]));
double conversion;
if (conversionsDic.TryGetValue(userUnit1 + userUnit2, out conversion))
{
// use 'conversion' to do whatever
var result = userValue * conversion;
}

Related

Checking Each Element in an Array to See if it is equal to a string

I have a number of elements in an array, I would like to check if a string is equal to any of these elements in the array. The number of elements in the array can change in number.
I have counted the number of elements in the array hoping to get somewhat of an advantage but haven't been able to come up with a solution.
int ArrayCount = FinalEncryptText.Count();
foreach (string i in FinalEncryptText)
{
}
Using the foreach implementation you have provided, you could include an if condition with String.Equals(string) - as Sean pointed out earlier.
But it's worth noting that String.Equals(string) without additional arguments is equivalent to using the == operator. So it's better if you specify the StringComparison type so that you express what kind of comparison you wish to perform.
For example, you could do something like this:
foreach (string element in myStringArray)
{
if(element.Equals("foo", StringComparison.CurrentCultureIgnoreCase))
...
}
You could even include the evaluation as a predicate in a LINQ query. For example, let's say you wanted to see which strings passed the evaluation:
var matches = myStringArray
.Where(element => element.Equals("foo", StringComparison.CurrentCultureIgnoreCase));
You can read more about comparing strings here.
I'm not sure what your method looks like, but I'm assuming.. you're given a random array of strings.. and you want to find a certain element in that array. Using a foreach loop:
public string Check(string[] FinalEncryptText)
{
foreach (string i in FinalEncryptText)
{
//let's say the word you want to match in that array is "whatever"
if (i == "whatever")
{
return "Found the match: " + i;
}
}
}
Using a regular for loop:
public string Check(string[] FinalEncryptText)
{
for (int i = 0; i < FinalEncryptText.Count; i++)
{
//let's say the word you want to match in that array is "whatever"
if (FinalEncryptText[i] == "whatever")
{
//Do Something
return "Found the match: " + FinalEncryptText[i];
}
}
}
Now if you already have a fixed array.. and you're passing in a string to check if that string exists in the array then it would go something like this:
public string Check(string stringToMatch)
{
for (int i = 0; i < FinalEncryptText.Count; i++)
{
//this will match whatever string you pass into the parameter
if (FinalEncryptText[i] == stringToMatch)
{
//Do Something
return "Found the match: " + FinalEncryptText[i];
}
}
}
You could use the String.Equals method in an if statement. More info on String.Method here: String.Equals Method.
if(firstString.Equals(secondString))
{
//whatever you need to do here
}

How to use Split() for making a command system in c#

I want to do something like this:
string a = Console.Readline();
string[] b = a.Split(' ');
string i = b[0];
string j = b[1];
Now the problem is, putting the 'string j' may be optional like the input may be hi hello here hello is optional. How to make the code work if someone doesn't put something in place of hello.
Thanks in advance.
You could use the Length property to check how many elements are in the split array.
If there are not enough elements in the array to assign the optional value you can set it to null.
In the rest of your code you just have to null-check the optional value before using it.
Something liket his would work:
string input = Console.ReadLine();
string[] tokens = input.Split(' ');
string hi = tokens[0];
string optional = tokens.Length > 1 ? tokens[1] : null; // make sure we have enough elements
Console.WriteLine(hi);
// null check before using the optional value
if (optional != null)
Console.WriteLine(optional);
// or
Console.WriteLine(optional ?? "Optional value is null..");
Instead of accessing the arrays particular element by its index position, I would use foreach loop to iterate over a list like:
string a = Console.ReadLine();
string[] b = a.Split(' ');
foreach (string elem in b)
{
Console.WriteLine(elem); // Do whatever you want with each element
}
Console.ReadLine();
Since the "commands" entered by the user will be stored in the array (e.g. b based on your code) after the split, I don't think it's necessary to store them in individual variables yourself. Thus, avoiding the problem you have in your current setup. On the other hand, if you want to see if a specific "command" was keyed in, you can do something like this:
static void Main(string[] args)
{
Console.Write("> ");
string input = Console.ReadLine();
// Doing it like this will automatically remove blanks from the resulting array
// so you won't have to clean it up yourself
string[] commands = input.Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries);
// Contains is from the System.Linq namespace
// this will allow you to see if a given value is in the array
if (commands.Contains("hi"))
{
Console.WriteLine("> The command 'hi' has been received.");
}
Console.Read();
}
You can use Linq's Contains method to check if a specific value exists in the array of command strings.
if you just want to see all the commands in the array, a simple for loop would be enough.
// The Length property of the array will give you the
// number of items it contains
for(int i = 0; i < commands.Length; i++)
{
Console.WriteLine("> Command read: {0} ", commands[i]);
}
One more thing, I suggest that you normalize the inputs your application will receive as to avoid problems when filtering through them. You could do this by calling the ToLower method available to ReadLine:
string inputs = Console.ReadLine().ToLower();
Happy coding :)

how to convert a String list into a String array then converting it into an int array then counting the total sum of the numbers in the array?

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

Checking if a value is in an array and then using that value

I have a method which recieves an array of strings from another method.
This array contains several strings, for days, moviegenres etc.
I need to check if the array contains any of the moviegenres, and if so I need that specific value.
here is what I have now:
if (eventsSelected.Contains("act") ||
eventsSelected.Contains("adv") ||
eventsSelected.Contains("ani") ||
eventsSelected.Contains("doc") ||
eventsSelected.Contains("dra") ||
eventsSelected.Contains("hor") ||
eventsSelected.Contains("mys") ||
eventsSelected.Contains("rom") ||
eventsSelected.Contains("sci") ||
eventsSelected.Contains("thr"))
{
//get the value that is in the array contains.
}
Because my code checks for 10 different values how can I find out which value is true?
So let's say the array contains the value "act" how can I get that specific value?
foreach(var match in new [] {"act", "adv"}.Where(eventsSelected.Contains))
{
//do stuff
}
Or if you just need the first one
var match = Array.Find(new[] { "act", "adv" }, eventsSelected.Contains);
if (!string.IsNullOrEmpty(match))
{
// If you just want the first match
}
The match field contains the token you were searching on, so act or adv.
Either use multiple if or use a string[] and Array.FindIndex:
string[] tokens = {"adv", "ani", "doc" .... };
int index = Array.FindIndex(tokens, t => eventsSelected.Contains(t));
if(index >= 0)
{
Console.WriteLine("First value found was: " + tokens[index])
}
You can use Intersect
var values = new[] {"act", "adv", "ani", etc};
var matches = values.Intersect(eventsSelected);
//matches contains all matching values
Similar to the answer from Tim, but using LINQ rather than Array functions:
string[] tokens = {"adv", "ani", "doc" .... };
string firstMatch = eventsSelected.FirstOrDefault(s => tokens.Contains(s));
if (firstMatch != null)
{
// Do something with firstMatch
}
I couldn't help but notice that you've written your tokens in sorted order so just for the fun of it you could do:
string[] sortedTokens = {"act", "adv", "ani", ... };
int index = Array.FindIndex(eventsSelected, e => Array.BinSearch(sortedTokens, e) > 0)
And then proceed as in Tim's answer, except that this time, index is the index from eventsSelected. This gives you O(nlog(k)) time complexity, where n is the size of eventsSelected and k is the size of tokens (other answers give O(nk), not that it matters much).

C#: Get an integer representation of a string array

I am new to C# and I ran into the following problem (I have looked for a solution here and on google but was not successful):
Given an array of strings (some columns can possibly be doubles or integers "in string format") I would like to convert this array to an integer array.
The question only concerns the columns with actual string values (say a list of countries).
Now I believe a Dictionary can help me to identify the unique values in a given column and associate an integer number to every country that appears.
Then to create my new array which should be of type int (or double) I could loop through the whole array and define the new array via the dictionary. This I would need to do for every column which has string values.
This seems inefficient, is there a better way?
In the end I would like to do multiple linear regression (or even fit a generalized linear model, meaning I want to get a design matrix eventually) with the data.
EDIT:
1) Sorry for being unclear, I will try to clarify:
Given:
MAKE;VALUE ;GENDER
AUDI;40912.2;m
WV;3332;f
AUDI;1234.99;m
DACIA;0;m
AUDI;12354.2;m
AUDI;123;m
VW;21321.2;f
I want to get a "numerical" matrix with identifiers for the the string valued columns
MAKE;VALUE;GENDER
1;40912.2;0
2;3332;1
1;1234.99;0
3;0;0
1;12354.2;0
1;123;0
2;21321.2;1
2) I think this is actually not what I need to solve my problem. Still it does seem like an interesting question.
3) Thank you for the responses so far.
This will take all the possible strings which represent an integer and puts them in a List.
You can do the same with strings wich represent a double.
Is this what you mean??
List<int> myIntList = new List<int>()
foreach(string value in stringArray)
{
int myInt;
if(Int.TryParse(value,out myInt)
{
myIntList.Add(myInt);
}
}
Dictionary is good if you want to map each string to a key like this:
var myDictionary = new Dictionary<int,string>();
myDictionary.Add(1,"CountryOne");
myDictionary.Add(2,"CountryTwo");
myDictionary.Add(3,"CountryThree");
Then you can get your values like:
string myCountry = myDictionary[2];
But still not sure if i'm helping you right now. Do you have som code to specify what you mean?
I'm not sure if this is what you are looking for but it does output the result you are looking for, from which you can create an appropriate data structure to use. I use a list of string but you can use something else to hold the processed data. I can expand further, if needed.
It does assume that the number of "columns", based on the semicolon character, is equal throughout the data and is flexible enough to handle any number of columns. Its kind of ugly but it should get what you want.
using System;
using System.Collections.Generic;
using System.Linq;
namespace ConsoleApplication3
{
class StringColIndex
{
public int ColIndex { get; set; }
public List<string> StringValues {get;set;}
}
class Program
{
static void Main(string[] args)
{
var StringRepresentationAsInt = new List<StringColIndex>();
List<string> rawDataList = new List<string>();
List<string> rawDataWithStringsAsIdsList = new List<string>();
rawDataList.Add("AUDI;40912.2;m");rawDataList.Add("VW;3332;f ");
rawDataList.Add("AUDI;1234.99;m");rawDataList.Add("DACIA;0;m");
rawDataList.Add("AUDI;12354.2;m");rawDataList.Add("AUDI;123;m");
rawDataList.Add("VW;21321.2;f ");
foreach(var rawData in rawDataList)
{
var split = rawData.Split(';');
var line = string.Empty;
for(int i= 0; i < split.Length; i++)
{
double outValue;
var isNumberic = Double.TryParse(split[i], out outValue);
var txt = split[i];
if (!isNumberic)
{
if(StringRepresentationAsInt
.Where(x => x.ColIndex == i).Count() == 0)
{
StringRepresentationAsInt.Add(
new StringColIndex { ColIndex = i,
StringValues = new List<string> { txt } });
}
var obj = StringRepresentationAsInt
.First(x => x.ColIndex == i);
if (!obj.StringValues.Contains(txt)){
obj.StringValues.Add(txt);
}
line += (string.IsNullOrEmpty(line) ?
string.Empty :
("," + (obj.StringValues.IndexOf(txt) + 1).ToString()));
}
else
{
line += "," + split[i];
}
}
rawDataWithStringsAsIdsList.Add(line);
}
rawDataWithStringsAsIdsList.ForEach(x => Console.WriteLine(x));
Console.ReadLine();
/*
Desired output:
1;40912.2;0
2;3332;1
1;1234.99;0
3;0;0
1;12354.2;0
1;123;0
2;21321.2;1
*/
}
}
}

Categories

Resources