βœ… Is it possible to Parse a string and identify it's type?

I want to scan an index of an array and identify if it's an operator such as (+,-,*) or if it's an operand (doubles). Is this possible?
101 Replies
Merineth πŸ‡ΈπŸ‡ͺ
while (stack != null)
{

if (stack.top == )
while (stack != null)
{

if (stack.top == )
basically in my if statement i'm triyng to identify if the string at the top of the stack is an operator or operand My guess would be an if else statement to check the operators? else it's an operand?
Angius
Angiusβ€’3mo ago
You can use an if/else, you can use a switch, sure
Merineth πŸ‡ΈπŸ‡ͺ
nice Also by any chance, do you know how i can identify the length of an array? Does C# have a CBL for such a method?
Angius
Angiusβ€’3mo ago
uh .Length? Or .Count, I can never remember which is arrays which is lists
Merineth πŸ‡ΈπŸ‡ͺ
Hmmm
int topofstack = mainstack[mainstack.top];
int topofstack = mainstack[mainstack.top];
Are attributes not a part of the object that we create?
Angius
Angiusβ€’3mo ago
No description
Angius
Angiusβ€’3mo ago
What attributes?
Merineth πŸ‡ΈπŸ‡ͺ
I'm trying to access the attribute "top" But it says it's not accessible because of it's protection level even tho the class is public
Angius
Angiusβ€’3mo ago
it's private
Merineth πŸ‡ΈπŸ‡ͺ
oh i have to set the attributes to public if i want to use them?
Angius
Angiusβ€’3mo ago
C# defaults to lowest access if no access is specified
Merineth πŸ‡ΈπŸ‡ͺ
I see
Merineth πŸ‡ΈπŸ‡ͺ
I don't get the type error? top is of type int
Angius
Angiusβ€’3mo ago
Does Stack have an indexer?
Merineth πŸ‡ΈπŸ‡ͺ
indexer?
Angius
Angiusβ€’3mo ago
This tells me that it does not:
No description
Angius
Angiusβ€’3mo ago
Yes You can't just get an element from anything with [9] or what have you That thing must have an indexer Yout StackArray field in Stack is an array, so that one would have an indexer by default You could do mainstack.StackArray[mainstack.top] But not mainstack[mainstack.top] Not unless you implement an indexer
Merineth πŸ‡ΈπŸ‡ͺ
:harold:
Angius
Angiusβ€’3mo ago
Using Indexers - C#
Learn how to declare and use an indexer for a class, struct, or interface in C#. This article includes example code.
Merineth πŸ‡ΈπŸ‡ͺ
Got another related question i think
right = mainstack.StackArray[Convert.ToDouble(mainstack.top)];
right = mainstack.StackArray[Convert.ToDouble(mainstack.top)];
Merineth πŸ‡ΈπŸ‡ͺ
I'm trying to set right which is a double to the value at mainstack.StackArray[mainstack.top] Can i not convert a string to a double? "Cannot implicitly convert type string to double" I'm not entirely sure what that means
Angius
Angiusβ€’3mo ago
You can parse a string into a double, yes But mainstack.top is not a string And you cannot index arrays with doubles
Merineth πŸ‡ΈπŸ‡ͺ
mainstack.top is an integer
Angius
Angiusβ€’3mo ago
Yes
Merineth πŸ‡ΈπŸ‡ͺ
Can i convert int to double?
Angius
Angiusβ€’3mo ago
Sure But you cannot use a double to index an array
Merineth πŸ‡ΈπŸ‡ͺ
Ahhh So i have to convert it first and then assign it to right?
Angius
Angiusβ€’3mo ago
uh You use ints to index arrays mainstack.top is an int
Merineth πŸ‡ΈπŸ‡ͺ
yea
Angius
Angiusβ€’3mo ago
Why would you convert anything here?
Merineth πŸ‡ΈπŸ‡ͺ
AAAh
right = Convert.ToDouble(mainstack.StackArray[mainstack.top]);
right = Convert.ToDouble(mainstack.StackArray[mainstack.top]);
` Is that what you meant?
Angius
Angiusβ€’3mo ago
That seems better, yes
Merineth πŸ‡ΈπŸ‡ͺ
nicee very smart of you
Angius
Angiusβ€’3mo ago
Also, it's generally better to use double.TryParse() since it handles the string not being a double better
MODiX
MODiXβ€’3mo ago
Angius
REPL Result: Success
string[] str = { "1.4", "0", "chair", " ", "", "0.1" };
foreach (var s in str)
{
var isDouble = double.TryParse(s, out var num);
if (isDouble)
{
Console.WriteLine($"Double of value {num}");
}
else
{
Console.WriteLine($"'{s}' is not a double");
}
}
string[] str = { "1.4", "0", "chair", " ", "", "0.1" };
foreach (var s in str)
{
var isDouble = double.TryParse(s, out var num);
if (isDouble)
{
Console.WriteLine($"Double of value {num}");
}
else
{
Console.WriteLine($"'{s}' is not a double");
}
}
Console Output
Double of value 1.4
Double of value 0
'chair' is not a double
' ' is not a double
'' is not a double
Double of value 0.1
Double of value 1.4
Double of value 0
'chair' is not a double
' ' is not a double
'' is not a double
Double of value 0.1
Compile: 521.251ms | Execution: 59.278ms | React with ❌ to remove this embed.
Merineth πŸ‡ΈπŸ‡ͺ
Ahh i see C# is so confusing sometimes I don't really understand why some errors occur
Merineth πŸ‡ΈπŸ‡ͺ
Here for example. It says it doesn't exists, even tho it's clearly public ?
No description
No description
Angius
Angiusβ€’3mo ago
Does it exist in the same class?
Merineth πŸ‡ΈπŸ‡ͺ
Noo it's in a differenty class
Angius
Angiusβ€’3mo ago
Well there you go, then
Merineth πŸ‡ΈπŸ‡ͺ
But both the class and the method are public
Angius
Angiusβ€’3mo ago
Sure But this method is a member of a specific class You can't just reference it and not the class If the method is static, you just do Class.Method() If the method is not static, you need an instance of the class first
var c = new Class();
c.Method();
var c = new Class();
c.Method();
Merineth πŸ‡ΈπŸ‡ͺ
Aaaah right i completely forgot about that 🧠 It worked!
Angius
Angiusβ€’3mo ago
nice
Merineth πŸ‡ΈπŸ‡ͺ
Got another question :sadcat:
Angius
Angiusβ€’3mo ago
Shoot
Merineth πŸ‡ΈπŸ‡ͺ
I'm trying to read/write of files. One input.txt and output.txt. They are located in C:\Users\aljom\Documents\Plugg Programmering filer\OOP\Assignment1\Assignment1\Calculator\Calculator\bin\Debug\net6.0. in my code i set my filepath to
string inputfile = @"\input.txt";
string outputfile = @"\output.txt";
string inputfile = @"\input.txt";
string outputfile = @"\output.txt";
It couldn't find them, not exactly sure what i messed up
Angius
Angiusβ€’3mo ago
Right-click on each of those files, go to properties, set them to be copied always or copied if newer
Merineth πŸ‡ΈπŸ‡ͺ
In my folder on my pc?
Angius
Angiusβ€’3mo ago
In Visual Studio
Merineth πŸ‡ΈπŸ‡ͺ
Do you mean in the code?
Angius
Angiusβ€’3mo ago
No
Merineth πŸ‡ΈπŸ‡ͺ
Or should they be in the same file location as my solution explorer?
Angius
Angiusβ€’3mo ago
Ideally, place them next to the .csproj Then right-click them And do what I mentioned When doing so, they will be automatically copied to that .bin folder when the project gets built and ran
Angius
Angiusβ€’3mo ago
Remove that \ Or replace it with ./ \ refers to the root, the root of the drive in this case No slash or ./ will refer to the current working directory
Merineth πŸ‡ΈπŸ‡ͺ
Ooh ok the code ran! i'm not sure tho where it got placed the copies that is
Angius
Angiusβ€’3mo ago
Probably in that .bin/debug/......
Merineth πŸ‡ΈπŸ‡ͺ
Yeah i found them! now all that remains is fix the code since it's not outputting wha ti want xD I got a quick question about foreach loop and StreamWriter
Merineth πŸ‡ΈπŸ‡ͺ
when applying a foreach loop
Angius
Angiusβ€’3mo ago
WriteAllLines It expects an array of lines
Merineth πŸ‡ΈπŸ‡ͺ
ooh In that case, are there any substitution to it? Or would i have to take my double and convert it into an array of lines?
Angius
Angiusβ€’3mo ago
There are many options You can keep appending to the file in the loop Could use a stringbuilder to build a single string and use WriteAllText() Could store the calculated results in another array/list and write those as lines And that can be done with either a loop like here, or LINQ Plenty of choices
Merineth πŸ‡ΈπŸ‡ͺ
Oh damn i didn't even think of that hehe
Merineth πŸ‡ΈπŸ‡ͺ
Shouldn't outputlines be defined inside my foreach loop?
Angius
Angiusβ€’3mo ago
outputlines has no value It's empty, uninitialized You have a space that says "apple crates go here" You're trying to put an apple in a crate But there are no crates
Merineth πŸ‡ΈπŸ‡ͺ
haha okay that makes sense I just assume it auto initialized like in C
Angius
Angiusβ€’3mo ago
Nope
Merineth πŸ‡ΈπŸ‡ͺ
Nice works now!
Merineth πŸ‡ΈπŸ‡ͺ
Can the string 2.5 not be converted to decimals? (or even double, i tried making it double)
Angius
Angiusβ€’3mo ago
I'm assuming you live in a country that uses a comma as decimal separator πŸ˜„ Just need to pass CultureInfo.InvariantCulture as the second parameter Or any other culture that uses a period Alternatively, you can set the culture of the whole thread
Merineth πŸ‡ΈπŸ‡ͺ
Hmmm How do i pass CultureInfo.InvariantCulture as a second parameter?
Angius
Angiusβ€’3mo ago
Just... do? Assuming Convert.ToDecimal() takes it
Merineth πŸ‡ΈπŸ‡ͺ
right = Convert.ToDecimal.CultureInfo.InvariantCulture(mainstack.StackArray[mainstack.top]);
right = Convert.ToDecimal.CultureInfo.InvariantCulture(mainstack.StackArray[mainstack.top]);
?
Angius
Angiusβ€’3mo ago
Convert.ToDecimal("123.45", CultureInfo.InvariantCulture)
Convert.ToDecimal("123.45", CultureInfo.InvariantCulture)
Merineth πŸ‡ΈπŸ‡ͺ
ohh
Angius
Angiusβ€’3mo ago
A parameter
Merineth πŸ‡ΈπŸ‡ͺ
I thought it was pass it as an argument
Angius
Angiusβ€’3mo ago
$structure
MODiX
MODiXβ€’3mo ago
namespace Namespace;

[Attribute]
public class Class
{
public string PublicField;
private bool _privateField;

public int PublicProperty { get; set; }

public Class() {} // Constructor

public void Method(int parameter)
{
var localVariable = parameter;

int LocalMethod(string param) { return 3; }
}
}
namespace Namespace;

[Attribute]
public class Class
{
public string PublicField;
private bool _privateField;

public int PublicProperty { get; set; }

public Class() {} // Constructor

public void Method(int parameter)
{
var localVariable = parameter;

int LocalMethod(string param) { return 3; }
}
}
Merineth πŸ‡ΈπŸ‡ͺ
Or rather everything inside () are arguments
Angius
Angiusβ€’3mo ago
Here's the naming reference
Merineth πŸ‡ΈπŸ‡ͺ
Aah i see! I'm a little unclear how to handle exceptions
try
{
right = Convert.ToDecimal(mainstack.StackArray[mainstack.top], CultureInfo.InvariantCulture);
}
catch (FormatException)
{

}
try
{
right = Convert.ToDecimal(mainstack.StackArray[mainstack.top], CultureInfo.InvariantCulture);
}
catch (FormatException)
{

}
`
Angius
Angiusβ€’3mo ago
Remember when I mentioned the TryParse() method? Like that So you don't get exceptions but a nice bool you can check with a regular if $tryparse
MODiX
MODiXβ€’3mo ago
When you don't know if a string is actually a number when handling user input, use int.TryParse (or variants, e.g. double.TryParse)
if(int.TryParse("123", out int number))
{
var total = number + 1;
Console.WriteLine(total); // output: 124
}
if(int.TryParse("123", out int number))
{
var total = number + 1;
Console.WriteLine(total); // output: 124
}
TryParse returns a bool, where true indicates successful parsing. Remarks: - Avoid int.Parse if you do not know if the value parsed is definitely a number. - Avoid Convert.ToInt32 entirely, this is an older method and Parse should be preferred where you know the string can be parsed. Read more here
Merineth πŸ‡ΈπŸ‡ͺ
Well i think in our assignment we want to get an exception Basically when the user tries different things
Merineth πŸ‡ΈπŸ‡ͺ
It should output the exception into the output file
Angius
Angiusβ€’3mo ago
Ah Then yeah, you handle exceptions with a try/catch But the thing in the image here I think is about creating and throwing your own exceptions
Merineth πŸ‡ΈπŸ‡ͺ
Yes, most likely Okay i watched a few videos and tried again
Merineth πŸ‡ΈπŸ‡ͺ
However i have nooo idea how to return it? xD
Want results from more Discord servers?
Add your server