此代码将字符串分隔为标记并将它们存储在字符串数组中,然后将变量与第一个主页进行比较...为什么它不起作用?
public static void main(String...aArguments) throws IOException {
String usuario = "Jorman";
String password = "14988611";
String strDatos = "Jorman 14988611";
StringTokenizer tokens = new StringTokenizer(strDatos, " ");
int nDatos = tokens.countTokens();
String[] datos = new String[nDatos];
int i = 0;
while (tokens.hasMoreTokens()) {
String str = tokens.nextToken();
datos[i] = str;
i++;
}
//System.out.println (usuario);
if ((datos[0] == usuario)) {
System.out.println("WORKING");
}
}
Run Code Online (Sandbox Code Playgroud) 我有以下代码行来比较String.str1不等于str2,这是可以理解的,因为它比较了对象引用.但那么为什么s1等于s2?
String s1 = "abc";
String s2 = "abc";
String str1 = new String("abc");
String str2 = new String("abc");
if (s1==s2)
System.out.println("s1==s2");
else
System.out.println("s1!=s2");
if (str1==str2)
System.out.println("str1==str2");
else
System.out.println("str1!=str2");
if (s1==str1)
System.out.println("str1==s1");
else
System.out.println("str1!=s1");
Run Code Online (Sandbox Code Playgroud)
输出:
s1==s2
str1!=str2
str1!=s1
Run Code Online (Sandbox Code Playgroud) 为什么建议在Scala中使用==而不是.equals字符串比较?StackOverflow中有很多问题不建议在Java中使用引用等式进行字符串比较,但为什么建议在Scala中进行完全相反的操作呢?
所以我正在读取一个文件的输入,该文件说这些行:
NEO
You're the Oracle?
NEO
Yeah.
Run Code Online (Sandbox Code Playgroud)
所以我只想输出他的实际线,而不是它所说的NEO.所以我尝试了这个:
if(line.trim()=="NEO")
output=false;
if (output)
TextIO.putln(name + ":" + "\"" + line.trim() + "\""); // Only print the line if 'output' is true
Run Code Online (Sandbox Code Playgroud)
但那没有成功.它仍然打印NEO.我怎样才能做到这一点?
这是一个程序,当我键入"kutty"它应该说我"你好kutty"但这个代码不起作用.
import java.util.*;
public class Kutty
{
public static void main(String args[])
{
byte a[]={5,2,3};
String c;
Scanner scan=new Scanner(System.in);
c=scan.next();
if(c=="kutty")
{
System.out.println("Hello" +c);
}
else
{
System.out.println("Invalid");
}
}
}
Run Code Online (Sandbox Code Playgroud) 我有这样的条件:
try {
History history = requestHelper.getHistory();
if (!history.getInvoiceNo().equalsIgnoreCase("") || //String
!history.getCcEmail().equalsIgnoreCase("") || //String
!history.getHostAuthCode().equalsIgnoreCase("") || //String
!history.getMerchantCategory().equalsIgnoreCase(null) || //String
!history.getCcName().equalsIgnoreCase("") || //String
!history.getCcPhone().equalsIgnoreCase("") || //String
history.getBatchNo() != 0 || //int
!history.getIpAddress().equalsIgnoreCase("") || //String
!history.getTransactionStatus().equals(null)) { //char
System.out.println(": : : FAILED GET HISTORY, NO PARAMETER SET!");
requestHelper.getResultHelper().setLstHistoryTransaction(null);
else {
System.out.println(": : : SUCCESS GET HISTORY!");
/* some command */
}
} catch (Exception ex) {
System.out.println(": : : ERROR QUERYING HISTORY TO DATABASE");
ex.printStackTrace();
}
Run Code Online (Sandbox Code Playgroud)
但是,当我不填充该参数的任何变量时,为什么它会像这样得到错误:
10:07:35,238 INFO [STDOUT] :::错误查询历史到数据库 …