Here is your answer:
String regex = "(?<=[\\d])(,)(?=[\\d])";
Pattern p = Pattern.compile(regex);
String str = "Your input";
Matcher m = p.matcher(str);
str = m.replaceAll("");
System.out.println(str);
This only affects NUMBERS, not strings, as you asked.
Try adding that in your main method. Or try this one, it receives input:
String regex = "(?<=[\\d])(,)(?=[\\d])";
Pattern p = Pattern.compile(regex);
System.out.println("Value?: ");
Scanner scanIn = new Scanner(System.in);
String str = scanIn.next();
Matcher m = p.matcher(str);
str = m.replaceAll("");
System.out.println(str);
Answer from Whippet on Stack OverflowHere is your answer:
String regex = "(?<=[\\d])(,)(?=[\\d])";
Pattern p = Pattern.compile(regex);
String str = "Your input";
Matcher m = p.matcher(str);
str = m.replaceAll("");
System.out.println(str);
This only affects NUMBERS, not strings, as you asked.
Try adding that in your main method. Or try this one, it receives input:
String regex = "(?<=[\\d])(,)(?=[\\d])";
Pattern p = Pattern.compile(regex);
System.out.println("Value?: ");
Scanner scanIn = new Scanner(System.in);
String str = scanIn.next();
Matcher m = p.matcher(str);
str = m.replaceAll("");
System.out.println(str);
The easiest way is to use two regexes. The first to make sure it is numeric (something along the lines of [0-9.,]*), and the second to clean it (result.replaceAll("/,//"))
Java Regex Remove comma's between numbers from String - Stack Overflow
java - Removing Dollar and comma from string - Stack Overflow
java - how to remove a comma in a string - Stack Overflow
exception - How to parse number string containing commas into an integer in java? - Stack Overflow
public static void main(String args[]) throws IOException
{
String regex = "(?<=[\\d])(,)(?=[\\d])";
Pattern p = Pattern.compile(regex);
String str = "John loves cakes and he always orders them by dialing \"989,444 1234\". Johns credentials are as follows\" \"Name\":\"John\", \"Jr\", \"Mobile\":\"945,234,1110\"";
Matcher m = p.matcher(str);
str = m.replaceAll("");
System.out.println(str);
}
Output
John loves cakes and he always orders them by dialing "989444 1234". Johns credentials are as follows" "Name":"John", "Jr", "Mobile":"9452341110"
This regex uses a positive lookbehind and a positive lookahead to only match commas with a preceding digit and a following digit, without including those digits in the match itself:
(?<=\d),(?=\d)
do like this
NumberFormat format = NumberFormat.getCurrencyInstance();
Number number = format.parse("\$123,456.78");
System.out.println(number.toString());
output
123456.78
Try,
String liveprice = "$123,456.78";
String newStr = liveprice.replaceAll("[$,]", "");
replaceAll uses regex, to avoid regex than try with consecutive replace method.
String liveprice = "$1,23,456.78";
String newStr = liveprice.replace("$", "").replace(",", "");
Is this comma a decimal separator or are these two numbers? In the first case you must provide Locale to NumberFormat class that uses comma as decimal separator:
NumberFormat.getNumberInstance(Locale.FRANCE).parse("265,858")
This results in 265.858. But using US locale you'll get 265858:
NumberFormat.getNumberInstance(java.util.Locale.US).parse("265,858")
That's because in France they treat comma as decimal separator while in US - as grouping (thousand) separator.
If these are two numbers - String.split() them and parse two separate strings independently.
You can remove the , before parsing it to an int:
int i = Integer.parseInt(myNumberString.replaceAll(",", ""));
You can modify the loop
for(int i = 0; i < numbers.length - 1; i++) {
System.out.print(numbers[i] + ",");
}
System.out.print(numbers[numbers.length - 1]);
Or use streams
Arrays.stream(numbers).collect(Collectors.joining(","));
StringJoiner is used to construct a sequence of characters separated by a delimiter and optionally starting with a supplied prefix and ending with a supplied suffix. Here in this case we are using the delimiter as ,
int numbers[] = {23, 79, 41, 68, 17, 39, 51, 75, 95, 19};
StringJoiner stringJoiner = new StringJoiner(",");
System.out.print("Integer values: ");
for (int i = 0; i < numbers.length; i++) {
stringJoiner.add(String.valueOf(numbers[i]));
}
System.out.println(stringJoiner);
It seems that you are looking for modulo (reminder) operator %. Also there is no "after comma value" in integers words so 184 / 60 = 3 not 3.06666.
int time = 184;
int minutes = time / 60;
int seconds = time % 60;
System.out.println(minutes + " minutes : " + seconds + " seconds");
Output: 3 minutes : 4 seconds
You can also use Period from JodaTime library.
int time = 184;
Period period = new Period(time * 1000);//in milliseconds
System.out.printf("%d minutes, %d seconds%n", period.getMinutes(),
period.getSeconds());
which will print 3 minutes, 4 seconds.
Just use %, /, and a little math:
int totalSeconds = 184;
int minutes = totalSeconds/60; //will be 3 minutes
int seconds = totalSeconds%60; // will be 4 seconds
import java.io.File; import java.util.ArrayList; import java.util.Scanner;
public class EmailReader {
public static void main(String[] args) throws Exception {
Scanner inputFile = new Scanner(new File("NameList.csv"));
inputFile.nextLine();
ArrayList<String> studentNames = new ArrayList();
ArrayList<String> emails = new ArrayList();
String ender = "@virginia.edu";
while (inputFile.hasNext()) {
String name = inputFile.nextLine().concat("@virginia.edu");
studentNames.add(name);
}
for (int i = 0; i < studentNames.size(); i += 2) {
emails.add(studentNames.get(i));
;
}
System.out.println(emails);
}}