Extract decimal/ integer values before a character sequence in C# - c#

I want to extract all decimal/ integer value before a character sequence(am/AM) until an alphabet or a special charecter comes up value
Input Output
PEK\n2545 AMAzerbhaijan 2545
PEK\n2545 AMamorphous 2545
ANGwwquui\3.0 amAm 3.0
Awyu&&#^/Non-Prog//#*(*889/328.19 am -> 328.19
qii2781a/k28U28am 28
PEK\nam2545 AM 2545
Can I know what is the best possible way to do this? Thanks in advance.

Given the examples you showed, I believe this would do the trick:
public string ParseData(string input)
{
StringBuilder numberBuilder = new StringBuilder();
string terminatorChars = "am";
bool isCaseSensitive = false;
int terminatorCharLength = terminatorChars.Length;
for (int i = 0; i < input.Length; i++)
{
if (input.Length - i >= terminatorCharLength)
{
var currentSubstr = input.Substring(i, terminatorCharLength);
if ((currentSubstr == terminatorChars) || (!isCaseSensitive && currentSubstr.ToLowerInvariant() == terminatorChars.ToLowerInvariant()))
if(numberBuilder.Length>0)
return numberBuilder.ToString();
}
if (Char.IsDigit(input[i]) || input[i] == '.')
numberBuilder.Append(input[i]);
else if (Char.IsWhiteSpace(input[i]))
continue;
else
numberBuilder.Clear();
}
return null;
}

The data is fixed width data with name the first 50 character and value the data after column 50. See code below
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace ConsoleApplication1
{
class Program
{
const string FILENAME = #"c:\temp\test.txt";
static void Main(string[] args)
{
StreamReader reader = new StreamReader(FILENAME);
string line = "";
List<KeyValuePair<string, decimal>> data = new List<KeyValuePair<string,decimal>>();
int lineNumber = 0;
while((line = reader.ReadLine()) != null)
{
if(++lineNumber > 1)
{
string key = line.Substring(0, 50).Trim();
decimal value = decimal.Parse(line.Substring(50));
data.Add(new KeyValuePair<string,decimal>(key,value));
}
}
}
}
}

Related

Check if string contains all required characters

I need to import a number of characters in a string format with a comma and check if a string is valid by containing all of them. The string must be in a format like that "AB2345CD" . This is the code I have for now but i dont know how to check if it is valid by containing all of the input characters and every digit between 0 and 9.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace License_Plates
{
class Program
{
static void Main(string[] args)
{
string input = Console.ReadLine();
var allowedCharacters = input.ToList();
int platesToValidate = int.Parse(Console.ReadLine());
List<string> plateNumbers = new List<string>();
for (int i = 0; i < platesToValidate; i++)
{
string plate = Console.ReadLine();
plateNumbers.Add(plate);
}
List<string> validNumbers = new List<string>();
foreach (var number in plateNumbers)
{
if (number.Contains(allowedCharacters.ToString()))
{
validNumbers.Add(number);
}
}
for (int i = 0; i < validNumbers.Count; i++)
{
Console.WriteLine(i);
}
}
}
}
Check if string contains all required characters
you could use the linq extension All:
var allowedstring = "A,B,C,D";
var allowednumber = "0,1,2,3,4,5,6,7,8,9";
var test1 = "ABC0";
var test2 = "AZ9";
var allowed = $"{allowedstring}{allowednumber}".Replace(",", "");
Console.WriteLine(test1.All(c => allowed.Contains(c))); //True
Console.WriteLine(test2.All(c => allowed.Contains(c))); //False

Character Counter using Array List

