I have been storing phone numbers as longs and I would like to simply add hyphens when printing the phone number as a string.
I tried using DecimalFormat but that doesn't like the hyphen. Probably because it is meant for formatting decimal numbers and not longs.
long phoneFmt = 123456789L;
DecimalFormat phoneFmt = new DecimalFormat("###-###-####");
System.out.println(phoneFmt.format(phoneNum)); //doesn't work as I had hoped
Ideally, I would like to have parenthesis on the area code too.
new DecimalFormat("(###)-###-####");
What is the correct way to do this?
You can use String.replaceFirst with regex method like
long phoneNum = 123456789L;
System.out.println(String.valueOf(phoneNum).replaceFirst("(\\d{3})(\\d{3})(\\d+)", "($1)-$2-$3"));
To get your desired output:
long phoneFmt = 123456789L;
//get a 12 digits String, filling with left '0' (on the prefix)
DecimalFormat phoneDecimalFmt = new DecimalFormat("0000000000");
String phoneRawString= phoneDecimalFmt.format(phoneFmt);
java.text.MessageFormat phoneMsgFmt=new java.text.MessageFormat("({0})-{1}-{2}");
//suposing a grouping of 3-3-4
String[] phoneNumArr={phoneRawString.substring(0, 3),
phoneRawString.substring(3,6),
phoneRawString.substring(6)};
System.out.println(phoneMsgFmt.format(phoneNumArr));
The result at the Console looks like this:
(012)-345-6789
For storing phone numbers, you should consider using a data type other than numbers.
The easiest way to do this is by using the built in MaskFormatter in the javax.swing.text library.
You can do something like this :
import javax.swing.text.MaskFormatter;
String phoneMask= "###-###-####";
String phoneNumber= "123423452345";
MaskFormatter maskFormatter= new MaskFormatter(phoneMask);
maskFormatter.setValueContainsLiteralCharacters(false);
maskFormatter.valueToString(phoneNumber) ;
If you really need the right way then you can use Google's recently open sourced libphonenumber
You could also use https://github.com/googlei18n/libphonenumber. Here is an example:
import com.google.i18n.phonenumbers.NumberParseException;
import com.google.i18n.phonenumbers.PhoneNumberUtil;
import com.google.i18n.phonenumbers.Phonenumber;
String s = "18005551234";
PhoneNumberUtil phoneUtil = PhoneNumberUtil.getInstance();
Phonenumber.PhoneNumber phoneNumber = phoneUtil.parse(s, Locale.US.getCountry());
String formatted = phoneUtil.format(phoneNumber, PhoneNumberUtil.PhoneNumberFormat.NATIONAL);
Here you can get the library on your classpath: http://mvnrepository.com/artifact/com.googlecode.libphonenumber/libphonenumber
The worst possible solution would be:
StringBuilder sb = new StringBuilder();
long tmp = phoneFmt;
sb.append("(");
sb.append(tmp / 10000000);
tmp = tmp % 10000000;
sb.append(")-");
sb.apppend(tmp / 10000);
tmp = tmp % 10000000;
sb.append("-");
sb.append(tmp);
This is how I ended up doing it:
private String printPhone(Long phoneNum) {
StringBuilder sb = new StringBuilder(15);
StringBuilder temp = new StringBuilder(phoneNum.toString());
while (temp.length() < 10)
temp.insert(0, "0");
char[] chars = temp.toString().toCharArray();
sb.append("(");
for (int i = 0; i < chars.length; i++) {
if (i == 3)
sb.append(") ");
else if (i == 6)
sb.append("-");
sb.append(chars[i]);
}
return sb.toString();
}
I understand that this does not support international numbers, but I'm not writing a "real" application so I'm not concerned about that. I only accept a 10 character long as a phone number. I just wanted to print it with some formatting.
Thanks for the responses.
You can implement your own method to do that for you, I recommend you to use something such as this. Using DecimalFormat and MessageFormat. With this method you can use pretty much whatever you want (String,Integer,Float,Double) and the output will be always right.
import java.text.DecimalFormat;
import java.text.MessageFormat;
/**
* Created by Yamil Garcia Hernandez on 25/4/16.
*/
public class test {
// Constants
public static final DecimalFormat phoneFormatD = new DecimalFormat("0000000000");
public static final MessageFormat phoneFormatM = new MessageFormat("({0}) {1}-{2}");
// Example Method on a Main Class
public static void main(String... args) {
try {
System.out.println(formatPhoneNumber("8091231234"));
} catch (Exception e) {
e.printStackTrace();
}
try {
System.out.println(formatPhoneNumber("18091231234"));
} catch (Exception e) {
e.printStackTrace();
}
try {
System.out.println(formatPhoneNumber("451231234"));
} catch (Exception e) {
e.printStackTrace();
}
try {
System.out.println(formatPhoneNumber("11231234"));
} catch (Exception e) {
e.printStackTrace();
}
try {
System.out.println(formatPhoneNumber("1231234"));
} catch (Exception e) {
e.printStackTrace();
}
try {
System.out.println(formatPhoneNumber("231234"));
} catch (Exception e) {
e.printStackTrace();
}
try {
System.out.println(formatPhoneNumber(""));
} catch (Exception e) {
e.printStackTrace();
}
try {
System.out.println(formatPhoneNumber(0));
} catch (Exception e) {
e.printStackTrace();
}
try {
System.out.println(formatPhoneNumber(8091231234f));
} catch (Exception e) {
e.printStackTrace();
}
}
// Magic
public static String formatPhoneNumber(Object phone) throws Exception {
double p = 0;
if (phone instanceof String)
p = Double.valueOf((String) phone);
if (phone instanceof Integer)
p = (Integer) phone;
if (phone instanceof Float)
p = (Float) phone;
if (phone instanceof Double)
p = (Double) phone;
if (p == 0 || String.valueOf(p) == "" || String.valueOf(p).length() < 7)
throw new Exception("Paramenter is no valid");
String fot = phoneFormatD.format(p);
String extra = fot.length() > 10 ? fot.substring(0, fot.length() - 10) : "";
fot = fot.length() > 10 ? fot.substring(fot.length() - 10, fot.length()) : fot;
String[] arr = {
(fot.charAt(0) != '0') ? fot.substring(0, 3) : (fot.charAt(1) != '0') ? fot.substring(1, 3) : fot.substring(2, 3),
fot.substring(3, 6),
fot.substring(6)
};
String r = phoneFormatM.format(arr);
r = (r.contains("(0)")) ? r.replace("(0) ", "") : r;
r = (extra != "") ? ("+" + extra + " " + r) : r;
return (r);
}
}
Result will be
(809) 123-1234
+1 (809) 123-1234
(45) 123-1234
(1) 123-1234
123-1234
023-1234
java.lang.NumberFormatException: empty String
at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:1842)
at sun.misc.FloatingDecimal.parseDouble(FloatingDecimal.java:110)
at java.lang.Double.parseDouble(Double.java:538)
at java.lang.Double.valueOf(Double.java:502)
at test.formatPhoneNumber(test.java:66)
at test.main(test.java:45)
java.lang.Exception: Paramenter is no valid
at test.formatPhoneNumber(test.java:78)
at test.main(test.java:50)
(809) 123-1232
DecimalFormat doesn't allow arbitrary text within the number to be formatted, just as a prefix or a suffix. So it won't be able to help you there.
In my opinion, storing a phone number as a numeric value is wrong, entirely. What if I want to store an international number? Many countries use + to indicate a country code (e.g. +1 for USA/Canda), others use 00 (e.g. 001).
Both of those can't really be represented in a numeric data type ("Is that number 1555123 or 001555123?")
You could use the substring and concatenation for easy formatting too.
telephoneNumber = "("+telephoneNumber.substring(0, 3)+")-"+telephoneNumber.substring(3, 6)+"-"+telephoneNumber.substring(6, 10);
But one thing to note is that you must check for the lenght of the telephone number field just to make sure that your formatting is safe.
U can format any string containing non numeric characters also to your desired format use my util class to format
usage is very simple
public static void main(String[] args){
String num = "ab12345*&67890";
System.out.println(PhoneNumberUtil.formateToPhoneNumber(num,"(XXX)-XXX-XXXX",10));
}
output: (123)-456-7890
u can specify any foramt such as XXX-XXX-XXXX and length of the phone number , if input length is greater than specified length then string will be trimmed.
Get my class from here: https://github.com/gajeralalji/PhoneNumberUtil/blob/master/PhoneNumberUtil.java
Pattern phoneNumber = Pattern.compile("(\\d{3})(\\d{3})(\\d{4})");
// ...
Matcher matcher = phoneNumber(numberAsLineOf10Symbols);
if (matcher.matches) {
return "(" + matcher.group(1) + ")-" +matcher.group(2) + "-" + matcher.group(3);
}
I'd have thought you need to use a MessageFormat rather than DecimalFormat. That should be more flexible.
String formatterPhone = String.format("%s-%s-%s", phoneNumber.substring(0, 3), phoneNumber.substring(3, 6), phoneNumber.substring(6, 10));
Using StringBuilder for performance.
long number = 12345678L;
System.out.println(getPhoneFormat(String.valueOf(number)));
public static String getPhoneFormat(String number)
{
if (number == null || number.isEmpty() || number.length() < 6 || number.length() > 15)
{
return number;
}
return new StringBuilder("(").append(number.substring(0, 3))
.append(") ").append(number.substring(3, 6))
.append("-").append(number.substring(6))
.toString();
}
Kotlin
val number = 088899998888
val phone = number.phoneFormatter()
fun String.phoneFormatter(): String { return this.replace("\\B(?=(\\d{4})+(?!\\d))".toRegex(), "-") }
The result will be 0888-9999-8888
I used this one
String columValue = "1234567890
String number = columValue.replaceFirst("(\d{3})(\d{3})(\d+)", "($1) $2-$3");
Related
I want to convert the number "4,471.26" into decimal number "4471.26".
Actually number "4,471.26" is received as String and for further process, need to convert it into Decimal number.
"4,471.26" - is just a format,so value is keeps changing every-time. This value could be anything like "654,654,25.54"
Tried by considering comma (,) as group -separator. But looks like has different ASCII values.
String Tempholder = "4,471.26";
Decimal.valueOf(Tempholder.replaceAll(getGroupSeparator(), ""));
private char getGroupSeparator() {
DecimalFormat decFormat = new DecimalFormat();
DecimalFormatSymbols decSymbols = decFormat.getDecimalFormatSymbols();
return Character.valueOf(decSymbols.getGroupingSeparator());
}
Below code would be temporary solution, but it will not work other geo regions.
String Tempholder = "4,471.26";
Decimal.valueOf(Tempholder.replaceAll(",", ""));
Kindly help ..
you can do it like this :
public class JavaCodes{
public static void main(String[] args) {
String str = new String("4,233.19");
str = str.replaceFirst(",", "");
System.out.println(Double.valueOf(str));
}
}
Ok, some assumptions first
You don't know the locale beforehand
If just one separator is found, and it appears only once, we treat it as a decimal separator (for simplicity)
If just one separator is found but it appears multiple times, it's a thousands separator
Any non-digit character is a valid separator
If there are more than two non-digit characters in a string, that's an error.
So here's the algorithm
Find the first non-digit character and temporarily consider it a decimal separator
If another non-digit character is found
If it's the same as the decimal separator, and we still don't have a thousands seprator, make that the thousands separator and reset the decimal separator
If it's the same as the decimal separator, and we already have a thousands separator, that's an error
If it's not the same as the decimal separator, it's the thousands separator
Remove from the original string all occurrences of the thousands separator (if present)
Substitute the decimal separator with a dot
Parse as double.
And here is the code.
public class Test {
public static double parseNumber(String x) {
Character thousandsSeparator = null;
Character decimalSeparator = null;
for (int i = 0; i < x.length(); i++) {
if (!Character.isDigit(x.charAt(i))) {
if (decimalSeparator == null) {
decimalSeparator = x.charAt(i);
} else {
if (decimalSeparator.equals(x.charAt(i))) {
if (thousandsSeparator == null) {
thousandsSeparator = x.charAt(i);
decimalSeparator = null;
} else {
if (!thousandsSeparator.equals(x.charAt(i))) {
throw new IllegalArgumentException();
}
}
} else {
thousandsSeparator = x.charAt(i);
}
}
}
}
// remove thousands separator
if (thousandsSeparator != null) {
int formerSeparatorPosition;
while ((formerSeparatorPosition = x.indexOf(thousandsSeparator)) != -1) {
x = x.substring(0, formerSeparatorPosition) + x.substring(formerSeparatorPosition + 1);
}
}
// replace decimal separator with a dot
if (decimalSeparator != null) {
x = x.replace(decimalSeparator, '.');
}
return Double.parseDouble(x);
}
public static void main(String args[]) {
System.out.println(parseNumber("123.45"));
System.out.println(parseNumber("123,45"));
System.out.println(parseNumber("1.234,5"));
System.out.println(parseNumber("1,234.5"));
System.out.println(parseNumber("1,234,567.512"));
System.out.println(parseNumber("1.234.567,512"));
ystem.out.println(parseNumber("1.234.567"));
System.out.println(parseNumber("1_234_567|34")); // works with any two characters, if there are just two
try {
System.out.println(parseNumber("1_234_567|34,7")); // invalid
} catch (IllegalArgumentException e) {
System.out.println("Too many separators!");
}
}
}
It is quite circumstantial to parse to a BigDecimal. To a double it is easier
but floating point is just an approximation, and will loose the number's precision. 0.2 == 0.20 == 0.200 == (actually something like) 0.1999999987.
String text = "4,471.26";
DecimalFormat format = (DecimalFormat) DecimalFormat.getInstance(Locale.US);
format.setParseBigDecimal(true);
BigDecimal number = (BigDecimal) format.parseObject(text);
// number.scale() == 2
The US locale uses your desired separators.
Thank you guys, for all your suggestion and helps. I figure out the below solution. Convert it into decimal number, And applicable to all geo region.
public static void geoSp() {
String a = "4.233,19";
NumberFormat as = NumberFormat.getInstance();
double myNumber = 0;
try {
myNumber = as.parse(a).doubleValue();
} catch (ParseException e) {
e.printStackTrace();
}
System.out.println(String.valueOf(myNumber));
}
gives Out put :- 4233.19
I want to check is a String I pass to Integer.valueOf(String s) a valid String to parse. If one is not parsable I need to return 0.
I do it in the following way:
try{
Integer.valueOf(String s)
} catch(NumberFormatException e) {
return 0;
}
Is it a bad way to do that?
method 1: use a regular expression to check for validity of being a number
public static int parseStrToInt(String str) {
if (str.matches("\\d+")) {
return Integer.parseInt(str);
} else {
return 0;
}
}
method 2: use Java's built-in java.text.NumberFormat object to see if, after parsing the string the parser position is at the end of the string. If it is, we can assume the entire string is numeric
public static int strToInt(String str) {
NumberFormat formatter = NumberFormat.getInstance();
ParsePosition pos = new ParsePosition(0);
formatter.parse(str, pos);
if (str.length() == pos.getIndex()) {
return Integer.parseInt(str);
} else {
return 0;
}
}
I would have used:
s = s.trim(); // sometimes user inputs white spaces without knowing it
int value;
if (s.length() == 0) {
value = 0; // obviously not a string
} else {
try{
value = Integer.valueOf(s);
} catch(NumberFormatException e) {
value = 0;
}
}
// do whatever you like here
Hi this will do even the number is double or long, which is useful always while parsing.
List<String> myStrings = new ArrayList<String>();
myStrings.add("text");
myStrings.add("25");
myStrings.add("102.23333333");
myStrings.add("22.34");
NumberFormat nf = NumberFormat.getInstance();
for( String text : myStrings){
try {
System.out.println( nf.parse(text));
} catch (ParseException e) {
e.printStackTrace();
}
}
My password condition is, minimum 8 characters, minimum one special character, minimum one numeric
For this I wrote a simple class to verify, but eventually fails.
Any help is highly appreciated.
public class PasswordVerifier {
private static final String SPECIAL_CHARACTERS = "(`~!##$%^&*()_+=-][;'/.,\\<>?|:\"}{)";
public static void main(String... args) {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
try {
String password = in.readLine();
if(!password.matches("^.*(?=.{8,})(?=.*[0-9])(?=.*[SPECIAL_CHARACTERS]).*$")){
System.out.println("Password does not satisfy compliant");
} else {
System.out.println("Yes.. gets through");
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
This might work for your requirement:
private static final String SPECIAL_CHARACTERS = "(`~!##$%^&*()_+=-\\]\\[;'/.,\\<>?|:\"}{)";
public static void main(String[] args) {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
try {
String password = in.readLine();
if(!password.matches("((?=.*\\d)(?=.*["+SPECIAL_CHARACTERS+"]).{8,})")){
System.out.println("Password does not satisfy compliant");
} else {
System.out.println("Yes.. gets through");
}
} catch (IOException e) {
e.printStackTrace();
}
}
The regex specifies:
Input must contains one digit from 0-9
Must contains one special symbols in the list of SPECIAL_CHARACTERS that you've defined
Length should be at least 8 characters
I would not bother trying to write a regular expression. A r.e. that includes all your conditions and nothing more would be difficult to write, harder to understand, and probably not terribly efficient. Just code your requirements explicitly:
boolean isAcceptablePassword(String pwd) {
boolean numeric = false, special = false;
if (pwd.length() >= 8) {
for (int i = pwd.length() - 1; !numeric && !special && i >= 0; --i) {
char c = pwd.charAt(i);
numeric = numeric || Character.isDigit();
special = special || SPECIAL_CHARACTERS.indexOf(c) >= 0;
}
}
return numeric && special;
}
You cannot have ] or - in the middle if a character class, because they have a meaning for the character class syntax. If you want them in there, they have to be the first two elements in the class, - before ].
I'd like to do a function which gets a string and in case it has inline comments it removes it. I know it sounds pretty simple but i wanna make sure im doing this right, for example:
private String filterString(String code) {
// lets say code = "some code //comment inside"
// return the string "some code" (without the comment)
}
I thought about 2 ways: feel free to advice otherwise
Iterating the string and finding double inline brackets and using substring method.
regex way.. (im not so sure bout it)
can u tell me what's the best way and show me how it should be done? (please don't advice too advanced solutions)
edited: can this be done somehow with Scanner object? (im using this object anyway)
If you want a more efficient regex to really match all types of comments, use this one :
replaceAll("(?:/\\*(?:[^*]|(?:\\*+[^*/]))*\\*+/)|(?://.*)","");
source : http://ostermiller.org/findcomment.html
EDIT:
Another solution, if you're not sure about using regex is to design a small automata like follows :
public static String removeComments(String code){
final int outsideComment=0;
final int insideLineComment=1;
final int insideblockComment=2;
final int insideblockComment_noNewLineYet=3; // we want to have at least one new line in the result if the block is not inline.
int currentState=outsideComment;
String endResult="";
Scanner s= new Scanner(code);
s.useDelimiter("");
while(s.hasNext()){
String c=s.next();
switch(currentState){
case outsideComment:
if(c.equals("/") && s.hasNext()){
String c2=s.next();
if(c2.equals("/"))
currentState=insideLineComment;
else if(c2.equals("*")){
currentState=insideblockComment_noNewLineYet;
}
else
endResult+=c+c2;
}
else
endResult+=c;
break;
case insideLineComment:
if(c.equals("\n")){
currentState=outsideComment;
endResult+="\n";
}
break;
case insideblockComment_noNewLineYet:
if(c.equals("\n")){
endResult+="\n";
currentState=insideblockComment;
}
case insideblockComment:
while(c.equals("*") && s.hasNext()){
String c2=s.next();
if(c2.equals("/")){
currentState=outsideComment;
break;
}
}
}
}
s.close();
return endResult;
}
The best way to do this is to use regular expressions.
At first to find the /**/ comments and then remove all // commnets. For example:
private String filterString(String code) {
String partialFiltered = code.replaceAll("/\\*.*\\*/", "");
String fullFiltered = partialFiltered.replaceAll("//.*(?=\\n)", "")
}
Just use the replaceAll method from the String class, combined with a simple regular expression. Here's how to do it:
import java.util.*;
import java.lang.*;
class Main
{
public static void main (String[] args) throws java.lang.Exception
{
String s = "private String filterString(String code) {\n" +
" // lets say code = \"some code //comment inside\"\n" +
" // return the string \"some code\" (without the comment)\n}";
s = s.replaceAll("//.*?\n","\n");
System.out.println("s=" + s);
}
}
The key is the line:
s = s.replaceAll("//.*?\n","\n");
The regex //.*?\n matches strings starting with // until the end of the line.
And if you want to see this code in action, go here: http://www.ideone.com/e26Ve
Hope it helps!
To find the substring before a constant substring using a regular expression replacement is a bit much.
You can do it using indexOf() to check for the position of the comment start and substring() to get the first part, something like:
String code = "some code // comment";
int offset = code.indexOf("//");
if (-1 != offset) {
code = code.substring(0, offset);
}
#Christian Hujer has been correctly pointing out that many or all of the solutions posted fail if the comments occur within a string.
#Loïc Gammaitoni suggests that his automata approach could easily be extended to handle that case. Here is that extension.
enum State { outsideComment, insideLineComment, insideblockComment, insideblockComment_noNewLineYet, insideString };
public static String removeComments(String code) {
State state = State.outsideComment;
StringBuilder result = new StringBuilder();
Scanner s = new Scanner(code);
s.useDelimiter("");
while (s.hasNext()) {
String c = s.next();
switch (state) {
case outsideComment:
if (c.equals("/") && s.hasNext()) {
String c2 = s.next();
if (c2.equals("/"))
state = State.insideLineComment;
else if (c2.equals("*")) {
state = State.insideblockComment_noNewLineYet;
} else {
result.append(c).append(c2);
}
} else {
result.append(c);
if (c.equals("\"")) {
state = State.insideString;
}
}
break;
case insideString:
result.append(c);
if (c.equals("\"")) {
state = State.outsideComment;
} else if (c.equals("\\") && s.hasNext()) {
result.append(s.next());
}
break;
case insideLineComment:
if (c.equals("\n")) {
state = State.outsideComment;
result.append("\n");
}
break;
case insideblockComment_noNewLineYet:
if (c.equals("\n")) {
result.append("\n");
state = State.insideblockComment;
}
case insideblockComment:
while (c.equals("*") && s.hasNext()) {
String c2 = s.next();
if (c2.equals("/")) {
state = State.outsideComment;
break;
}
}
}
}
s.close();
return result.toString();
}
I made an open source library (on GitHub) for this purpose , its called CommentRemover you can remove single line and multiple line Java Comments.
It supports remove or NOT remove TODO's.
Also it supports JavaScript , HTML , CSS , Properties , JSP and XML Comments too.
Little code snippet how to use it (There is 2 type usage):
First way InternalPath
public static void main(String[] args) throws CommentRemoverException {
// root dir is: /Users/user/Projects/MyProject
// example for startInternalPath
CommentRemover commentRemover = new CommentRemover.CommentRemoverBuilder()
.removeJava(true) // Remove Java file Comments....
.removeJavaScript(true) // Remove JavaScript file Comments....
.removeJSP(true) // etc.. goes like that
.removeTodos(false) // Do Not Touch Todos (leave them alone)
.removeSingleLines(true) // Remove single line type comments
.removeMultiLines(true) // Remove multiple type comments
.startInternalPath("src.main.app") // Starts from {rootDir}/src/main/app , leave it empty string when you want to start from root dir
.setExcludePackages(new String[]{"src.main.java.app.pattern"}) // Refers to {rootDir}/src/main/java/app/pattern and skips this directory
.build();
CommentProcessor commentProcessor = new CommentProcessor(commentRemover);
commentProcessor.start();
}
Second way ExternalPath
public static void main(String[] args) throws CommentRemoverException {
// example for externalPath
CommentRemover commentRemover = new CommentRemover.CommentRemoverBuilder()
.removeJava(true) // Remove Java file Comments....
.removeJavaScript(true) // Remove JavaScript file Comments....
.removeJSP(true) // etc..
.removeTodos(true) // Remove todos
.removeSingleLines(false) // Do not remove single line type comments
.removeMultiLines(true) // Remove multiple type comments
.startExternalPath("/Users/user/Projects/MyOtherProject")// Give it full path for external directories
.setExcludePackages(new String[]{"src.main.java.model"}) // Refers to /Users/user/Projects/MyOtherProject/src/main/java/model and skips this directory.
.build();
CommentProcessor commentProcessor = new CommentProcessor(commentRemover);
commentProcessor.start();
}
for scanner, use a delimiter,
delimiter example.
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;
public class MainClass {
public static void main(String args[]) throws IOException {
FileWriter fout = new FileWriter("test.txt");
fout.write("2, 3.4, 5,6, 7.4, 9.1, 10.5, done");
fout.close();
FileReader fin = new FileReader("Test.txt");
Scanner src = new Scanner(fin);
// Set delimiters to space and comma.
// ", *" tells Scanner to match a comma and zero or more spaces as
// delimiters.
src.useDelimiter(", *");
// Read and sum numbers.
while (src.hasNext()) {
if (src.hasNextDouble()) {
System.out.println(src.nextDouble());
} else {
break;
}
}
fin.close();
}
}
Use a tokenizer for a normal string
tokenizer:
// start with a String of space-separated words
String tags = "pizza pepperoni food cheese";
// convert each tag to a token
StringTokenizer st = new StringTokenizer(tags," ");
while ( st.hasMoreTokens() )
{
String token = (String)st.nextToken();
System.out.println(token);
}
http://www.devdaily.com/blog/post/java/java-faq-stringtokenizer-example
It will be better if code handles single line comment and multi line comment separately . Any suggestions ?
public class RemovingCommentsFromFile {
public static void main(String[] args) throws IOException {
BufferedReader fin = new BufferedReader(new FileReader("/home/pathtofilewithcomments/File"));
BufferedWriter fout = new BufferedWriter(new FileWriter("/home/result/File1"));
boolean multilinecomment = false;
boolean singlelinecomment = false;
int len,j;
String s = null;
while ((s = fin.readLine()) != null) {
StringBuilder obj = new StringBuilder(s);
len = obj.length();
for (int i = 0; i < len; i++) {
for (j = i; j < len; j++) {
if (obj.charAt(j) == '/' && obj.charAt(j + 1) == '*') {
j += 2;
multilinecomment = true;
continue;
} else if (obj.charAt(j) == '/' && obj.charAt(j + 1) == '/') {
singlelinecomment = true;
j = len;
break;
} else if (obj.charAt(j) == '*' && obj.charAt(j + 1) == '/') {
j += 2;
multilinecomment = false;
break;
} else if (multilinecomment == true)
continue;
else
break;
}
if (j == len)
{
singlelinecomment=false;
break;
}
else
i = j;
System.out.print((char)obj.charAt(i));
fout.write((char)obj.charAt(i));
}
System.out.println();
fout.write((char)10);
}
fin.close();
fout.close();
}
Easy solution that doesn't remove extra parts of code (like those above)
// works for any reader, you can also iterate over list of strings instead
String str="";
String s;
while ((s = reader.readLine()) != null)
{
s=s.replaceAll("//.*","\n");
str+=s;
}
str=str.replaceAll("/\\*.*\\*/"," ");
I try to format a number using NumberFormatter in Android. I use the code bellow and it works perfectly:
NumberFormat formatter = new DecimalFormat("###,###");
String myFormattedString = formatter.format(123456);
But, when i use a pattern with space, like that: new DecimalFormat("###,# ##"); it throws an IllegalArgumentException. I've read documentation about NumberFormatter and DecimalFormatter and found nothing about spaces in patterns. Can anyone explane me why can't i use spaces or how to add them as allowed characters.
Thanks in advance!!!
You can not put spaces in the middle of a number: it is not a valid format.
If you look at the JavaDoc of DecimalFormat, you'll see this:
Prefix:
any Unicode characters except \uFFFE, \uFFFF, and special characters
Suffix:
any Unicode characters except \uFFFE, \uFFFF, and special characters
Number:
Integer Exponentopt
Integer . Fraction Exponentopt
Without copying the entire doc, none of the components of the Number pattern accept spaces, so trying to fit a space in the middle will not work. You can only use spaces in the prefix or suffix.
In a regular JDK this does not throw an exception - it just formats the number as 123,456.
It is not clear what is the space in your example. You have a couple of options for a symbol's role:
decimal separator
group separator
exponent separator
monetary decimal separator
You can set each of these with:
DecimalFormatSymbols symbols = formatter.getDecimalFormatSymbols();
symbols.setGroupingSeparator(' ');
formatter.setSymbols(symbols);
I've achieved my goal by making my own formatter using standart formatter and finding prohibited symbols using exeptions. Hope it will be useful for someone else.
public static String getFormattedNumberWithPattern(String aPattern,
float aNumber) {
String lFormattedNumber = null;
String lOriginalPattern = aPattern;
try {
Hashtable<Integer, String> lIlligalChars = new Hashtable<Integer, String>();
// analyze illegal characters
for (int i = aPattern.length() - 1; i >= 0; i--) {
char[] lAux = new char[1];
aPattern.getChars(i, i + 1, lAux, 0);
try {
// if character is illegal, it throws an exception
#SuppressWarnings("unused")
NumberFormat lNumberFormatter = new DecimalFormat("#"
+ lAux[0] + "#");
} catch (IllegalArgumentException e) {
// add illegal chars and indexes to dictionary
lIlligalChars.put(new Integer(i), String.valueOf(lAux[0]));}}
Enumeration<String> lChars = lIlligalChars.elements();
while (lChars.hasMoreElements()) {
String lIllegalChar = lChars.nextElement();
// remove illegal chars from pattern
aPattern = removeChar(aPattern, lIllegalChar.charAt(0));
}
// format number using legal pattern
NumberFormat lNumberFormatter = new DecimalFormat(aPattern);
lFormattedNumber = lNumberFormatter.format(aNumber);
int lLenghtDifference = lOriginalPattern.length()
- lFormattedNumber.length();
// add illegal chars to formatted string using their indexes
Enumeration<Integer> lKeys = lIlligalChars.keys();
while (lKeys.hasMoreElements()) {
Integer lIllegalCharIndex = lKeys.nextElement();
int lInsertIndex = lIllegalCharIndex.intValue()
- lLenghtDifference;
// insert illegal chars into formatted number
if (lInsertIndex >= 0
|| lInsertIndex < lFormattedNumber.length()) {
lFormattedNumber = new StringBuffer(lFormattedNumber)
.insert(lInsertIndex,
lIlligalChars.get(lIllegalCharIndex)
.charAt(0)).toString();
}
}
} catch (Exception e) {
// Log.d("info", "formater error:" + e + "mask: " + aPattern
// + " number:" + aNumber);
}
return lFormattedNumber;
}
public static String removeChar(String s, char c) {
StringBuffer r = new StringBuffer(s.length());
r.setLength(s.length());
int current = 0;
for (int i = 0; i < s.length(); i++) {
char cur = s.charAt(i);
if (cur != c)
r.setCharAt(current++, cur);
}
r.setLength(current);
return r.toString();
}