Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/354.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 将输入货币拆分为字符串和整数_Java_String_Split_Currency - Fatal编程技术网

Java 将输入货币拆分为字符串和整数

Java 将输入货币拆分为字符串和整数,java,string,split,currency,Java,String,Split,Currency,例如,在以2英镑或10便士的形式输入货币的程序中,是否有一种方法可以将其拆分为两个变量 货币类型=£ currencyValue=2 或 currencyType=p currencyValue=10 如果currencyType是一个字符串,currencyValue是一个int?您可以将您的值作为字符串输入,使用split函数将其正常拆分,并将每个值分配给自己的字符串。然后将字符串转换为整数 int currencyValue=Integer.parseInt(数组[0]); 字符串curr

例如,在以2英镑或10便士的形式输入货币的程序中,是否有一种方法可以将其拆分为两个变量 货币类型=£ currencyValue=2 或 currencyType=p currencyValue=10


如果currencyType是一个字符串,currencyValue是一个int?

您可以将您的值作为字符串输入,使用split函数将其正常拆分,并将每个值分配给自己的字符串。然后将字符串转换为整数

int currencyValue=Integer.parseInt(数组[0]); 字符串currencyType=数组[1]

数组[]是将字符串拆分为的数组

String input = user_input.nextLine();
char[] array = input.toCharArray();
for(int i = 0; i < input.length(); i++) {
    if (Character.isLetter(array[i])){
        //use .split based on the output of the if statement
    }
}
String input=user_input.nextLine();
char[]数组=input.toCharArray();
对于(int i=0;i
使用patten和matcher类,如下所示<代码>\d+匹配一个或多个数字,
\d+
匹配一个或多个非数字字符

String s1 = "£2";
Matcher m = Pattern.compile("(\\D+)|(\\d+)").matcher(s1);
while(m.find())
{
if (m.group(1) != null)
System.out.println("Currency Type: " + m.group(1));
if (m.group(2) != null)
System.out.println("Currency Value: " + m.group(2));
}
输出:

Currency Type: £
Currency Value: 2

如果还想处理十进制值,请使用此正则表达式

Pattern.compile("(\\D+)|(\\d+(?:\\.\\d+)?)");

您可以使用此正则表达式获得结果:
“(.*?)([\\d,]*)(.*?”
这将把输入分为三组: 1) 主要货币代币 2) 值标记(可以包含“,”,在字符串版本中,您可以将“,”替换为“”,然后转换为整数) 3) 尾随货币代币


通过查看正则表达式中的组,可以确定是否存在前导或尾随货币,然后从第二个组中获取值。您可以通过查找java正则表达式的用法自己编写代码。

这是一个没有正则表达式的解决方案,尽管我更喜欢其中一种:

String entry = "€2.73";
StringBuilder currency = new StringBuilder();
StringBuilder value = new StringBuilder();
for (char c : entry.toCharArray()) {
    if (Character.isDigit(c) || c == '.' || c == ',') {
        value.append(c);
    } else {
        currency.append(c);
    }
}
System.out.println("Value = " + value + " Currency = " + currency);

您尝试了什么?这对他没有帮助,因为货币类型可以是前缀或后缀。所以您不能保证第一个元素是value,第二个值是Type。N还有一件事,你能详细说明你将如何分割吗?我会使用类似的方法(我编辑了我的答案)我同意其他答案,即正则表达式是更好的方法。这不会正确处理尾随货币,请查看我的正则表达式以获得完整的解决方案。在您的中,它可能在组1中有值,在组2中键入值,这是不正确的。真的吗?你试过我的密码了吗?值必须在组2中,类型必须在组1中。啊,我现在看到了。我的错@4我们的剑