Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
1. Add an entry to the book.
Command syntax: add firstName lastName month day year Each entry in the birthday book consists of a first name, a last name, and a date. A date consists of a month, a day, and a year. You do not need to verify that the month, day, and year are sensible values. You may NOT assume that the month, day, and year are given as integers, and you must require the user to give you integers. Example ("> " is the prompt):
> add Natalie Hershlag 6 9 1981
Added "Natalie Hershlag, 6/9/1981" to birthday book.
> add William Pitt 12 18 1963
Added "William Pitt, 12/18/1963" to birthday book.
> add John Depp 6 9 1963
Added "John Depp, 6/9/1963" to birthday book.
> add Bono
Error: unable to add birthday to book. Add requires 5 arguments.
> add Paul Hewson May 10 1960
Error: unable to add birthday to book. Please use integers for dates.
2. List all entries in the book.
Command syntax: list
Prints out a list of all birthday book entries, numbered, one per line. Example:
> list
1. Madonna Ciccone, 8/16/1958
2. Natalie Hershlag, 6/9/1981
3. William Pitt, 12/18/1963
4. John Depp, 6/9/1963
3. Delete an entry in the book by number.
Command syntax: delete number
You may NOT assume that the user will always give an integer as the number that they want to delete. After
checking to see that the entry number is valid, the program should verify that the user really wants to delete the
entry by asking for a "y" or a "n", which mean "yes" or "no" respectively. You should continue to prompt for a
"y" or a "n" until you get one or the other. Example:
> list
1. Madonna Ciccone, 8/16/1958
2. Natalie Hershlag, 6/9/1981
3. William Pitt, 12/18/1963
4. John Depp, 6/9/1963
> delete 3
Really delete William Pitt from the birthday book? (y/n) y
> list
1. Madonna Ciccone, 8/16/1958
2. Natalie Hershlag, 6/9/1981
3. John Depp, 6/9/1963
> delete 4
I'm sorry, but there is no such entry in the book.
> delete 1
Really delete Madonna Ciccone from the birthday book? (y/n) nada
Please enter "y" or "n". (y/n) n
> list
1. Madonna Ciccone, 8/16/1958
2. Natalie Hershlag, 6/9/1981
3. John Depp, 6/9/1963
How would I go about starting this question? I need to incorporate the use of classes as well? I don't need specific code, but rather a general point on how to start this.
Would I start by putting each function (add, list, delete) into a different class to deal with them and having a common array in the main class?
In the spirit of helping, I'll give you a starting point:
Console.ReadLine <-- gives you inputs from what they typed in
You will need some classes, for example:
public class Person {
public string FirstName;
public string LastName;
public Date BirthDate;
}
Then you're going to want to have a main class that does some sort of printing with lines like
Console.WriteLine("Gimme stuffs");
and maybe even some
foreach(var p in People){
Console.WriteLine("Found person {0} {1}, {2}",p.FirstName, p.LastName, p.BirthDate);
}
and somewhere in that main class you'll have something like
List<Person> People = new List<Person>();
Validation:
I would make some sort of a thing like:
public static Person GetPerson(string str){
var all = str.Split(' ');
var p = new Person();
p.FirstName = all[0];
p.LastName = all[1];
int month = (int)all[2];
int day = (int)all[3];
int year = (int)all[4];
p.BirthDate = new Date(year,month,day);
return p;
}
and for a fast validation:
if(str.Split(' ').Length != 5) //you don't have a valid input line
but then also
int testInt = 0;
if(!int.TryParse(str.Split(' ')[3], out testInt)) //you don't have a valid month
Now, that's a lot of help. Go forth and TRY FOR YOURSELF.
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I'm receiving data from a third party device. I need to extract two pieces of information. I think I need to use a Regular Expression, but I don't know anything of this.
Below you can find a few example strings:
TN 12 1 17:45:19.90400 7173
TN 4 4 17:45:20.51800 7173
TN 13 1 17:45:24.03200 7173
TN 5 4 17:45:26.06300 7173
TN 6 4 17:45:29.28700 7173
TN 14 1 17:45:31.03200 7173
From each of these strings I need to extract two pieces of data:
the time
the number before the time
So the data I'm looking for is this:
1 and 17:45:19.90400
4 and 17:45:20.51800
1 and 17:45:24.03200
4 and 17:45:26.06300
4 and 17:45:29.28700
1 and 17:45:31.03200
The number will always be present and it will always be 1, 2, 3 or 4.
The time will also be the same format but I'm not sure if there will be single digit hours. So I don't know if 9 o'clock will be displayed as
9 or 09
Any suggestions on how I can extract this using a RegEx?
Thanks
My usual approach to this is to create a class that represents the data we want to capture, and give it a static Parse method that takes in an input string and returns an instance of the class populated with data from the string. Then we can just loop through the lines and populate a list of our custom class with data from each line.
For example:
class TimeData
{
public TimeSpan Time { get; set; }
public int Number { get; set; }
public static TimeData Parse(string input)
{
var timeData = new TimeData();
int number;
TimeSpan time;
if (string.IsNullOrWhiteSpace(input)) return timeData;
var parts = input.Split(new char[0], StringSplitOptions.RemoveEmptyEntries);
if (parts.Length > 2 && int.TryParse(parts[2], out number))
{
timeData.Number = number;
}
if (parts.Length > 3 && TimeSpan.TryParseExact(parts[3], "hh\\:mm\\:ss\\.fffff",
CultureInfo.CurrentCulture, out time))
{
timeData.Time = time;
}
return timeData;
}
}
Now we can just loop through the list of strings, call Parse on each line, and end up with a new list of objects that contain the Time and associated Number for each line. Also note that, by using a TimeSpan to represent the time, we now have properties for all the parts, like Hour, Minute, Seconds, Milliseconds, TotalMinutes, etc:
var fileLines = new List<string>
{
"TN 12 1 17:45:19.90400 7173",
"TN 4 4 17:45:20.51800 7173",
"TN 13 1 17:45:24.03200 7173",
"TN 5 4 17:45:26.06300 7173",
"TN 6 4 17:45:29.28700 7173",
"TN 14 1 17:45:31.03200 7173",
};
List<TimeData> allTimeData = fileLines.Select(TimeData.Parse).ToList();
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
In my code I get a string as input from the user and return to him a list of names(strings) that consist inside themselves the users input.
I wish to sort this list by the index of the input in the first name and last name, with priority to the first name. example:
User input string: Da
desired result:
"David Gal"
"American Dad"
"Adam Jones"
example explanation:
"David Gal" is first because the index of "Da" is 0 in the first name.
"America Dad" is second because "Da" is also first in the last name but last names have lesser priority than first names when the index is equal.
"Adam Jones" is last because "Da" has the lowest index appearance in his name.
Edit:
I've found the answer but I don't have enough reputation to answer myself so here it is:
listToSort.OrderBy(contact =>
contact.Name
.Split(' ')
.Select((WordInName, WordIndex) =>
(uint)WordInName.IndexOf(SearchString, StringComparison.CurrentCultureIgnoreCase) + WordIndex / ((double)WordIndex + 1)
)
.Min()
);
Assuming your list looks like this:
var input = new List<string>
{
"David Gal",
"American Dad",
"Adam Jones"
};
This will give you a sorted set:
var search = "da";
var results = input
.OrderBy(w =>
w.Split(' ')
.Select(x => x.ToLower().IndexOf(search)).Max());
I have a tab-separated text file like this:
customerNo Offer Score
1 1 0.273
2 1 0.630
3 1 0.105
4 1 0.219
5 1 0.000
6 1 0.303
7 1 0.760
I have a string array in my program that contains all the lines in this text file.
Using LINQ, I first would like to get rid of any lines that have non-numerical characters (like the header line above) or are empty and then would like to save the other lines as a List of Objects. Here my object would be something called ScoreItem that has properties: customerNo, Offer and Score. So eventually I get 7 of these objects from this file.
In your very case I would do this:
File.ReadAllLines("the-file-full-path")
.Select(x => x.Split('\t'))
.Where(x =>
{
int i;
return int.TryParse(x[0], out i);
})
.Select(x => new ScoreItem
{
CustomerNo = int.Parse(x[0]),
Offer = int.Parse(x[1]),
Score = double.Parse(x[2])
});
And consider using .ToArray() or .ToList() at the end to prevent possible reenumerations of that block in further code.
Updated:
The code provided is straight-forward: it does not consider any additional checks for data format culture etc. To be sure the number are always parsed independently on current user's culture setup, for double parsing must be used double.Parse(x[2], CultureInfo.InvariantCulture) (for instance), instead.
I'd like a group of 3 values in the following regular expression and input string
With the help of the SO experts this is what I have:
string item = "strawb bana 1 10 1.93";
string pattern = #"(?<str>[\w\s]*)(?<qty>\s\d*\s)(?<num>\d*\.\d+)";
Basically,
The first value is going to be the product description. I put a 1 on the end just in case the description has a number in it.
The second value is the quantity.
The third value is price.
Does this look correct? Might I be missing other cases?
Result should be the following
Group 1 = "strawb bana 1"
Group 2 = "10"
Group 3 = "1.93"
It looks like you forgot to include digits in your first match.
string item = "strawb bana 1 10 1.93";
string pattern = #"(?<str>[\w\s]*)(?<qty>\s\d*\s)(?<num>\d*\.\d+)";
Should be:
string item = "strawb bana 1 10 1.93";
string pattern = #"(?<str>[\w\s\d]*)(?<qty>\s\d*\s)(?<num>\d*\.\d+)";
I've a string, In sql I've tested this string contains non-printable characters but I couldn't find which are those.
So can anyone suggest me how can i find and print those non-printable characters in C#
by passing this string?
I ordered a Taurus in August and it hasn't been scheduled, I was
just wondering if there is something wrong with the order? Or if the
queue is just long? Order Number:8028Vehicle Rep:74JTag#:
200L049Description: 2011 TAURUS FWD SELOrdered: 08-AUG- 2010VIN
Assigned:VIN#:Scheduled:In
Production:Produced:Invoiced:Released:Shipped:Ready:Tax
Location: STATE OF MICHIGAN (20 )State Insured:USOB Status:050 -
CLEAN UNSCHEDULED ORDER
As i pasted the string in notepad++ it shows like this.
You can use char.IsControl(c) to test whether a character is a control (non-printable) character or not:
foreach (var c in str)
{
if (char.IsControl(c))
{
Console.WriteLine("Found control character: {0}", (int)c);
}
}
I wrote a program with some help from google and SO.
this program will print the ascii value and the position of character which is non-printable character.
class Program
{
static void Main(string[] args)
{
string text = #"I am an FMCC employee located in the Chicagoland area that currently has 2 available/active ManagementLease tags (non-incremental) and 1 currently utilized Sales Vehicle tag. I have a 2009 Sales vehicle now andhave ordered a replacement for that vehicle (2010 Taurus) per our current direction. I have not had a 2009 Model vehicle for either of my Management Lease tags in 2009. I was married in August of this year andordered a 2010 Flex on one of my Management Lease tags in September.My issue is that my wife's current vehicle is no longer serviceable, and the 2010 Flex has yet to be scheduled tobe built.My exception request is that I be allowed to take delivery and assume payments for a 2010 Taurus that is at alocal dealership and displayed on the ""Vehicles Available Now"" page on an interim basis until my 2010 Flex isdelivered. I appreciate that typically an employee cannot have two vehicles from the same model year on agiven Management Lease tag, but I was hoping an exception could be made due to my recent marriage andthe fact that I did not have a 2009 model year vehicle on any tags.";
for (int i = 0; i < text.Length; i++)
{
if (Char.ConvertToUtf32(text, i) < 32)
{
Console.WriteLine( "position " + i + " " + text[i] + " => " + Char.ConvertToUtf32(text, i));
}
}
Console.ReadLine();
}
}