Is there an equivilent of vb.nets StringValue.ToString("0000") so that it returns the string as four numbers. I'm trying to work it on :
public static String getNextID(int stationID, String tablename) {
String rtnID;
rtnID = Integer.toString(stationID) + "-" + getNextID(tablename);
return rtnID;
}
So that the value of rtnID is 4 characters long and it's added 0's in the right place if needed
Tom
Edit: heres what I now have that isn't working:
public static String getNextID(int stationID, String tablename) {
String rtnID;
NumberFormat formatter = new DecimalFormat("0000");
String s = formatter.format(String.valueOf(stationID));
rtnID = s + "-" + getNextID(tablename);
return rtnID;
}
With this error: http://pastebin.com/XpvzkC5D
You can use this:
NumberFormat formatter = new DecimalFormat("0000");
String s = formatter.format(stationID);
Related
I have to remove this.
String removeKey = "problem_keys";
Code
public class Solution {
public static void main(String args[]) {
String removeKey = "problem_keys";
String url = "{\"arrival_mode:Self Drop;schedule:2020-09-10;payment_mode:Pay Online;address_id:67052;problem_id:11;problem_name:Abnormal noise;first:2000;product_name:demo tc;category_name:selfby;brand_name:bpl;transaction_type:Request;type:Display;problem_keys:1,35,3,4,5,6,7,15,16,11,12,16;\";}";
StringBuffer sb = new StringBuffer(url);
removeKey(sb, removeKey);
System.out.println(sb.toString());
}
public static void removeKey(StringBuffer url, String removeKey) {
int sIndex = url.indexOf(removeKey);
while (url.charAt(sIndex) != ';') {
url.deleteCharAt(sIndex);
}
url.deleteCharAt(sIndex);
}
}
Expected output.
{"arrival_mode:Self Drop;schedule:2020-09-10;payment_mode:Pay Online;address_id:67052;problem_id:11;problem_name:Abnormal noise;first:2000;product_name:demo tc;category_name:selfby;brand_name:bpl;transaction_type:Request;type:Display;}";
Guessing you want also to remove the "values", using java 8:
String toRemove = Arrays.stream(url.split(";")).filter(part -> part.contains(removeKey)).findFirst().orElse("");
String newUrl = url.replace(toRemove, "");
System.out.println(newUrl);
Speaking about the delimeters you can consider adding ";" to the toRemove string in a conditional block.
If you're aim is only to get rid of the string removeKey you can just:
url.replace(removeKey, "");
I would go with this:
String removeKey = "problem_keys;";
url = url.replace(removeKey, "") // Replace the whole "problem_keys;" string with an empty string
package com.webom.crypt;
import org.apache.commons.lang3.StringEscapeUtils;
import com.ibm.icu.text.Transliterator;
public class Test {
public static String ARABIC_TO_LATIN = "Any-Arabic";
public static String ARABIC_TO_LATIN_NO_ACCENTS = "Arabic-Latin/BGN; nfd; [:nonspacing mark:] remove; nfc";
public static void main(String[] args) {
String ARABICString = "صدام حسين التكريتي";
String unicodeCodes = StringEscapeUtils.escapeJava(ARABICString);
System.out.println("Unicode codes:" + unicodeCodes);
// conversion
Transliterator ARABICToLatinTrans = Transliterator.getInstance(ARABIC_TO_LATIN);
String result1 = ARABICToLatinTrans.transliterate(ARABICString);
System.out.println("ARABIC to Latin:" + result1);
// conversion
Transliterator ARABICToLatinNoAccentsTrans = Transliterator.getInstance(ARABIC_TO_LATIN_NO_ACCENTS);
String result2 = ARABICToLatinNoAccentsTrans.transliterate(ARABICString);
System.out.println("ARABIC to Latin (no accents):" + result2);
}
}
As conversion of arabic to latin fails because there is issue regarding to the instances .Could you please find out the correct instance string? As when you use google translator it will show exact conversion.
I am trying to import a csv with a date in the following format:
2018/01/25
the date format in mysql is in Int and I want to remove the slashes in the current date format.
I need to get the date looking like this:
20180125
I need to work only on the first column and not the rest of the csv.
here is the code for reading the csv.
while(reader.readLine()!= null)
{
String read = reader.readLine();//bufferedreader string variable
String [] rawRow = read.split(",");
String lastEntry = rawRow[rawRow.length-1];// this contains MemberNo/branchNo
String [] properLastEntry = lastEntry.split("/");//this contains MemberNo,BranchNo
//memory that contains rawRow nd properLastEntry in a single array
String [] oneRow = new String[rawRow.length+1];
System.arraycopy(rawRow, 0, oneRow, 0,rawRow.length-1);
System.arraycopy(properLastEntry, 0, oneRow, oneRow.length - properLastEntry.length,properLastEntry.length);
System.out.println(Arrays.toString(oneRow));
rs2.add(oneRow);
}
Why wouldn't it come from the database in a java.sql.Date object?
Well, if you're reading it as a string, just do this:
String convert(String dateIn) {
String year = dateIn.subString(0,4);
String month = dateIn.subString(5,7);
String day = dateIn.subString(8);
return year + month + day;
}
Of course it's still a very bad idea to read the date as a String from the database, because the format is not guaranteed to be the same on every server. Could be one on your test server and another on the production.
/**
*
* #author CHIRAG
*
*/
public class DateConverter {
public static void main(String... args) {
String date = "2018/01/25";
System.out.println(date + " converted to -> " + convertDate(date));
}
public static String convertDate(String date) {
return date.replaceAll("/|-", ""); // it will remove all occurrences of '/' or '-'
}
}
The output of this code snip is:
2018/01/25 converted to -> 20180125
Only you have to pass date string to the method convertDate(String date)
Happy Coding :)
I have a null value in my message or println that i want to delete, all succeed when i just using code like this.
the message before :
message = 2014-06-02 14:53:37.103 null tes
Here is the code that delete the null word.
public static void main(String[] args) {
//final int month = Integer.parseInt(period[0]), year = Integer.parseInt(period[1]);
Date x = new Date();
Timestamp t = new Timestamp(x.getTime());
String a = "null";
String b = t+" " +a + " tes";
String tes = (b.trim()!= null && b.trim().length()>=23) ? b.trim().replaceFirst(b.trim().substring(0,28), ""+t) : b;
System.out.println("message = " + tes);
}
The printout is right. its like this :
message = 2014-06-02 14:53:37.103 tes
But when i insert this | the printout gone wrong. i'm using that as a separator.
This is the code that went wrong.
public static void main(String[] args) {
//final int month = Integer.parseInt(period[0]), year = Integer.parseInt(period[1]);
Date x = new Date();
Timestamp t = new Timestamp(x.getTime());
String a = "null";
String b = t+"| " +a + " tes";
String tes = (b.trim()!= null && b.trim().length()>=23) ? b.trim().replaceFirst(b.trim().substring(0,28), ""+t) : b;
System.out.println("message = " + tes);
}
And this is the print out :
message = 2014-06-02 14:58:03.148| null tes
What happen to the second code actually?
Thanks
As Boris said there are other ways, but the main problem is that replaceFirst takes a regex and pipe is a special character in regex.
Below with the introduction of Pattern.quote the code should work:
b.trim().replaceFirst(Pattern.quote(b.trim().substring(0,28)), ""+t)
If you want to simply strip out the 'null' text string without the regEx issues above, and quickly and cleanly, just replace the 'null' String directly.
b.replace("null", "")
What is exactly the main purpose of the program? if it is just to remove the null in the String, I would do as JamasA says. Otherwise if you want to get the timestamp and the "tes" string in two clean strings I would do it in this way:
String tes = b.replace("null", "");
String[] aux = tes.split("\\|");
for (int i = 0; i < aux.length; i++) {
System.out.println(aux[i].trim());
}
In this way you get both information separated.I hope it will helpful for you :)
How does one get a Locale from a String containing both characters and numberals? Is it even possible? I'm not terribly advanced in java, and I'm afraid this is getting into the deeper end of the figurative pool.
Say I have;
String userInput = "250.00 SEK";
From this I want to device the means to print "Sweden".
So all I know is the currency code, and I'm wanting the DisplayName for that locale.
Assuming all currencies have 3 characters (http://www.xe.com/iso4217.php) you could just
String code = userInput.substring(userInput.length()-3,userInput.length());
state = Currency.valueOf(code);
system.out.print(state.name);
public enum Currency{
SEK("Sweden"),
GBP("British Pound");
private String name;
public Currency(String n) { this.name = n; }
public String toString() { return this.name; }
}
I think your only hope is to build a Map<String, Locale> by hand and do lookups based on the country code at the end of your currency string. There is no automated way to go from a currency to a locale (at least that I am aware of)
You could use the Currency class in combination with the codes used in ISO 4217
String userInput = "250.00 SEK";
final Set<Currency> availableCurrencies = Currency.getAvailableCurrencies();
for (Currency availableCurrency : availableCurrencies) {
final String currencyCode = availableCurrency.getCurrencyCode();
final String displayName = availableCurrency.getDisplayName();
if (userInput.contains(currencyCode)){
System.out.println("Currency is " + availableCurrency.getDisplayName());
}
}
Currency is Swedish Krona
Assuming that your string will always have space in it you can split it by space and then get currency instance by the second value in the returned array.
String userInput = "250.00 SEK";
String[] parts = userInput.split(" ");
Currency currency = Currency.getInstance(parts[1]);
System.out.print(currency.getDisplayName());
String userInput = "250.00 SEK";
final Set<Currency> availableCurrencies = Currency.getAvailableCurrencies();
for (Currency availableCurrency : availableCurrencies) {
final String strCode = availableCurrency.getCurrencyCode();
final String currencyCode = strCode;
if (userInput.contains(currencyCode)){
for (Locale locale : NumberFormat.getAvailableLocales()) {
String code = NumberFormat.getCurrencyInstance(locale).getCurrency().getCurrencyCode();
if (strCode.equals(code)) {
System.out.println(locale.getDisplayCountry());;
}
}
}
}
Will give you: Sweden