Question:
C#: Access a string and add up integers?
anonymous
2012-04-28 09:48:21 UTC
If I have a string: 'JSW1234'
via
Console.WriteLine("ID?");
String ID = Console.ReadLine();
How would I get it so I can show the sum of the last 4 characters (i.e 1+2+3+4)
I know theres something that can get the characters of the strings: Console.WriteLine(ID[2])
But It cant do sums so Console.WriteLine(ID[2]+ID[3]) WOULD NOT WORK!
PLEASE HELP!
Three answers:
Number 6
2012-04-28 12:02:00 UTC
(Edited on 30th April to fix a mistake)



It looks like you're trying to add the pieces of a string together - what your code is saying is:

Add "1" + "2" + "3" + "4"

when what your code should say is:

Add 1 + 2 + 3 + 4



The reason it won't work is because you're trying to use a mathematical operator (+) on strings - you need to change them into actual numbers, not strings that contain numbers.

This is because C# is Strongly Typed, meaning that variables have a set Type, e.g. String, Integer, Boolean, etc. and the compiler won't try and guess what Type you mean (the way a language such as PHP does)



If you explicitly cast (that is, force the compiler to treat one Type as another) then it should work.

To cast one variable Type into another in C#, you put brackets around the desired type, in front of the thing you want to cast, e.g.:

double someNumber = 1.67;

int i = (int)someNumber;

Console.WriteLine(i) // outputs 1



However, since a string is made up of Chars, when you take a string and access it using its array indices, e.g. id[21], what you get out is a Char too.

If you try to cast a Char to an Int, what happens is that you get the ASCII code for that Char, so:



string s = "567"; // s[0] = the Char "5"

int i = (int)s[0]; // i does not equal 5 but the ASCII code for the character "5", which is 53.

Console.WriteLine(i * 2); // Outputs 106



To get the actual number you need, you need to tell it to treat the Char as a number and not a character.

This is done with the Char.GetNumericValue method:



string s = "567"; // s[0] = the Char "5"

int i = char.GetNumericValue(s[0]); // i = 5;

Console.WriteLine(i * 2); // Outputs 10



So for your code:



Console.WriteLine("ID?");

String ID = Console.ReadLine();

Console.WriteLine( char.GetNumericValue(ID[2]) + char.GetNumericValue(ID[3]) );



Hope that helps and wasn't too long-winded!

Sorry for the confusion before the edit :-)
?
2017-03-03 10:35:52 UTC
interior the approach from which you attempt to call the interior maximum function, you create an merchandise of sophistication worker and attempt to call the interior maximum approach on that. it extremely is a mistake. each and each merchandise of a type is a unit of function become self reliant from something. you may call inner maximum purposes from purposes interior the comparable type, yet basically in connection with the article that the function is interior. think of we've been the two workers of Acme. As an worker, I extremely have a suited to convey at the same time a verify on the top of the pay era. particularly, my very own verify. i don't have the suited to convey at the same time your pay basically because of the fact we are the two workers, suited? The purposes interior the class can get entry to inner maximum records and methods basically in terms of the article they are being spoke of as from. they can't do the comparable with different gadgets.
anonymous
2012-04-28 09:50:05 UTC
syntax error..is occured


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...