Convert * to int
Author |
Message |
hamid14
|
Posted: Sun Oct 10, 2010 6:02 pm Post subject: Convert * to int |
|
|
Basically I'm trying to give a variable called b, which is a string, a integer value of 1 and be able to add it to another integer
String b = "*";
int i = 0;
//b should have an intege value of 1, so its basically the same thing as 1, it just has a symbol representing the 1, rather than just typing 1
i + b = 2;
Is this even possible? |
|
|
|
|
|
Sponsor Sponsor
|
|
|
TerranceN
|
Posted: Sun Oct 10, 2010 8:00 pm Post subject: RE:Convert * to int |
|
|
Something like this?
|
|
|
|
|
|
hamid14
|
Posted: Sun Oct 10, 2010 8:02 pm Post subject: Re: Convert * to int |
|
|
the string has to be * not 1, it should represent and function as a 1 but appear to the user as a * |
|
|
|
|
|
TerranceN
|
Posted: Sun Oct 10, 2010 8:14 pm Post subject: RE:Convert * to int |
|
|
Well if it only has to appear to the user to be all *s, then you can just make a function that creates a string the same length, but all *s.
Something like this:
Java: |
public static void main (String[] args )
{
String b = "5780";
int i = 0;
i = Integer. parseInt(b ) + 1;
System. out. println(starredOutString (b ));
System. out. println(i );
}
static String starredOutString (String src )
{
String temp = "";
for (int i = 0; i < src. length(); i++ )
{
temp += "*";
}
return temp;
}
|
Although unless the string with a number in it is input, theres not much point of it being a string anymore. |
|
|
|
|
|
hamid14
|
Posted: Sun Oct 10, 2010 8:19 pm Post subject: Re: Convert * to int |
|
|
no thats not it. good try though. IT should be like 1 + * = ** and 2 + * = *** and 3 + * = **** and so on. |
|
|
|
|
|
TerranceN
|
Posted: Sun Oct 10, 2010 8:24 pm Post subject: RE:Convert * to int |
|
|
Oh in that case you can just use b.length() to get the number of stars there are. |
|
|
|
|
|
DtY
|
Posted: Mon Oct 11, 2010 1:54 pm Post subject: Re: RE:Convert * to int |
|
|
TerranceN @ Sun Oct 10, 2010 8:14 pm wrote: Well if it only has to appear to the user to be all *s, then you can just make a function that creates a string the same length, but all *s.
[...]
Although unless the string with a number in it is input, theres not much point of it being a string anymore. This really is the best option, store it as a number (because that's what it is), and if you need to display it, convert it to a string, like above, and if you need to get that as input, use another function to convert it back. (you might even consider creating a class to wrap all this.)
If you really don't want to do that, you will still need to use those functions. For reference:
toStar(int) => string
fromStar(string) => int
fromStar(int) => int
And then:
toStar(fromStar("*") + fromStar(1)) = "**";
(fromStar(1) really isn't necessary, but it makes it more symmetrical. In dynamically typed languages this would allow you to use them interchangeably, but I don't know if it has any real advantage in a statically typed language like Java.) |
|
|
|
|
|
|
|