format of int value, change how it is displayed - java

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}"

Related

Using variables within printf format section

I've been working with the printf function for a bit now and was wondering if there was a way to use declared variables within the formatting section of printf? Something like:
int x = 5;
System.out.printf("%0xs\n", text);
// Normally this would be "%05s\n"
Meaning that I can use "x" as a changeable variable to be able to change how many 0 it can have. I am asking because I was given a code where the first line will give me a number, which is the amount of 0 I have to put before the text. Is something like this possible?
I don't think that you can do it in a singular String.format statement. However I was able to do with nested format.
final int padding = 5;
System.out.printf(String.format("%%0%dd%%n", padding), 7);
System.out.printf(String.format("%%%ds%%n", padding), "hey");
Output:
00007
hey
Also, you can use %n to insert an end line character automatically.

Get an Integer that can be modified from a String?

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

Convert int to hex string need a better way

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

How do you handle incomplete data in a Processing table?

I'm parsing a CSV using Processing's Table interface, but some rows are missing some data. I want to pull all the data available into my table, but I'm not sure how to handle the missing data--I keep getting NullPointerException when I loop over the table with dataTable.getInt on the missing values.
I don't have a background in statically typed languages, so I've no idea how to conditionally assign this data short of putting a separate try/catch around each assignment. Surely there's a better way?
Before calling dataTable.getInt method check if dataTable is not null like
if(dataTable != null) {
int my_nt = dataTable.getInt
}
//else skip since it is empty
Since your're using getInt--you should perform a regex search/replace ,<not numeric>, with ,<some int>,. In your case it may be as simple as replacing ,, with ,0,
Also, as Hassan suggests, double check that dataTable is not null.
Ok, so I figured out a way to do this:
First, call dataTable.makeNullEmpty(), which turns all the null values into empty strings.
Then, you can use a pattern like this:
String total_value = dataTable.getString(i, 4);
if(total_value.length() > 0) s.total_value = parseInt(total_value);
and you get assignment only if an int is there to be parsed.

small java problem

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.

Categories

Resources