I am making my own assistant program that connects to a bank management program i wrote. I want the user to be able to type into the command field: add $5 or add $10500 or any other amount. How would i calculate the "10500" while ignoring the "add $". The "add $" is used to check what command the user is typing to perform the action. This is what i have so far.
我正在制作自己的助理程序,该程序连接到我写的银行管理程序。我希望用户能够输入命令字段:添加$ 5或添加$ 10500或任何其他金额。如何在忽略“添加$”的同时计算“10500”。 “add $”用于检查用户正在键入的命令以执行操作。这就是我到目前为止所拥有的。
} else if (AssistantFrame.getCommand().equalsIgnoreCase("add $5")) {
BankBalance.addToBalance(5);
}
This is the code that handles adding the money to the balance.
这是处理将余额添加到余额中的代码。
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.");
}
4 个解决方案
#1
2
Something like that:
像这样的东西:
String command = AssistantFrame.getCommand();
int amount = Integer.parseInt(command.replaceAll("[^\\d]",""));
BankBalance.addToBalance(amount);
#2
0
What I would do is iterate through all the chars, check to see if their value is between 48 and 57, inclusive, and if they are, then add them to a String. You will have a String that contains digits only. You can then just parse the String using 'Integer.parseInt(...)'
我要做的是遍历所有字符,检查它们的值是否在48到57之间,包括它们,如果是,则将它们添加到字符串中。您将拥有一个仅包含数字的String。然后,您可以使用'Integer.parseInt(...)'解析字符串
#3
0
You could use String.startsWith(String)
and then use String.substring(int)
before you parse. Like,
您可以使用String.startsWith(String),然后在解析之前使用String.substring(int)。喜欢,
String command = "add $10";
if (command.startsWith("add $")) {
int v = Integer.parseInt(command.substring(5));
System.out.println(v);
}
Which outputs
10
#4
0
What you are trying to achieve is parsing the command. You can simply the following example if your command will be simple enough:
您要实现的是解析命令。如果您的命令足够简单,您可以简单地使用以下示例:
if(command.startsWith("add $")){
money=command.substring(5);
todo=ADD;
else if(command.startsWith("something else")){
...
todo=SOMETHING_ELSE;
}
...
Or if your commands will be more complex in nature, then go through Lexical Analyser codes.
或者如果您的命令本质上更复杂,那么请查看Lexical Analyzer代码。
...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
...词法分析,lexing或标记化是将一系列字符(例如在计算机程序或网页中)转换为一系列标记(具有指定且因此标识的含义的字符串)的过程...*
One of such example is: here
其中一个例子是:这里
#1
2
Something like that:
像这样的东西:
String command = AssistantFrame.getCommand();
int amount = Integer.parseInt(command.replaceAll("[^\\d]",""));
BankBalance.addToBalance(amount);
#2
0
What I would do is iterate through all the chars, check to see if their value is between 48 and 57, inclusive, and if they are, then add them to a String. You will have a String that contains digits only. You can then just parse the String using 'Integer.parseInt(...)'
我要做的是遍历所有字符,检查它们的值是否在48到57之间,包括它们,如果是,则将它们添加到字符串中。您将拥有一个仅包含数字的String。然后,您可以使用'Integer.parseInt(...)'解析字符串
#3
0
You could use String.startsWith(String)
and then use String.substring(int)
before you parse. Like,
您可以使用String.startsWith(String),然后在解析之前使用String.substring(int)。喜欢,
String command = "add $10";
if (command.startsWith("add $")) {
int v = Integer.parseInt(command.substring(5));
System.out.println(v);
}
Which outputs
10
#4
0
What you are trying to achieve is parsing the command. You can simply the following example if your command will be simple enough:
您要实现的是解析命令。如果您的命令足够简单,您可以简单地使用以下示例:
if(command.startsWith("add $")){
money=command.substring(5);
todo=ADD;
else if(command.startsWith("something else")){
...
todo=SOMETHING_ELSE;
}
...
Or if your commands will be more complex in nature, then go through Lexical Analyser codes.
或者如果您的命令本质上更复杂,那么请查看Lexical Analyzer代码。
...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
...词法分析,lexing或标记化是将一系列字符(例如在计算机程序或网页中)转换为一系列标记(具有指定且因此标识的含义的字符串)的过程...*
One of such example is: here
其中一个例子是:这里