I need to design a program that reads in an ASCII text file and creates an output file that contains each unique ASCII character and the number of times it appears in the file. Each unique character in the file must be represented by a character frequency class instance. The character frequency objects must be stored in an array list. My code is below:
using System.IO;
using System;
using System.Collections;
namespace ASCII
{
class CharacterFrequency
{
char ch;
int frequency;
public char getCharacter()
{
return ch;
}
public void setCharacter(char ch)
{
this.ch = ch;
}
public int getfrequency()
{
return frequency;
}
public void setfrequency(int frequency)
{
this.frequency = frequency;
}
static void Main()
{
string OutputFileName;
string InputFileName;
Console.WriteLine("Enter the file path");
InputFileName = Console.ReadLine();
Console.WriteLine("Enter the outputfile name");
OutputFileName = Console.ReadLine();
StreamWriter streamWriter = new StreamWriter(OutputFileName);
string data = File.ReadAllText(InputFileName);
ArrayList al = new ArrayList();
//create two for loops to traverse through the arraylist and compare
for (int i = 0; i < data.Length; i++)
{
int k = 0;
int f = 0;
for (int j = 0; j < data.Length; j++)
{
if (data[i].Equals(data[j]))
{
f++;
if (i > j) { k++; }
}
}
al.Add(data[i] + "(" + (int)data[i] + ")" + f + " ");
foreach (var item in al)
{
streamWriter.WriteLine(item);
}
}
streamWriter.Close();
}
}
}
When I run the program, the program does not stop running and the output file keeps getting larger until it eventually runs out memory and I get an error stating that. I am not seeing where the error is or why the loop won't terminate. It should just count the characters but it seems to keep looping and repeating counting the characters. Any help?
Try this approach :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace yourNamespace
{
class Char_frrequency
{
Dictionary<Char, int> countMap = new Dictionary<char, int>();
public String getStringWithUniqueCharacters(String input)
{
List<Char> uniqueList = new List<Char>();
foreach (Char x in input)
{
if (countMap.ContainsKey(x))
{
countMap[x]++;
}
else
{
countMap.Add(x, 1);
}
if (!uniqueList.Contains(x))
{
uniqueList.Add(x);
}
}
Char[] uniqueArray = uniqueList.ToArray();
return new String(uniqueArray);
}
public int getFrequency(Char x)
{
return countMap[x];
}
}
}
This might not be the ideal solution. But you can use these methods

Null pointer Exception in static array C#

I am initializing static array by a method, then want to use it in non static method and it throws nullpointerException. When I have created small example everything worked. I don't know what is wrong with that. Attaching solution.
http://www.speedyshare.com/QRjW5/Funkcjonalnosc-Kopia-2.zip
Main method is in the class
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Funkcjonalnosc
{
class Dzwiek
{
static Dzwiek[] tabRefDzwiekow;// = zaladujDzwieki();//przy uruchomeniu dzwieki referencyjne wczytaja sie raz
double hz;
String dzwiek;
bool first = true;
Dzwiek(double hz) {
this.hz = hz;
dzwiek = dopasujDzwiek(hz);
}
Dzwiek(String dzwiek, double hz) {
this.dzwiek = dzwiek;
this.hz = hz;
}
public static void zaladujDzwieki() {
System.IO.StreamReader sr = System.IO.File.OpenText("dzwieki.txt");
tabRefDzwiekow = new Dzwiek[100];
string s = "";
int i = 0;
string[] splitted;
while ((s = sr.ReadLine()) != null) {
splitted = s.Split('\t');
tabRefDzwiekow[i] = new Dzwiek(splitted[0], Double.Parse(splitted[1]));
Console.WriteLine(tabRefDzwiekow[i].hz);
}
sr.Close();
}
//Znajduje odpowiedni dzwiek w tablicy dzwiekow
String dopasujDzwiek(double hz) {
double obecnaRoznica, poprzedniaRoznica = int.MaxValue;
string dopasowanyDzwiek = "";
for (int i = 0; i < tabRefDzwiekow.Length; i++) {
obecnaRoznica = Math.Abs(hz - tabRefDzwiekow[i].hz);//THROWS EXCEPTION!
if (obecnaRoznica > poprzedniaRoznica)
return tabRefDzwiekow[i - 1].dzwiek;
poprzedniaRoznica = obecnaRoznica;
}
return dopasowanyDzwiek;
}
static void Main(string[] args) {
zaladujDzwieki(); //initilize the static ARRAY tabRefDzwieki
Dzwiek dzwiek = new Dzwiek(440); //uses that array by calling function in //constructor doPasujDzwieki()
Console.Read();
}
}
}
unless I'm mising something, whenever you call
tabRefDzwiekow[i] = new Dzwiek(splitted[0], Double.Parse(splitted[1]));
i will always be zero, and then when you access it later, in your for loop,
obecnaRoznica = Math.Abs(hz - tabRefDzwiekow[i].hz);//THROWS EXCEPTION
you're trying to access an uninitialized Dzwiek object's hz property after the first iteration

Text file only reading odd OR even lines?

