Java 有没有办法将字母形式的数字字符串数组转换成数字形式?
我对java还是新手,但我有一个关于我想测试的理论的问题。我想知道是否有一种方法可以编写一个程序,将数字的字符串数组转换成整数的数字形式,这些数字以1、2、3等字母形式写出。这真的可能吗?我没有任何示例代码,因为我还没有测试出来。但是,如果您能回答一些问题,我们将不胜感激。谢谢大家! 您可能希望创建一个映射,用于将字符串值查找为整数Java 有没有办法将字母形式的数字字符串数组转换成数字形式?,java,arrays,string,int,theory,Java,Arrays,String,Int,Theory,我对java还是新手,但我有一个关于我想测试的理论的问题。我想知道是否有一种方法可以编写一个程序,将数字的字符串数组转换成整数的数字形式,这些数字以1、2、3等字母形式写出。这真的可能吗?我没有任何示例代码,因为我还没有测试出来。但是,如果您能回答一些问题,我们将不胜感激。谢谢大家! 您可能希望创建一个映射,用于将字符串值查找为整数 Map<String, Integer> map = new HashMap<>(); map.put("one", 1); map.put
Map<String, Integer> map = new HashMap<>();
map.put("one", 1);
map.put("two", 2);
map.put("three", 3);
map.put("four", 4);
如果你有一个要转换的数组,那么你就必须循环它,显然你的解析会变得更复杂,因为可能有更大范围的字符串值,例如:“四千三百五”你可能想要创建一个映射来将字符串值查找成整数
Map<String, Integer> map = new HashMap<>();
map.put("one", 1);
map.put("two", 2);
map.put("three", 3);
map.put("four", 4);
如果你有一个要转换的数组,那么你就必须循环它,很明显你的解析会变得更复杂,因为可能有更大范围的字符串值,例如:“四千三百五”一般来说,我用“配置重于实现”模式来解决这类问题。 我可以为你的问题提供一个解决方案吗 Java使我们能够在属性文件中存储大量的键/值对,并根据需要随时请求映射。 因此我将存储此文件:string2int.properties:
zero:0
one:1
two:2
three:3
four:4
five:5
six:6
seven:7
eight:8
nine:9
这是.properties文件的格式
然后,由于属性对象,我可以通过这种方式访问键的值:
Properties properties = new Properties();
try {
FileInputStream input = new FileInputStream("string2int.properties");
properties.load(input);
System.out.println("one is : " + properties.getProperty("one"));
}
catch(Exception e) {
e.printStackTrace();
}
输出:
one is : 1
通常,我通过“配置重于实现”模式来解决这类问题。 我可以为你的问题提供一个解决方案吗 Java使我们能够在属性文件中存储大量的键/值对,并根据需要随时请求映射。 因此我将存储此文件:string2int.properties:
zero:0
one:1
two:2
three:3
four:4
five:5
six:6
seven:7
eight:8
nine:9
这是.properties文件的格式
然后,由于属性对象,我可以通过这种方式访问键的值:
Properties properties = new Properties();
try {
FileInputStream input = new FileInputStream("string2int.properties");
properties.load(input);
System.out.println("one is : " + properties.getProperty("one"));
}
catch(Exception e) {
e.printStackTrace();
}
输出:
one is : 1
你需要10,elleven,12,13,二十,三十。。九千万。。在您的属性文件中。然后,您必须解析写入的数字 “二十一万二千零三”必须是(2x100+12)x1000+3 根据上面的数字,你知道你必须停在100,然后乘以2。你也知道,当你得到“千”的时候,左边的东西必须乘以1000。除非左边还有一百万 我尝试了一个从右端开始的例子,最不重要的数字。我记下键号100和1000并进行递归计算。我没有包含属性文件,而是使用了映射。如果有一个单词的映射,它将被添加到列表中,除非它是“and”。如果一个字没有映射,程序就会出现NullPointerException故障 请注意,此代码不应在任何生产环境中使用。它不是一个解析器,也不考虑语言的一些特殊情况 还要注意的是,没有检测到非法书写的数字,例如“一二九九九百零五四”。这将显示为3000+18000+9
public class TextToNumber{
public static void main(String[] args){
String s;
TextToNumber textToNumber = new TextToNumber();
s = "four";
System.out.println(s);
System.out.println(textToNumber.translateNumber(s));
System.out.println("----");
s = "one hundred";
System.out.println(s);
System.out.println(textToNumber.translateNumber(s));
System.out.println("----");
s = "five thousand";
System.out.println(s);
System.out.println(textToNumber.translateNumber(s));
System.out.println("----");
s = "twelve thousand two hundred fifty six";
System.out.println(s);
System.out.println(textToNumber.translateNumber(s));
System.out.println("----");
s = "two hundred and twelve thousand and three";
System.out.println(s);
System.out.println(textToNumber.translateNumber(s));
System.out.println("----");
s = "one two thousand ninety ninety hundred five four";
System.out.println(s);
System.out.println(textToNumber.translateNumber(s));
}
Map<String, Integer> map = new HashMap<>();
public TextToNumber() {
map.put("one", 1);
map.put("two", 2);
map.put("three", 3);
map.put("four", 4);
map.put("five", 5);
map.put("six", 6);
map.put("seven", 7);
map.put("eight", 8);
map.put("nine", 9);
map.put("ten", 10);
map.put("eleven", 11);
map.put("twelve", 12);
map.put("thirteen", 13);
map.put("fourteen", 14);
map.put("fifteen", 15);
map.put("sixteen", 16);
map.put("seventeen", 17);
map.put("eighteen", 18);
map.put("nineteen", 19);
map.put("twenty", 20);
map.put("thirty", 30);
map.put("fourty", 40);
map.put("fifty", 50);
map.put("sixty", 60);
map.put("seventy", 70);
map.put("eighty", 80);
map.put("ninety", 90);
map.put("hundred", 100);
map.put("thousand", 1000);
}
public int translateNumber(String text){
List<Integer> numbers = new ArrayList<>(10);
if (text == null) throw new IllegalArgumentException("Number cannot be empty");
// borrowed from https://docs.oracle.com/javase/tutorial/i18n/text/word.html
BreakIterator wordIterator = BreakIterator.getWordInstance();
wordIterator.setText(text);
int start = wordIterator.first();
int end = wordIterator.next();
while (end != wordIterator.DONE) {
String word = text.substring(start,end);
if (Character.isLetterOrDigit(word.charAt(0))) {
if (!("and".equals(word))) numbers.add(map.get(word));
}
start = end;
end = wordIterator.next();
}
System.out.println(numbers);
return recursiveCalculation(numbers);
}
private int recursiveCalculation(List<Integer> list){
int result = 0;
int index1000 = list.lastIndexOf(Integer.valueOf(1000));
int index100 = list.lastIndexOf(Integer.valueOf(100));
// add all numbers that's to the right of the hundred marker
// (or 1000 marker in case there's no hundred marker)
result += add(list, Math.max(index100+1,Math.max(index1000+1,0)), list.size());
if (index100 != -1 && index100 > index1000) {
result += 100 * recursiveCalculation(list.subList(index1000+1,index100));
}
if (index1000 != -1) {
result += 1000 * recursiveCalculation(list.subList(0,index1000));
}
return result;
}
private int add(List<Integer> list, int begin, int end) {
int sum = 0;
for (int i = begin; i < end; i++) {
sum += list.get(i);
}
return sum;
}
}
你需要10,elleven,12,13,二十,三十。。九千万。。在您的属性文件中。然后,您必须解析写入的数字 “二十一万二千零三”必须是(2x100+12)x1000+3 根据上面的数字,你知道你必须停在100,然后乘以2。你也知道,当你得到“千”的时候,左边的东西必须乘以1000。除非左边还有一百万 我尝试了一个从右端开始的例子,最不重要的数字。我记下键号100和1000并进行递归计算。我没有包含属性文件,而是使用了映射。如果有一个单词的映射,它将被添加到列表中,除非它是“and”。如果一个字没有映射,程序就会出现NullPointerException故障 请注意,此代码不应在任何生产环境中使用。它不是一个解析器,也不考虑语言的一些特殊情况 还要注意的是,没有检测到非法书写的数字,例如“一二九九九百零五四”。这将显示为3000+18000+9
public class TextToNumber{
public static void main(String[] args){
String s;
TextToNumber textToNumber = new TextToNumber();
s = "four";
System.out.println(s);
System.out.println(textToNumber.translateNumber(s));
System.out.println("----");
s = "one hundred";
System.out.println(s);
System.out.println(textToNumber.translateNumber(s));
System.out.println("----");
s = "five thousand";
System.out.println(s);
System.out.println(textToNumber.translateNumber(s));
System.out.println("----");
s = "twelve thousand two hundred fifty six";
System.out.println(s);
System.out.println(textToNumber.translateNumber(s));
System.out.println("----");
s = "two hundred and twelve thousand and three";
System.out.println(s);
System.out.println(textToNumber.translateNumber(s));
System.out.println("----");
s = "one two thousand ninety ninety hundred five four";
System.out.println(s);
System.out.println(textToNumber.translateNumber(s));
}
Map<String, Integer> map = new HashMap<>();
public TextToNumber() {
map.put("one", 1);
map.put("two", 2);
map.put("three", 3);
map.put("four", 4);
map.put("five", 5);
map.put("six", 6);
map.put("seven", 7);
map.put("eight", 8);
map.put("nine", 9);
map.put("ten", 10);
map.put("eleven", 11);
map.put("twelve", 12);
map.put("thirteen", 13);
map.put("fourteen", 14);
map.put("fifteen", 15);
map.put("sixteen", 16);
map.put("seventeen", 17);
map.put("eighteen", 18);
map.put("nineteen", 19);
map.put("twenty", 20);
map.put("thirty", 30);
map.put("fourty", 40);
map.put("fifty", 50);
map.put("sixty", 60);
map.put("seventy", 70);
map.put("eighty", 80);
map.put("ninety", 90);
map.put("hundred", 100);
map.put("thousand", 1000);
}
public int translateNumber(String text){
List<Integer> numbers = new ArrayList<>(10);
if (text == null) throw new IllegalArgumentException("Number cannot be empty");
// borrowed from https://docs.oracle.com/javase/tutorial/i18n/text/word.html
BreakIterator wordIterator = BreakIterator.getWordInstance();
wordIterator.setText(text);
int start = wordIterator.first();
int end = wordIterator.next();
while (end != wordIterator.DONE) {
String word = text.substring(start,end);
if (Character.isLetterOrDigit(word.charAt(0))) {
if (!("and".equals(word))) numbers.add(map.get(word));
}
start = end;
end = wordIterator.next();
}
System.out.println(numbers);
return recursiveCalculation(numbers);
}
private int recursiveCalculation(List<Integer> list){
int result = 0;
int index1000 = list.lastIndexOf(Integer.valueOf(1000));
int index100 = list.lastIndexOf(Integer.valueOf(100));
// add all numbers that's to the right of the hundred marker
// (or 1000 marker in case there's no hundred marker)
result += add(list, Math.max(index100+1,Math.max(index1000+1,0)), list.size());
if (index100 != -1 && index100 > index1000) {
result += 100 * recursiveCalculation(list.subList(index1000+1,index100));
}
if (index1000 != -1) {
result += 1000 * recursiveCalculation(list.subList(0,index1000));
}
return result;
}
private int add(List<Integer> list, int begin, int end) {
int sum = 0;
for (int i = begin; i < end; i++) {
sum += list.get(i);
}
return sum;
}
}
就目前而言很好,我也会这么做,但《九百七十五》不太适合这种方法:——)OP真的不清楚他说的是数字(所以975是一个数字)还是数字(所以975是三个数字)。就目前而言很好,我也会这么做,但是《九百七十五》不太适合这种方法:-)OP不太清楚他说的是数字(所以975是一个数字)还是数字(所以975是三个数字)。