我想检查字符串是否为正数,但我不想使用,Integer.parseInt()因为用户可能输入的数字大于int.相反,如果数字字符串包含所有"0"字符,我宁愿使用正则表达式返回false.
if(val.matches("[0-9]+")){
// We know that it will be a number, but what if it is "000"?
// what should I change to make sure
// "At Least 1 character in the String is from 1-9"
}
Run Code Online (Sandbox Code Playgroud)
注意:字符串必须只包含0- 9并且它不能包含所有0s; 换句话说,它必须至少有1个字符[1-9].
我有一个实用程序方法,它读取xml文件并转换为字符串,如下所示:
public static String readFile(String xmlFileName) throws IOException, DocumentException{
String xmlMsg = null;
Resource resource = null;
InputStream inputStream = null;
try{
resource = new ClassPathResource(xmlFileName);
inputStream = resource.getInputStream();
SAXReader reader = new SAXReader();
Document doc = reader.read( inputStream );
xmlMsg = doc.asXML();
}finally{
if(inputStream != null){
inputStream.close();
}
}
return xmlMsg;
}
Run Code Online (Sandbox Code Playgroud)
如果我在上面的代码中捕获DocumentException并重新抛出它,这是一个坏主意:
public static String readFile(String xmlFileName) throws IOException, DocumentException{
String xmlMsg = null;
Resource resource = null;
InputStream inputStream = null;
try{
resource = new ClassPathResource(xmlFileName);
inputStream …Run Code Online (Sandbox Code Playgroud) 我正在编写一个 PHP 文件,但我不知道当链接位于同一文件但具有不同 ID 时创建链接的最佳方式是什么。我的意思是我有一个名为的文件test.php,它包含类似以下内容的内容:
if ($id == "") {
echo "<a href='".$_SERVER['PHP_SELF']."?id=test'>Try the test page</a>";
} else if ($id == "test") {
echo "Here is the testing content with various information!";
}
Run Code Online (Sandbox Code Playgroud)
我想知道更安全的代码哪个更好:
$_SERVER['PHP_SELF']?id=test或test.php?id=test。
我知道它现在具有相同的目的,但如果我更改文件名$_SERVER['PHP_SELF']?id=test似乎更好,因为它仍然指向同一个文件。
但我想确切地知道哪个更安全。
我有这个Java 6代码,我想将其转换为Java 8.
BigDecimal sumContributionPercentage = new BigDecimal(0);
if (hasElements(mandateDetails)) {
for (InstrumentMandateDetail mandate: mandateDetails) {
if(!(VERFI_STATUS_CLOSED.equals(mandate.getMandateVerificationStatusType().getCode()) ||
VERFI_STATUS_REJECTED.equals(mandate.getMandateVerificationStatusType().getCode()))) {
sumContributionPercentage = sumContributionPercentage.add(mandate.getContributionPercentage());
}
}
}
Run Code Online (Sandbox Code Playgroud) 在 Javadoc 中,如何链接到特定的枚举值而不是枚举类本身?
public enum SomeJavaCass {
SomeEnum;
}
Run Code Online (Sandbox Code Playgroud)
对于上面的枚举,有一种方法可以做到:
/**
* {@link SomeJavaClass.SomeEnum}
*/
Run Code Online (Sandbox Code Playgroud) Makefile 和 Bash 之间有什么关系?
Makefile 中的示例:
CC = gcc
Run Code Online (Sandbox Code Playgroud)
但是如果你在 shell 中输入这个,你会得到错误:
~# CC = gcc
-bash: CC: command not found
Run Code Online (Sandbox Code Playgroud)
可以理解,因为有空格。但它在 Makefile 中有效。
另外,在 Makefile 中使用命令替换$()
$(CC) $(LDFLAGS) -o $@ $(OBJS) $(LIBS)
Run Code Online (Sandbox Code Playgroud)
这应该是变量替换$CC或${CC}在 bash 中。
所以 Makefile 语法和 bash 语法是不同的,但它们看起来确实相关,例如$@.
如何在没有重新编程整个函数的情况下使函数与Python中编程的函数相反?例如:
swap1 = "abc"
swap2 = "def"
def swap():
global swap1, swap2
swap1 = swap2
Run Code Online (Sandbox Code Playgroud)
成
swap1 = "abc"
swap2 = "def"
def swap():
global swap1, swap2
swap2 = swap1
Run Code Online (Sandbox Code Playgroud)
任何帮助表示赞赏.
nb = 1 # nb = 1
while nb < 10: # while nb is inferior do this
print(nb," * 7 = ",nb * 7) # print: 1 * 7 = 7
nb1 += 1 # add 1 to nb
Run Code Online (Sandbox Code Playgroud)
我的问题是它没有添加1 nb而是无限循环!
我不是在寻找解决方案,只需要有人向我解释.因为我知道我可以使用这段代码:
nb = 7
i = 0
while i < 10:
print(i + 1, "*", nb, "=", (i + 1) * nb)
i += 1
Run Code Online (Sandbox Code Playgroud)