我想计算文本字段中整数的值,同时忽略整数之前所需的文本字符串?
I want to calculate the value of an integer in a text field while ignoring a string of text that is required before the integer?
我正在制作自己的助手程序,该程序连接到我编写的银行管理程序。我希望用户能够在命令字段中键入:添加 5 美元或添加 10500 美元或任何其他金额。我将如何在忽略 "add $" 的同时计算“10500”。 "add $" 用于检查用户正在键入什么命令来执行操作。这是我目前所拥有的。
} else if (AssistantFrame.getCommand().equalsIgnoreCase("add ")) {
BankBalance.addToBalance(5);
}
这是处理将钱添加到余额中的代码。
public static void addToBalance(int balanceAdd){
Commands.setContinuity(0);
if(fileBalance.exists()) {
try {
loadBalance();
} catch (FileNotFoundException | UnsupportedEncodingException e) {
}
balance += balanceAdd;
try {
saveBalance();
} catch (FileNotFoundException | UnsupportedEncodingException e) {
}
AssistantFrame.updateAssistant("Your balance has been succesfully updated.\nYour new balance is - $" + balance);
} else {
AssistantFrame.updateAssistant("Sorry, but you don't seem to have a personal\nbank balance created yet.");
}
类似的东西:
String command = AssistantFrame.getCommand();
int amount = Integer.parseInt(command.replaceAll("[^\d]",""));
BankBalance.addToBalance(amount);
我要做的是遍历所有字符,检查它们的值是否在 48 到 57 之间(含),如果是,则将它们添加到字符串中。您将拥有一个仅包含数字的字符串。然后您可以使用 'Integer.parseInt(...)'
解析字符串
您可以在解析之前使用String.startsWith(String)
and then use String.substring(int)
。喜欢,
String command = "add ";
if (command.startsWith("add $")) {
int v = Integer.parseInt(command.substring(5));
System.out.println(v);
}
输出
10
您要实现的是解析命令。如果您的命令足够简单,您可以简单地使用以下示例:
if(command.startsWith("add $")){
money=command.substring(5);
todo=ADD;
else if(command.startsWith("something else")){
...
todo=SOMETHING_ELSE;
}
...
或者,如果您的命令本质上会更复杂,则通过词法分析器代码。
...lexical analysis, lexing or tokenization is the process of converting a sequence of characters (such as in a computer program or web page) into a sequence of tokens (strings with an assigned and thus identified meaning)... Wikipedia
其中一个例子是:here
我正在制作自己的助手程序,该程序连接到我编写的银行管理程序。我希望用户能够在命令字段中键入:添加 5 美元或添加 10500 美元或任何其他金额。我将如何在忽略 "add $" 的同时计算“10500”。 "add $" 用于检查用户正在键入什么命令来执行操作。这是我目前所拥有的。
} else if (AssistantFrame.getCommand().equalsIgnoreCase("add ")) {
BankBalance.addToBalance(5);
}
这是处理将钱添加到余额中的代码。
public static void addToBalance(int balanceAdd){
Commands.setContinuity(0);
if(fileBalance.exists()) {
try {
loadBalance();
} catch (FileNotFoundException | UnsupportedEncodingException e) {
}
balance += balanceAdd;
try {
saveBalance();
} catch (FileNotFoundException | UnsupportedEncodingException e) {
}
AssistantFrame.updateAssistant("Your balance has been succesfully updated.\nYour new balance is - $" + balance);
} else {
AssistantFrame.updateAssistant("Sorry, but you don't seem to have a personal\nbank balance created yet.");
}
类似的东西:
String command = AssistantFrame.getCommand();
int amount = Integer.parseInt(command.replaceAll("[^\d]",""));
BankBalance.addToBalance(amount);
我要做的是遍历所有字符,检查它们的值是否在 48 到 57 之间(含),如果是,则将它们添加到字符串中。您将拥有一个仅包含数字的字符串。然后您可以使用 'Integer.parseInt(...)'
解析字符串您可以在解析之前使用String.startsWith(String)
and then use String.substring(int)
。喜欢,
String command = "add ";
if (command.startsWith("add $")) {
int v = Integer.parseInt(command.substring(5));
System.out.println(v);
}
输出
10
您要实现的是解析命令。如果您的命令足够简单,您可以简单地使用以下示例:
if(command.startsWith("add $")){
money=command.substring(5);
todo=ADD;
else if(command.startsWith("something else")){
...
todo=SOMETHING_ELSE;
}
...
或者,如果您的命令本质上会更复杂,则通过词法分析器代码。
...lexical analysis, lexing or tokenization is the process of converting a sequence of characters (such as in a computer program or web page) into a sequence of tokens (strings with an assigned and thus identified meaning)... Wikipedia
其中一个例子是:here