what I am trying to do is encode numbers in a string into numbers acording to the alphabet an leave the numbers still. so "abc123" would be "123123". Found the solution in javascript but cannot seem to fit into java. any help would be great, thanks.
The java function would be something like
import java.util.*;
import java.io.*;
class Main {
public static String NumberEncoding(String str) {
***call javascript function or translate it into java
}
public static void main (String[] args) {
// keep this function call here
Scanner s = new Scanner(System.in);
System.out.print(NumberEncoding(s.nextLine()));
}
}
The jasvascript function is
function NumberEncoding(str) {
str = str.toLowerCase();
var obj = {};
var alpha = "abcdefghijklmnopqrstuvwxyz";
var result = "";
for (var i = 1; i <= alpha.length; i++) {
obj[alpha[i-1]] = i;
}
for (var j = 0; j < str.length; j++) {
if (str[j].match(/[a-z]/)) {
result += obj[str[j]];
} else {
result += str[j];
}
}
return result;
}