How to convert MAC string to colon-separated string - java

I have csv file with 200k rows and 3 types of MAC address defined as:
ECE1A9312000
E8:6D:52:75:2D:16
24-C9-A1-15-89-B0
My goal to stay only with colon-separated form.
So to convert - to : is not big deal:
mac = mac.replace("-", ":");
But how to convert ECE1A9312000 to EC:E1:A9:31:20:00.
I thought to use regex but its too expensive to use groups for so many data (~80k).
Do I need to run over each char and append :
like:
for(int i=0; i<mac.length(); i++){
ch = mac.charAt(i);
if(i % 2 == 0 && i != 0){
tmp += ':';
}
tmp += ch;
}
or there is more efficient way?
Thank you,

I threw together a totally unoptimized program based on your discarded regex approach and timed it. It completed in 650 ms (250 ms with warmup). The slowest part doesn't involve the regex, but String.format. If we replace it with a straight StringBuilder approach, the time drops to 40 ms.
public class Test {
static Pattern regex = Pattern.compile("(..)(..)(..)(..)(..)(..)");
public static void main(String[] args) {
final List<String> inMacs = new ArrayList<>(), outMacs = new ArrayList<>();
for (int i = 0; i < 80_000; i++) inMacs.add(mac());
final long start = System.nanoTime();
for (String mac : inMacs) {
final Matcher m = regex.matcher(mac);
m.matches();
outMacs.add(String.format("%s:%s:%s:%s:%s:%s",
m.group(1), m.group(2), m.group(3), m.group(4), m.group(5), m.group(6)));
}
System.out.println("Took " + (System.nanoTime() - start)/1_000_000 + " milliseconds");
final Iterator<String> it = outMacs.iterator();
for (int i = 0; i < 100; i++) System.out.println(it.next());
}
static Random rnd = new Random();
static String mac() {
final long mac = (long) (rnd.nextDouble()*(1L<<48));
return String.format("%012x", mac).toUpperCase();
}
}
If you are really looking for a fast solution, then avoid the regex and use a simple test to detect your MAC format:
static List<String> fixMacs(List<String> inMacs) {
final List<String> outMacs = new ArrayList<>(inMacs.size());
for (String mac : inMacs) outMacs.add(
mac.charAt(2) == '-'? mac.replace("-", ":")
: mac.charAt(2) != ':'? fixMac(mac)
: mac);
return outMacs;
}
static String fixMac(String inMac) {
final StringBuilder b = new StringBuilder(18);
for (int i = 0; i < inMac.length(); i++) {
b.append(inMac.charAt(i));
if (i%2 == 1 && i != inMac.length()-1) b.append(':');
}
return b.toString();
}
With this approach I measured just 8 ms for your 80,000 MACs.

Iterate through it character by character, every two steps if the character found is '-' replace it with ':', if it's a letter or a number then insert a ':' character.

try this
String x="ECE1A9312000";
String finals="";
for(int i=0;i<x.length();i=i+2)
{
if((i+2)<x.length())
finals+=x.substring(i, i+2)+":";
if((i+2)==x.length())
{
finals+=x.substring(i, i+2);
}
}
System.out.println(finals);
output
EC:E1:A9:31:20:00

Split every 2 chars with regexp and join with delimiter using String.join:
public static String convertToColonSeparatedMac(String mac) {
if (mac.contains(":"))
return mac;
if (mac.contains("-"))
return mac.replaceAll("-", ":");
return String.join(":", mac.split("(?<=\\G.{2})"));
}
And you may validate it before converting:
private static final Pattern MAC_PATTERN = Pattern.compile("(^([0-9A-Fa-f]{2}[:-]){5}([0-9A-Fa-f]{2})$)|([0-9A-Fa-f]{12})");
public static boolean isValidMac(String mac) {
return MAC_PATTERN.matcher(mac).matches();
}

String mac[] = {"ECE1A9312000", "24-C9-A1-15-89-B0", "E8:6D:52:75:2D:16"};
for (int i = 0; i< mac.length; i++)
{
if (mac[i].charAt(2) == '-')
mac[i] = mac[i].replace("-", ":");
else if (mac[i].charAt(2) != ':')
mac[i] = new StringBuilder(mac[i].substring(0,2)).append(":").append(mac[i].substring(2,4))
.append(":").append(mac[i].substring(4,6)).append(":").append(mac[i].substring(6,8))
.append(":").append(mac[i].substring(8,10)).append(":").append(mac[i].substring(10)).toString();
}
for (int i = 0; i< mac.length; i++)
System.out.println(mac[i]);
OUTPUT:
EC:E1:A9:31:20:00
24:C9:A1:15:89:B0
E8:6D:52:75:2D:16

