I am doing a method that can convert a int number to hexadecimal string. Basically my code is work for all test, but I am still looking for an efficient way to get rid of the array part. Anyone would give me a hand?
the better way for not using array, but rather use a string type would be more efficient, and the answer is very clear as above. So i just delete my original code
Integer.toHexString(int);
Should be what you are looking for
String digits = "0123456789ABCDEF";
output = digits.charAt(remain) + output;
I hope there are no convertion problems.
I Think the Integer.toHexString(int) is the best way to fix your problem
Related
So, the full String that I'm trying to read is:
Members online: (0/0):
Members offline: (0/0):
While I think I could substring it to get the 0, it can change it's size, because it's an int... Is there any way I could get the integers even if they change?
Thanks.
EDIT: I also want to only get the first integer of "0/0"... Any ways I could do this? I know I could use regex but I don't know if it's the best way to do it...
You should use indexOf and lastIndexOf for that purpose.
int n1 = Integer.parseInt(t.substring(t.indexOf('(') + 1, t.indexOf('/')))
That variable (n1) will save the number itself, if you want just the position you should use next one.
int pos=t.indexOf('(') + 1
I am trying to return a string of names in a list as an integer, the names are pulled from a longer list. i have managed to get the list of names needed but cannot figure out how to convert the string into an integer. see code below:
public static void doStuff(List<Seat> uk){
for(Seat s:uk)
if (s.place.contains("Edinburgh"))
System.out.println(s.results);
This is where i get stuck does anyone have any advise on how to solve this?
You can use Integer.parseInt("123") to convert a String ("123") to an Integer (123).
What String are you trying to convert though ?
Let me know if it works (or not)
Happy coding :) -Charlie
To convert a string to an int:
String s = "5";
int i = Integer.parseInt(s);
Your question is a bit confusing, so if this isn't what you're looking for, let me know and I'll update my answer accordingly.
Sorry for the unclear title but I don't even know what to call it, I'll just go ahead and explain what's happening.
I'm using a Stringbuffer to build an URL. It looks like this:
http://maps.googleapis.com/maps/api/geocode/json?latlng=49.0516736,8.38891840&sensor=false
I encountered this behavious when comparing this string in a Unit-test to the actual result of the method.
And this is the assertion-error I'm getting:
latlng=49.0516736[,8.38891840]&sensor=false> but was:<...on?latlng=49.0516736[,8.3889184]&sensor=false
The emphasis is on the character sequence 0]& and 4]& right before sensor=false
IF I remove the zero before the & the test goes green.
then the created string looks like this:
latlng=49.0516736,8.3889184&sensor=false
so ... just as expected.
It's not the problem, that the 0 itself gets truncated and test would fail - I've proved that my code is doing what it's supposed to (when I remove the zero), but I want to know what is happening here.
0& must be some kind of indication for array-access or some kind of escaping. I don't know.
Anyone any idea what's causing this behaviour?
Edit:
Here's the code I'm using
StringBuffer s = new StringBuffer( grailsApplication.config.geocodingurl.toString() )
s.append(coordinates.latitude)
s.append(",")
s.append(coordinates.longitude)
s.append("&sensor=false")
return s.toString()
There is a formatting/padding issue when converting double into String.
What you are doing is probably using StringBuilder#append(double) which in the end calls Double#toString().
See the javadoc of those methods and find out how double values are converted to String.
Alternatively, if you want to have control over your code, use NumberFormat or it's subclasses.
I am using this line of code to dispaly a string with an int value at the end of it.
<legend><spring:message code="time.createMonth.legend" arguments="${year}"/></legend>
The code is working but it is displaying the int lik a number "2,011" instead of "2011".
How can I format it so as it is displayed without the comma?
is there an easy way that i can just throw a cast or somehting? Like so?
"arguments="${(Sting)year}" or "arguments="${Sting.valueOf(year)}" ?
Have a look at NumberFormat:
http://download.oracle.com/javase/6/docs/api/java/text/NumberFormat.html
I think the easiest would be:
arguments="${'' + year}"
Sorry if my question sounds dumb. But some time small things create big problem for you and take your whole time to solve it. But thanks to stackoverflow where i can get GURU advices. :)
So here is my problem. i search for a word in a string and put 0 where that word occur.
For example : search word is DOG and i have string "never ever let dog bite you" so the string
would be 000100 . Now when I try to convert this string into INT it produce result 100 :( which is bad. I also can not use int array i can only use string as i am concatinating it, also using somewhere else too in program.
Now i am sure you are wondering why i want to convert it into INT. So here my answer. I am using 3 words from each string to make this kind of binary string. So lets say i used three search queries like ( dog, dog, ever ) so all three strings would be
000100
000100
010000
Then I want to SUM them it should produce result like this "010200" while it produce result "10200" which is wrong. :(
Thanks in advance
Of course the int representation won't retain leading zeros. But you can easily convert back to a String after summing and pad the zeros on the left yourself - just store the maximum length of any string (assuming they can have different lengths). Or if you wanted to get even fancier you could use NumberFormat, but you might find this to be overkill for your needs.
Also, be careful - you will get some unexpected results with this code if any word appears in 10 or more strings.
Looks like you might want to investigate java.util.BitSet.
You could prefix your value with a '1', that would preserve your leading 0's. You can then take that prefix into account you do your sum in the end.
That all is assuming you work through your 10 overflow issue that was mentioned in another comment.
Could you store it as a character array instead? Your using an int, which is fine, but your really not wanting an int - you want each position in the int to represent words in a string, and you turn them on or off (1 or 0). Seems like storing them in a character array would make more sense.