|
|
If the data is corrupt, you cannot use it ... unless: you can see a pattern in what is wrong, and work around that.
Your definition of thee field names omits what are obviously two values representing years.
Each line of your data appears to contain 2 hyphens, and 13 commas, so if you split it like this:
char[] splitChars = new char[] { '-', ',' };
string[] splitLine =
"2014-7-1,76,66,86,66,85,48,97,1885,1970,0.13,0.16,5.09".Split(splitChars,
StringSplitOptions.RemoveEmptyEntries);
if (splitLine.Length != 15)
{
}
Of course that's a crude method. I would construct a validation method that would parse the string/line into (first) a DateTime, then, into integers (for the years), and doubles (for temps). Something like:
public bool TryValidate(string line, out ValidData data)
{
data = default(data);
// process
// return true or false
}
public class ValidData
{
public DateTime DTime;
// ...
// construct
}
«Where is the Life we have lost in living? Where is the wisdom we have lost in knowledge? Where is the knowledge we have lost in information?» T. S. Elliot
|
|
|
|
|
2014-7-1,76,66,86,66,85,48,97,1885,1970,0.13,0.16,5.09
Yes, String.Split() using comas.
token[0] is the "date"; Convert.ToDate(token[0])
token[1] is the "actual mean temp"; Convert.ToInt32(token[1])
etc.
The trailing fields are not part of your identified requirements.
How to: Parse Strings Using String.Split (C# Guide) | Microsoft Docs
(Just noticed some of the MS docs now have a "run" command button).
"(I) am amazed to see myself here rather than there ... now rather than then".
― Blaise Pascal
modified 3-May-19 10:05am.
|
|
|
|
|
A rudimentary google search would reveal several techniques for parsing a CSV file. In point of fact, there are several articles here on CodeProject that discuss that very thing.
".45 ACP - because shooting twice is just silly" - JSOP, 2010 ----- You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010 ----- When you pry the gun from my cold dead hands, be careful - the barrel will be very hot. - JSOP, 2013
|
|
|
|
|
Hi,
This is my hello world. When I run it with the Console.ReadLine() commented out, it runs with the exit code of 0. However, when I uncomment it to view the console output, it gives an error code. Can someone explain this for me please?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("sending you a bazillion dollars");
Console.ReadLine();
}
}
}
|
|
|
|
|
It was because I closed the console with the x instead of hitting enter.
|
|
|
|
|
Thanks for getting back. Yes, it's important to track all the details.
"(I) am amazed to see myself here rather than there ... now rather than then".
― Blaise Pascal
|
|
|
|
|
If your code doesn't do what you think it should, asking a question of others isn't the first thing you should do!
Visual Studio is an "IDE" - short for "Integrated Development Environment" - and that means it contains a lot of tools designed to help you create your code. One of the most important is the debugger, which lets you run your code (and even change it!) while you look at what is going on.
Have a look here: First look at the debugger - Visual Studio | Microsoft Docs[^] and it will help you get started with it - it may seem like a lot of effort, but the basics you will pick up really quickly, and a good familiarity with the debugger will save you a huge amount of time and hair pulling later! Most developers probably spend more time there than actually writing code
Sent from my Amstrad PC 1640
Never throw anything away, Griff
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
Hi All,
We need recommendation on any software/tool/online tool for designing forms.
Below are the conditions which should be matched:
1. Form designed can be user cross platform Web/Windows/Andriod/iOS
2. Form designed can be used offline or online. Offline means we can download the form designed and can use in our application.
3. Data from the form should not be saved on third party tools used instead should be saved on our local database.
4. Form designer tool should have various options for controls like file upload, draw on image control (can select image and draw on it).
5. Form designer tool can be either free or licences.
Any help will be appreciated.
Thanks
|
|
|
|
|
|
Bonsoir, j'ai un question concernant le protocole MODBUS RTU RS485. Je suis en train d'utiliser le langage c# pour manipuler le programme.
Mon problème était l'échange entre le maître et l'esclave.En fait,la réception de des données exactement.
Pouvez-vous m'aider?
Et merci.
[Google translate]
Good evening, I have a question about the protocol MODBUS RTU RS485. I am using the C # language to manipulate the program.
My problem was the exchange between the master and the slave. In fact, receiving data exactly.
Can you help me?
And thank you.
[/Google Translate - OriginalGriff]
modified 30-Apr-19 12:29pm.
|
|
|
|
|
This is an English language site, and we can only accept questions in that language.
Plus ... there is nowhere near enough information there to give us any idea what your problem might be: remember that we can't see your screen, access your HDD, or read your mind - we only get exactly what you type to work with; we get no other context.
Please, think about your problem and try again - perhaps using Google Translate - with information about your existing code and any errors that you get.
***
Ceci est un site en anglais et nous ne pouvons accepter que les questions dans cette langue.
De plus, il n'y a pratiquement aucune information suffisante pour nous donner une idée de votre problème: rappelez-vous que nous ne pouvons pas voir votre écran, accéder à votre disque dur ou lire dans vos pensées - nous obtenons exactement ce que vous tapez au travail avec; nous n'obtenons aucun autre contexte.
Réfléchissez bien à votre problème et essayez à nouveau - peut-être en utilisant Google Translate - en fournissant des informations sur votre code existant et les erreurs éventuelles.
Sent from my Amstrad PC 1640
Never throw anything away, Griff
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
Hi,
may I suggest you study this CodeProject article first: Simple Modbus Protocol in C# / .NET 2.0[^].
I haven't read it but it seems to be right on target. It explains a lot and offers a complete downloadable program. It may be a little dated, but maybe so is MODBUS itself.
|
|
|
|
|
|
Dear all,
I'm having a brain melt down...
Why do I get a red squiggly on the if clause?
input = Console.ReadLine();
if(input = "stop")
{
Console.WriteLine(result);
}
else
{
converted = Convert.ToInt32(input);
result = result + converted;
}
And yes I know there are other issues with the thing.
|
|
|
|
|
Because this is C#, not C, or C++.
C# has a concept of boolean values: true and false , C and C++ don't.
So an assignment is legal (though often dangerous) in C and C++ because any non-zero value is true and zero is false.
But it isn't in C#: if requires a condition that evaluates to true or false explicitly.
Probably what you meant to say was:
if (input == "stop") Not
if (input = "stop")
Sent from my Amstrad PC 1640
Never throw anything away, Griff
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
modified 30-Apr-19 9:37am.
|
|
|
|
|
Of course.
Thanks.
I feel even more melted down...
|
|
|
|
|
Don't worry about it - we've all done it. You tend to read what you meant to write - or at least I do ...
Just be grateful that the C# compiler spots it - it was a whole load harder to find the problem at run time in C!
Sent from my Amstrad PC 1640
Never throw anything away, Griff
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
modified 30-Apr-19 10:36am.
|
|
|
|
|
OriginalGriff wrote: C# has a concept of boolean values: true and false , C and C++ don't. Are you sure?
|
|
|
|
|
I'm trying not to confuse the newbie too much. Modern C++ does, but ... zero / non zero still works as well ...
Sent from my Amstrad PC 1640
Never throw anything away, Griff
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
How about the following in C:
int i = 5;
if (i == 5) {}
|
|
|
|
|
Not really - the == operator still returns zero / nonzero, as do all the other comparison operators.
if (i != 5) {} is the same as
if (i - 5) {} (and is normally executed as exactly that in machine code).
C# genuinely has boolean types - which internally use 0/1 - but which are the only types permitted as the result of a conditional statement.
Sent from my Amstrad PC 1640
Never throw anything away, Griff
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
|
well, while you are here, see if you get some ideas from this, and see this for more ideas: [^]
int result = -1;
int converted = 0;
bool conversionok = false;
Console.WriteLine("Enter an integer, or 'stop'");
string input = Console.ReadLine();
if (input == "stop")
{
Console.WriteLine($"stopped: result = {result}");
}
else
{
conversionok = Int32.TryParse(input, out converted);
if (conversionok)
{
result = result + converted;
Console.WriteLine($"conversion ok: result = {result}");
}
else
{
Console.WriteLine($"conversion fail: result = {result}");
}
}
Console.ReadLine(); Yes, this code can be simplified; it is written out "more fully" here for educational purposes.
«Where is the Life we have lost in living? Where is the wisdom we have lost in knowledge? Where is the knowledge we have lost in information?» T. S. Elliot
|
|
|
|
|
Quote: if(input = "stop"
I suspect you are coming over from Visual Basic, in which the equality test is pretty much the way you wrote it. In C#, the equality operator is == . Hence, your snippet should be as follows.
f(input == "stop")
{
Console.WriteLine(result);
}
else
{
converted = Convert.ToInt32(input);
result = result + converted;
<pre>
}</pre>
David A. Gray
Delivering Solutions for the Ages, One Problem at a Time
Interpreting the Fundamental Principle of Tabular Reporting
|
|
|
|
|