I'm trying to write a little C# program that reads from a text file and lets you choose a line to print out.
For some reason, it will only print lines 1,3,5,etc.
If I change the bit that says int chooseLine = Convert.ToInt32(input); to int chooseLine = (int)Convert.ToInt64(input);, then it only prints even lines.(0,2,4,6,etc).
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
class Steve
{
public static int count = 0;
public static String[] steveTalk;
public static void Main()
{
using (StreamReader r = new StreamReader("Steve.txt"))
{
string line;
while ((line = r.ReadLine()) != null)
{
count++;
}
}
using (StreamReader sr = new StreamReader("Steve.txt"))
{
int i = 0;
steveTalk = new String[count];
String line;
while ((line = sr.ReadLine()) != null)
{
steveTalk[i] = line;
Console.WriteLine(steveTalk[i]);
i++;
}
}
while (true)
{
string input = Console.ReadLine();
int chooseLine = Convert.ToInt32(input);
try
{
Console.WriteLine(steveTalk[chooseLine]);
}
catch
{
Console.WriteLine("Error! Not a number or array index out of bounds");
}
Console.ReadLine();
}
}
}
Any ideas?
I'd like to suggest the System.IO.File.ReadAllLines(filename) method.
string []lines=System.IO.File.ReadAllLines("Steve.txt");
string input ;
while ((input = Console.ReadLine()) != "end")
{
int chooseLine;
int.TryParse(input,out chooseLine);
if(chooseLine<lines.Length)
{
Console.WriteLine(lines[chooseLine]);
}
}
There is no such problem with your code. What you might experience is that you have a Console.ReadLine() at the end of your loop, so if you enter a number it will show that line, then enter another number, that number will be ignored. Every other number that you enter will be ignored, which fits your description if you only tried to enter the numbers in sequence.
Here is some improvements to the code.
Use File.ReadAlLines to read the file.
Don't use exceptions unless you need it. You can easily check the input before any exception occurs.
Code:
using System;
using System.IO;
class Steve {
public static void Main() {
string[] lines = File.ReadAllLines("Steve.txt");
while (true) {
int line;
if (Int32.TryParse(Console.ReadLine(), out line)) {
if (line >= 0 && line < lines.Length) {
Console.WriteLine(lines[chooseLine]);
} else {
Console.WriteLine("Error! Array index out of bounds");
}
} else {
Console.WriteLine("Error! Not a number");
}
}
}
}

getting the best record from a file

