[英]How can I split a string as such…?
In my programming class my teacher asked us to make a calculator that works in a 'number function number' type format. 在我的编程课上,老师要求我们制造一个能以“数字功能数字”类型格式使用的计算器。 When writing a method to check if the string was valid I realized it stops checking the string after the space. 当编写一种方法来检查字符串是否有效时,我意识到它会在空格后停止检查字符串。 How can I continue to check it? 如何继续检查? The only Idea I had was to split it and then check it, but I don't know how to do that, so... 我唯一的想法是将其拆分然后检查,但是我不知道该怎么做,所以...
How do I split a string like this: 我如何分割这样的字符串:
String s = "2345 * 2341";
in the following way: 通过以下方式:
String String1 = 2345;
String String2 = 2341;
Then how can I check each string to make sure it is valid? 然后,如何检查每个字符串以确保其有效? So far I have: (Note: I am a beginner at programming) 到目前为止,我有:(注意:我是编程的初学者)
public boolean validNumber() throws IOException {
input = getUserInput();
boolean valid = false;
int i = 0;
for (valid = true; i < input.length();) {
if (input.charAt(i) == '0'
&& input.charAt(i) == '1'
&& input.charAt(i) == '2'
&& input.charAt(i) == '3'
&& input.charAt(i) == '4'
&& input.charAt(i) == '5'
&& input.charAt(i) == '6'
&& input.charAt(i) == '7'
&& input.charAt(i) == '8'
&& input.charAt(i) == '9') {
valid = true;
}else{
valid = false;
}
i++;
}
return valid;
}
public void check() throws IOException {
boolean valid = validNumber();
int i = 0;
while (valid = true && i < 1){
if (input.contains(" + ")) {
plus();
} else if (input.contains(" - ")) {
minus();
} else if (input.contains(" / ")) {
divide();
} else if (input.contains(" * ")) {
multiply();
}else {
System.out.println("Error Incorrect Input");
System.out.println("Reinput your numbers");
}
check();
}
}
}
You can use the split
method to split the strings. 您可以使用split
方法拆分字符串。 To check whether the splitted strings are valid, use Long.parseLong
method. 若要检查Long.parseLong
字符串是否有效,请使用Long.parseLong
方法。 If the string is not a valid number, it will throw a NumberFormatException
. 如果字符串不是有效数字,则将引发NumberFormatException
。
String s = "2345 * 23s41";
String[] strings = s.split("\\*"); //Split strings
string1 = strings[0].trim();
string2 = strings[1].trim();
You can check the validity by the following method: 您可以通过以下方法检查有效性:
public boolean isValid(String s)
{
try
{
Long.parseLong(s);
}
catch(NumberFormatException e)
{
return false;
}
return true;
}
You can split a string as follows, 您可以按如下方式拆分字符串,
String string = "1234*4321";
String[] strings = string.split("\\*");
String string1 = strings[0];
String string2 = strings[1];
If you want to remove the spaces you can do this aswell instead of trimming : 如果要删除空格,也可以执行以下操作,而不是修剪:
String s = "2345 * 2341";
String[] strings = s.split(" \\* ");
If you are guaranteed to always have a well formed string of the form <operand1> <operator> <operand2>
, you can seperate the different parts in the following manner: 如果保证始终具有格式良好的字符串,格式为<operand1> <operator> <operand2>
,则可以按以下方式分隔不同部分:
String expression = "2345 * 2341";
String[] expressionParts = s.split(" ");
System.out.println(expressionParts[0].equals("2345")); //prints True
System.out.println(expressionParts[1].equals("*")); //prints True
System.out.println(expressionParts[2].equals("2341")); //prints True
Below is a sample code: 下面是一个示例代码:
String String1 = null;
String String2 = null;
String strNumber = "1234*4321";
String delimeter = "\\*";
if(strNumber != null)
{
String[] parts = strNumber.split(delimeter);
if(parts.length == 2)
{
String1 = parts[0];
String2 = parts[1];
}
}
Hope it helps. 希望能帮助到你。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.