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
Related
just a beginner in java doing a simple reading file processing some data and writing to a file, however whenever I print to a file I get this inconsistent spacing such as shown below. Especially the zeros are throwing me off. Right now I am padding the strings with empty spaces but I am sure there are better suggestions out there to have consistent spacing between strings in a file. Thanks for the help. I have attached a picture of the inconsistent printing, especially the zeros.
In Apache Commons StringUtils library there are convenient methods to set the proper pad - e.g. rightPad seems to be useful in your case - you could make something like
String row = StringUtils.rightPad(firstValue, lengthOfTheLongestValueInColumn1 + definedColumnMargin) + StringUtils.rightPad(secondValue, lengthOfTheLongestValueInColumn2 + definedColumnMargin) // + ... etc
If these lengthOfTheLongestValueInColumn variables would be calculated dynamically then it would work perfectly but you could also just hardcode some reasonable value (if you know that no value will be longer than, let say, 20 it could have value of 20)
I think best option will be to use String format
return String.format("%1$" + length + "s", inputString)
where length is max size of your digit
you can read more here
Say we have a number
int number = 1234
and I want to use formatting to extract the final two digits as a string 34. How can one accomplish that?
I attempted:
String extraction = String.format("%d", number)
but this simply copies the entire number. I'm new to the syntax used by the formatter and can't seem to figure out the syntax that can go inside the characters of a number (or a String, for that matter) and pull them out. I've found a solution using charAt methods but am particularly curious about whether it's possible to accomplish it using formatting techniques.
Going via a String is inefficient and unnecessary to extract the last two digits of an integer.
Simply:
int lastTwoDigits = number % 100;
If you do want to go via a String, you can use:
String s = Integer.toString(number);
s = s.substring(s.length() - 2);
int lastTwoDigits = Integer.parseInt(s);
(note that this handles -ve numbers slightly differently to the first suggestion).
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
I was wondering if there is a way to compare strings in android with greater than or >.
Lets say I have this:
String numbers = number.getText().toString();
if (numbers.equals("9")){
output.setText("50");}
so if you enter 9 in the number EditText field the output TextView will display 50.
I have quite a few different numbers that will then = a different number but what can I do if I want 10,11,12,13,etc to = 100?
Is there a way to do this by using something like this?
if (numbers.equals("9"++))
or is there some kind of wildcard in android like
if (numbers.equals("1"+"*"))
i know if i replace the * with zero it will be 10 so if this is possible I could make one for 1, one for 2, one for 3, etc. and this would still save me so much code.
If this is not possible let me know if you have any ideas.
Thanks guys!
You'll need to convert the String to a number first. Something like this:
int number = Integer.parseInt(number.getText().toString());
if (number > 9)
{
output.setText("50");
}
If the String is not guaranteed to be a valid integer you'll have to handle NumberFormatException too.
Is there a reason you can't use
Integer.valueOf("9");
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.