Related

Print String words reverse - edge cases

I try to create a function that changes the String from StackOverflow is the best. to best. the is StackOverflow.
I wrote the following function, but can't seem to fix the spaces in the result string. For some reason, I receive best.the is Stackoverflow. There is no space between best. & the, and there is an extra space after StackOverflow.
I could add a variable that represents space and use if's in the edge cases, but I believe that there is a better way to do so.
Could anyone help me figure this out?
public static void main(String[] args) {
String str = "Stackoverflow is the best.";
String result = change(str);
System.out.println(result);
}
private static String change(String str) {
String result = "";
int i1 = str.length()-1;
int i2 = str.length();
for (i1 = str.length(); i1 >= 0; i1--) {
if (i1 ==0 || str.charAt(i1-1) == ' ') {
result = result.concat(str.substring(i1, i2));
i2 = i1;
}
}
return result;
}
One way i could think of without using if's is :
String line = "Stackoverflow is the best.";
String delimeter = " ";
final String[] words = line.split(delimeter);
String reversedLine = "";
for(int i = words.length - 1; i >= 0; i--) {
reversedLine += words[i] + delimeter;
}
// remove the delimeter present at last of line
reversedLine = reversedLine.substring(0, reversedLine.length() - 1);
System.out.println(reversedLine);
To generate the output as you have mentioned, I would approach a problem in this way:
class Solution {
public static void main(String[] args) {
String str = "StackOverflow is the best.";
String[] arr = str.split(" ");
System.out.print(arr[arr.length-1]);
for(int i = arr.length - 2; i >= 0; i--){
System.out.print(" "+arr[i]);
}
}
}
The only reason to your problem is that you haven't added the space after the '.'
Try using String str = "Stackoverflow is the best. ";
Hope it helped... :)

How to split a string after 2nd occurrence of dot(.) in java

I have a string which looks something like this(the most basic form):
String str = "1.0.0.190"
The str can be something like this as well:
1.11.0.12 or 2.111.1.190 or 1.0.0.0
I want to split the string at the 2nd occurrence of the dot(.). How can I achieve that ?
Output:
String str = "1.0.0.190"
String output = "1.0"
I'd fit the answer to OP's level, so I wouldn't recommend split or regexps to him...
If you need substring to second dot, simply find second dot and cut the string to that position...
public class DotSubstring {
public static void main(String[] args) {
String s = "1.2.3.4";
int secondDotPosition = findSecondDotPosition(s);
if (secondDotPosition > 0) {
System.out.println(s.substring(0, secondDotPosition));
} else {
System.out.printf("ERROR: there is not a 2nd dot in '%s'%n", s);
}
}
private static int findSecondDotPosition(String s) {
int result = -1;
int dotsToFind = 2;
char[] ca = s.toCharArray();
for (int i = 0; i < ca.length; ++i) {
if (ca[i] == '.') --dotsToFind;
if (dotsToFind == 0) return i;
}
return result;
}
}
The problem with split for beginner is, that is accepts regexp, that's why it is escaped in Joop Eggen's answe like this str.split("\\.").
And yes, that can be achieved in one line as user3458271 wrote in a comment same as xyz later in answer, just error checking would be more difficult (for example if there are no 2 dots...).
In one line with substring and indexOf:
String output = str.substring(0,str.indexOf(".",str.indexOf(".")+1));
public static void main(String[] args) {
String input = "2.111.1.190";
String[] out = input.split("\\.");
String output1 = out[0]+"."+out[1];
System.out.println(output1);
String output2 = "";
for(int x=2; x < out.length; x++)
output2 += out[x] +".";
System.out.println(output2);
}
For the other fields too:
String[] halfs = str.split("\\.");
String[] fulls = new String[halfs.length / 2];
for (int i = 0; i < fulls.length; ++i) {
fulls[i] = halfs[2*i] + "." + halfs[2*i + 1];
}
return fulls[0];
The same technique reduced for the first field:
String[] halfs = str.split("\\.", 3);
return halfs[0] + "." + halfs[1];
Simply:
return str.replaceAll("^([^.]*\\.[^.]*)\\..*$", "$1");

