我正在开发一个个人项目,我想接受如下所示的userinput:
1.0+2.5+3--4
并将其格式化为:
1.0 + 2.5 + 3 - -4
到目前为止,我使用.replace(“+”)到.replace(“+”)并为所有操作数执行此操作,但问题是它使用户输入到此:
1.0 + 2.5 + 3 - - 4
有没有办法可以用负号来表现。我想这样做,所以我可以将数字解析成双打,然后加上和减去它们。
我的代码:
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class StringMan {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
String check = "-a1 +a2 + a3 +-a5";
check = check.replace("--", "+");
System.out.println(check);
Pattern pattern = Pattern.compile("\\s+");
Matcher matcher = pattern.matcher(check);
boolean expr = matcher.find();
String str = matcher.replaceAll(" ");
System.out.println(str);
}
}
输出是:
-a1 +a2 - a3 +-a5
-a1 +a2 - a3 +-a5
问题是我希望输出看起来像这样: -a1 + a2 - a3 + -a5
答案 0 :(得分:1)
在这种特定情况下,您只需将其替换为--
即可处理+
:
--
替换为+
答案 1 :(得分:0)
首先用+替换 - 这在数学上是等价的。或者首先替换 - with - - ,这将保持 - 和4在一起。
答案 2 :(得分:0)
我建议使用正则表达式及其“组”功能。我实际上会删除所有空格以使事情变得更容易,将其从等式中删除,少一点处理。显然我建议简化字符串,将“ - ”替换为“+”,将“* +”替换为“*”,依此类推。
现在你可以在清理过的字符串上使用正则表达式。
Pattern firstPat = Pattern.compile("(((\\+|-)?)\\d+(.\\d+)?)");//for matching the first number, leading sign is optional
Pattern remainingPat = Pattern.compile("(\\+|-)(\\d+(.\\d+)?)");//for remaining numbers, leading sign is mandatory.
Pattern remainingPatWithExtOps = Pattern.compile("(\\*|/|\\+|-)(-?\\d+(.\\d+)?)");//for remaining numbers, accommodating multiply and divide with negative signs(positive signs should have been cleaned out)
Matcher match = firstPat.matcher(inputString);
现在您可以使用match.find()
方法遍历字符串。然后使用match.group(1)
获取签名/操作,并使用match.group(2)
获取该号码...
因此...
Double firstnum;
boolean firstNumSigned = false;
if(match.find())
{
firstNum = Double.parse(match.group(0));// Parsing handles possible sign in string.
//obv check for exceptions during this and double check group num
String tmp = match.group(1);
firstNumSigned = tmp.equals("+") || tmp.equals("-");
}
else
{//no match means the input was probably invalid....
throw new IllegalArgumentException("What the heck were you thinking inputting that?!");
}
match = remainingPat.matcher(inputString);//use our other pattern for remaining numbers
if(firstNumSigned)
{
match.find();//a signed first number will cause success here, we need to ignore this since we already got the first number
}
Double tmpRemaingingNum;
String operation;
while(match.find())
{
operation = match.group(1);
tmpRemainingNum = Double.parse(match.group(2));
//Do what you want with these values now until match.find() returns false and you are done
}
PS:代码没有经过测试,我对正则表达式相当自信,但我不是100%肯定第一个模式上的分组括号..可能需要实验
答案 3 :(得分:0)
检查一下, 读取运算符之间的字符串和整数,如'*, - , - ,+“ 我们可以读取整数和字符。
public static void main(String[] args) {
// TODO Auto-generated method stub
final Pattern remainingPatWithExt=Pattern.compile("(\\p{L}\\p{M}*)[\\p{L}\\p{M}0-9^\\-.-?_+-=<>!;]*");
String check = "a1+a2+--a7+ a3 +-a5";
Matcher matcher = remainingPatWithExt.matcher(check);
while( matcher.find())
{
System.out.println(matcher.group());
//use matcher.group(0) or matcher.group(1)
}
}
A1 a2 A7 A3 A5