I have a file with the following text inside
mimi,m,70
tata,f,60
bobo,m,100
soso,f,30
I did the reading from file thing and many many other methods and functions, but how I can get the best male name and his grade according to the grade.
here is the code I wrote. Hope it's not so long
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
namespace practice_Ex
{
class Program
{
public static int[] ReadFile(string FileName, out string[] Name, out char[] Gender)
{
Name = new string[1];
int[] Mark = new int[1];
Gender = new char[1];
if (File.Exists(FileName))
{
FileStream Input = new FileStream(FileName, FileMode.Open, FileAccess.Read);
StreamReader SR = new StreamReader(Input);
string[] Current;
int Counter = 0;
string Str = SR.ReadLine();
while (Str != null)
{
Current = Str.Split(',');
Name[Counter] = Current[0];
Mark[Counter] = int.Parse(Current[2]);
Gender[Counter] = char.Parse(Current[1].ToUpper());
Counter++;
Array.Resize(ref Name, Counter + 1);
Array.Resize(ref Mark, Counter + 1);
Array.Resize(ref Gender, Counter + 1);
Str = SR.ReadLine();
}
}
return Mark;
}
public static int MostFreq(int[] M, out int Frequency)
{
int Counter = 0;
int Frequent = 0;
Frequency = 0;
for (int i = 0; i < M.Length; i++)
{
Counter = 0;
for (int j = 0; j < M.Length; j++)
if (M[i] == M[j])
Counter++;
if (Counter > Frequency)
{
Frequency = Counter;
Frequent = M[i];
}
}
return Frequent;
}
public static int Avg(int[] M)
{
int total = 0;
for (int i = 0; i < M.Length; i++)
total += M[i];
return total / M.Length;
}
public static int AvgCond(char[] G, int[] M, char S)
{
int total = 0;
int counter = 0;
for (int i = 0; i < G.Length; i++)
if (G[i] == S)
{
total += M[i];
counter++;
}
return total / counter;
}
public static int BelowAvg(int[] M, out int AboveAvg)
{
int Bcounter = 0;
AboveAvg = 0;
for (int i = 0; i < M.Length; i++)
{
if (M[i] < Avg(M))
Bcounter++;
else
AboveAvg++;
}
return Bcounter;
}
public static int CheckNames(string[] Name, char C)
{
C = char.Parse(C.ToString().ToLower());
int counter = 0;
string Str;
for (int i = 0; i < Name.Length - 1; i++)
{
Str = Name[i].ToLower();
if (Str[0] == C || Str[Str.Length - 1] == C)
counter++;
}
return counter;
}
public static void WriteFile(string FileName, string[] Output)
{
FileStream FS = new FileStream(FileName, FileMode.OpenOrCreate, FileAccess.Write);
StreamWriter SW = new StreamWriter(FS);
for (int i = 0; i < Output.Length; i++)
SW.WriteLine(Output[i]);
}
static void Main(string[] args)
{
int[] Mark;
char[] Gender;
string[] Name;
string[] Output = new string[8];
int Frequent, Frequency, AvgAll, MaleAvg, FemaleAvg, BelowAverage, AboveAverage, NamesCheck;
Mark = ReadFile("c:\\IUST1.txt", out Name, out Gender);
Frequent = MostFreq(Mark, out Frequency);
AvgAll = Avg(Mark);
MaleAvg = AvgCond(Gender, Mark, 'M');
FemaleAvg = AvgCond(Gender, Mark, 'F');
BelowAverage = BelowAvg(Mark, out AboveAverage);
NamesCheck = CheckNames(Name, 'T');
Output [0]= "Frequent Mark = " + Frequent.ToString();
Output [1]= "Frequency = " + Frequency.ToString();
Output [2]= "Average Of All = " + AvgAll.ToString();
Output [3]= "Average Of Males = " + MaleAvg.ToString();
Output [4]= "Average Of Females = " + FemaleAvg.ToString();
Output [5]= "Below Average = " + BelowAverage.ToString();
Output [6]= "Above Average = " + AboveAverage.ToString();
Output [7]= "Names With \"T\" = " + NamesCheck.ToString();
WriteFile("c:\\Output.txt", Output);
}
}
}
Well, I like LINQ (update: excluded via comments) for querying, especially if I can do it without buffering the data (so I can process a huge file efficiently). For example below (update: removed LINQ); note the use of iterator blocks (yield return) makes this fully "lazy" - only one record is held in memory at a time.
This also shows separation of concerns: one method deals with reading a file line by line; one method deals with parsing a line into a typed data record; one (or more) method(s) work with those data record(s).
using System;
using System.Collections.Generic;
using System.IO;
enum Gender { Male, Female, Unknown }
class Record
{
public string Name { get; set; }
public Gender Gender { get; set; }
public int Score { get; set; }
}
static class Program
{
static IEnumerable<string> ReadLines(string path)
{
using (StreamReader reader = File.OpenText(path))
{
string line;
while ((line = reader.ReadLine()) != null)
{
yield return line;
}
}
}
static IEnumerable<Record> Parse(string path)
{
foreach (string line in ReadLines(path))
{
string[] segments = line.Split(',');
Gender gender;
switch(segments[1]) {
case "m": gender = Gender.Male; break;
case "f": gender = Gender.Female; break;
default: gender = Gender.Unknown; break;
}
yield return new Record
{
Name = segments[0],
Gender = gender,
Score = int.Parse(segments[2])
};
}
}
static void Main()
{
Record best = null;
foreach (Record record in Parse("data.txt"))
{
if (record.Gender != Gender.Male) continue;
if (best == null || record.Score > best.Score)
{
best = record;
}
}
Console.WriteLine("{0}: {1}", best.Name, best.Score);
}
}
The advantage of writing things as iterators is that you can easily use either streaming or buffering - for example, you can do:
List<Record> data = new List<Record>(Parse("data.txt"));
and then manipulate data all day long (assuming it isn't too large) - useful for multiple aggregates, mutating data, etc.
This question asks how to find a maximal element by a certain criterion. Combine that with Marc's LINQ part and you're away.
In the real world, of course, these would be records in a database, and you would use one line of SQL to select the best record, ie:
SELECT Name, Score FROM Grades WHERE Score = MAX(Score)
(This returns more than one record where there's more than one best record, of course.) This is an example of the power of using the right tool for the job.
I think the fastest and least-code way would be to transform the txt to xml and then use Linq2Xml to select from it. Here's a link.
Edit: That might be more work than you'd like to do. Another option is to create a class called AcademicRecord that has properties for the persons name gender etc. Then when you read the file, add to a List for each line in the file. Then use a Sort predicate to sort the list; the highest record would then be the first one in the list. Here's a link.
Your assignment might have different requirements, but if you only want to get "best male name and grade" from a file you described, a compact way is:
public String FindRecord()
{
String[] lines = File.ReadAllLines("MyFile.csv");
Array.Sort(lines, CompareByBestMaleName);
return lines[0];
}
int SortByBestMaleName(String a, String b)
{
String[] ap = a.Split();
String[] bp = b.Split();
// Always rank male higher
if (ap[1] == "m" && bp[1] == "f") { return 1; }
if (ap[1] == "f" && bp[1] == "m") { return -1; }
// Compare by score
return int.Parse(ap[2]).CompareTo(int.Parse(bp[2]));
}
Note that this is neither fast nor robust.

Categories