Removing characters from string efficiently

This may sound like a very simple question but how do you remove multiple different characters from a string without having to write a line for each, which is what I have laboriously done. I have written a string example below:
String word = "Hello, t-his is; an- (example) line."
word = word.replace(",", "");
word = word.replace(".", "");
word = word.replace(";", "");
word = word.replace("-", "");
word = word.replace("(", "");
word = word.replace(")", "");
System.out.println(word);
Which would produce "Hello this is an example line". A more efficient way is?
Use
word = word.replaceAll("[,.;\\-()]", "");
Note that special character - (hyphen) should be escaped by double backslashes, because otherwise it is considered to construct a range.
Although no more efficient than the original replace technique you could use
word = word.replaceAll("\\p{Punct}+", "");
to use a simple expression using replaceAll with a wider range of characters replaced
Without (ab)using regex, I would do that way:
String word = "Hello, t-his is; an- (example) line.";
String undesirable = ",.;-()";
int len1 = undesirable.length();
int len2 = word.length();
StringBuilder sb = new StringBuilder(len2);
outer: for (int j = 0; j < len2; j++) {
char c = word.charAt(j);
for (int i = 0; i < len; i++) {
if (c == undesirable.charAt(i)) continue outer;
}
sb.append(c);
}
System.out.println(sb.toString());
The advantage is performance. You don't need the overhead of creating and parsing a regular expression.
You could encapsulate that in a method:
public static String removeCharacters(String word, String undesirable) {
int len1 = undesirable.length();
int len2 = word.length();
StringBuilder sb = new StringBuilder(len2);
outer: for (int j = 0; j < len2; j++) {
char c = word.charAt(j);
for (int i = 0; i < len1; i++) {
if (c == undesirable.charAt(i)) continue outer;
}
sb.append(c);
}
return sb.toString();
}
public static String removeSpecialCharacters(String word) {
return removeCharacters(word, ",.;-()");
}
And then, you would use it this way:
public static void testMethod() {
String word = "Hello, t-his is; an- (example) line.";
System.out.println(removeSpecialCharacters(word));
}
Here is a performance test:
public class WordTest {
public static void main(String[] args) {
int iterations = 10000000;
long t1 = System.currentTimeMillis();
for (int i = 0; i < iterations; i++) {
testAsArray();
}
long t2 = System.currentTimeMillis();
for (int i = 0; i < iterations; i++) {
testRegex();
}
long t3 = System.currentTimeMillis();
for (int i = 0; i < iterations; i++) {
testAsString();
}
long t4 = System.currentTimeMillis();
System.out.println("Without regex, but using copied arrays: " + (t2 - t1));
System.out.println("With precompiled regex: " + (t3 - t2));
System.out.println("Without regex, but using string: " + (t4 - t3));
}
public static void testAsArray() {
String word = "Hello, t-his is; an- (example) line.";
char[] undesirable = ",.;-()".toCharArray();
StringBuilder sb = new StringBuilder(word.length());
outer: for (char c : word.toCharArray()) {
for (char h : undesirable) {
if (c == h) continue outer;
}
sb.append(c);
}
sb.toString();
}
public static void testAsString() {
String word = "Hello, t-his is; an- (example) line.";
String undesirable = ",.;-()";
int len1 = undesirable.length();
int len2 = word.length();
StringBuilder sb = new StringBuilder(len2);
outer: for (int j = 0; j < len2; j++) {
char c = word.charAt(j);
for (int i = 0; i < len1; i++) {
if (c == undesirable.charAt(i)) continue outer;
}
sb.append(c);
}
sb.toString();
}
private static final Pattern regex = Pattern.compile("[,\\.;\\-\\(\\)]");
public static void testRegex() {
String word = "Hello, t-his is; an- (example) line.";
String result = regex.matcher(word).replaceAll("");
}
}
The output on my machine:
Without regex, but using copied arrays: 5880
With precompiled regex: 11011
Without regex, but using string: 3844
Here is a solution to do this with minimal effort; the toRemove string contains all character you don't want to see in the output:
public static String removeChars(final String input, final String toRemove)
{
final StringBuilder sb = new StringBuilder(input.length());
final CharBuffer buf = CharBuffer.wrap(input);
char c;
while (buf.hasRemaining()) {
c = buf.get();
if (toRemove.indexOf(c) == -1)
sb.append(c);
}
return sb.toString();
}
If you use Java 8 you can even use this (unfortunately there's no CharStream so the casts are necessary...):
public static String removeChars(final String input, final String toRemove)
{
final StringBuilder sb = new StringBuilder(input.length());
input.chars().filter(c -> toRemove.indexOf((char) c) == -1)
.forEach(i -> sb.append((char) i));
return sb.toString();
}
You could try using a regular expression with Java's String.replaceAll method:
word = word.replaceAll(",|\.|;|-|\(|\)", "");
If you're not familiar with regular expressions, | means "or". So we are essentially saying , or . or ; or - or ( or ).
See more: Java documentation for String.replaceAll
Edit:
As mentioned, my previous version will not compile. Just for the sake of correctness (even though it has been pointed out that this is not the optimal solution), here is the corrected version of my regex:
word = word.replaceAll(",|\\.|;|-|\\(|\\)", "");

Can anybody help me to correct the following code?

Please help me to identify my mistakes in this code. I am new to Java. Excuse me if I have done any mistake. This is one of codingbat java questions. I am getting Timed Out error message for some inputs like "xxxyakyyyakzzz". For some inputs like "yakpak" and "pakyak" this code is working fine.
Question:
Suppose the string "yak" is unlucky. Given a string, return a version where all the "yak" are removed, but the "a" can be any char. The "yak" strings will not overlap.
public String stringYak(String str) {
String result = "";
int yakIndex = str.indexOf("yak");
if (yakIndex == -1)
return str; //there is no yak
//there is at least one yak
//if there are yaks store their indexes in the arraylist
ArrayList<Integer> yakArray = new ArrayList<Integer>();
int length = str.length();
yakIndex = 0;
while (yakIndex < length - 3) {
yakIndex = str.indexOf("yak", yakIndex);
yakArray.add(yakIndex);
yakIndex += 3;
}//all the yak indexes are stored in the arraylist
//iterate through the arraylist. skip the yaks and get non-yak substrings
for(int i = 0; i < length; i++) {
if (yakArray.contains(i))
i = i + 2;
else
result = result + str.charAt(i);
}
return result;
}
Shouldn't you be looking for any three character sequence starting with a 'y' and ending with a 'k'? Like so?
public static String stringYak(String str) {
char[] chars = (str != null) ? str.toCharArray()
: new char[] {};
StringBuilder sb = new StringBuilder();
for (int i = 0; i < chars.length; i++) {
if (chars[i] == 'y' && chars[i + 2] == 'k') { // if we have 'y' and two away is 'k'
// then it's unlucky...
i += 2;
continue; //skip the statement sb.append
} //do not append any pattern like y1k or yak etc
sb.append(chars[i]);
}
return sb.toString();
}
public static void main(String[] args) {
System.out.println(stringYak("1yik2yak3yuk4")); // Remove the "unlucky" strings
// The result will be 1234.
}
It looks like your programming assignment. You need to use regular expressions.
Look at http://www.vogella.com/articles/JavaRegularExpressions/article.html#regex for more information.
Remember, that you can not use contains. Your code maybe something like
result = str.removeall("y\wk")
you can try this
public static String stringYak(String str) {
for (int i = 0; i < str.length(); i++) {
if(str.charAt(i)=='y'){
str=str.replace("yak", "");
}
}
return str;
}

Compression algorithm in java

My goal is to write a program that compresses a string, for example:
input: hellooopppppp!
output:he2l3o6p!
Here is the code I have so far, but there are errors.
When I have the input: hellooo
my code outputs: hel2l3o
instead of: he213o
the 2 is being printed in the wrong spot, but I cannot figure out how to fix this.
Also, with an input of: hello
my code outputs: hel2l
instead of: he2lo
It skips the last letter in this case all together, and the 2 is also in the wrong place, an error from my first example.
Any help is much appreciated. Thanks so much!
public class compressionTime
{
public static void main(String [] args)
{
System.out.println ("Enter a string");
//read in user input
String userString = IO.readString();
//store length of string
int length = userString.length();
System.out.println(length);
int count;
String result = "";
for (int i=1; i<=length; i++)
{
char a = userString.charAt(i-1);
count = 1;
if (i-2 >= 0)
{
while (i<=length && userString.charAt(i-1) == userString.charAt(i-2))
{
count++;
i++;
}
System.out.print(count);
}
if (count==1)
result = result.concat(Character.toString(a));
else
result = result.concat(Integer.toString(count).concat(Character.toString(a)));
}
IO.outputStringAnswer(result);
}
}
I would
count from 0 as that is how indexes work in Java. Your code will be simpler.
would compare the current char to the next one. This will avoid printing the first character.
wouldn't compress ll as 2l as it is no smaller. Only sequences of at least 3 will help.
try to detect if a number 3 to 9 has been used and at least print an error.
use the debugger to step through the code to understand what it is doing and why it doesn't do what you think it should.
I am doing it this way. Very simple:
public static void compressString (String string) {
StringBuffer stringBuffer = new StringBuffer();
for (int i = 0; i < string.length(); i++) {
int count = 1;
while (i + 1 < string.length()
&& string.charAt(i) == string.charAt(i + 1)) {
count++;
i++;
}
if (count > 1) {
stringBuffer.append(count);
}
stringBuffer.append(string.charAt(i));
}
System.out.println("Compressed string: " + stringBuffer);
}
You can accomplish this using a nested for loops and do something simial to:
count = 0;
String results = "";
for(int i=0;i<userString.length();){
char begin = userString.charAt(i);
//System.out.println("begin is: "+begin);
for(int j=i+1; j<userString.length();j++){
char next = userString.charAt(j);
//System.out.println("next is: "+next);
if(begin == next){
count++;
}
else{
System.out.println("Breaking");
break;
}
}
i+= count+1;
if(count>0){
String add = begin + "";
int tempcount = count +1;
results+= tempcount + add;
}
else{
results+= begin;
}
count=0;
}
System.out.println(results);
I tested this output with Hello and the result was He2lo
also tested with hellooopppppp result he2l3o6p
If you don't understand how this works, you should learn regular expressions.
public String rleEncodeString(String in) {
StringBuilder out = new StringBuilder();
Pattern p = Pattern.compile("((\\w)\\2*)");
Matcher m = p.matcher(in);
while(m.find()) {
if(m.group(1).length() > 1) {
out.append(m.group(1).length());
}
out.append(m.group(2));
}
return out.toString();
}
Try something like this:
public static void main(String[] args) {
System.out.println("Enter a string:");
Scanner IO = new Scanner(System.in);
// read in user input
String userString = IO.nextLine() + "-";
int length = userString.length();
int count = 0;
String result = "";
char new_char;
for (int i = 0; i < length; i++) {
new_char = userString.charAt(i);
count++;
if (new_char != userString.charAt(i + 1)) {
if (count != 1) {
result = result.concat(Integer.toString(count + 1));
}
result = result.concat(Character.toString(new_char));
count = 0;
}
if (userString.charAt(i + 1) == '-')
break;
}
System.out.println(result);
}
The problem is that your code checks if the previous letter, not the next, is the same as the current.
Your for loops basically goes through each letter in the string, and if it is the same as the previous letter, it figures out how many of that letter there is and puts that number into the result string. However, for a word like "hello", it will check 'e' and 'l' (and notice that they are preceded by 'h' and 'e', receptively) and think that there is no repeat. It will then get to the next 'l', and then see that it is the same as the previous letter. It will put '2' in the result, but too late, resulting in "hel2l" instead of "he2lo".
To clean up and fix your code, I recommend the following to replace your for loop:
int count = 1;
String result = "";
for(int i=0;i<length;i++) {
if(i < userString.length()-1 && userString.charAt(i) == userString.charAt(i+1))
count++;
else {
if(count == 1)
result += userString.charAt(i);
else {
result = result + count + userString.charAt(i);
count = 1;
}
}
}
Comment if you need me to explain some of the changes. Some are necessary, others optional.
Here is the solution for the problem with better time complexity:
public static void compressString (String string) {
LinkedHashSet<String> charMap = new LinkedHashSet<String>();
HashMap<String, Integer> countMap = new HashMap<String, Integer>();
int count;
String key;
for (int i = 0; i < string.length(); i++) {
key = new String(string.charAt(i) + "");
charMap.add(key);
if(countMap.containsKey(key)) {
count = countMap.get(key);
countMap.put(key, count + 1);
}
else {
countMap.put(key, 1);
}
}
Iterator<String> iterator = charMap.iterator();
String resultStr = "";
while (iterator.hasNext()) {
key = iterator.next();
count = countMap.get(key);
if(count > 1) {
resultStr = resultStr + count + key;
}
else{
resultStr = resultStr + key;
}
}
System.out.println(resultStr);
}

Categories