==到目前为止,我一直在我的程序中使用运算符来比较我的所有字符串.但是,我遇到了一个错误,将其中一个更改为了.equals(),并修复了该错误.
是==坏?什么时候应该不应该使用它?有什么不同?
此代码将字符串分隔为标记并将它们存储在字符串数组中,然后将变量与第一个主页进行比较...为什么它不起作用?
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) for (int i = 0; i< expArray.length; i++){
System.out.println(expArray[i]);
if ( expArray[i] == "5" ){ ///WHY IS THIS NOT WORKING!!!??
System.out.println("here");
Run Code Online (Sandbox Code Playgroud)
我知道输出System.out...:
0
=
0
+
5
Run Code Online (Sandbox Code Playgroud)
这是我的String阵列的制作方式:
for (int i = 0; i< expArray.length; i++){
if(varTable.get(expArray[i]) != null){
expArray[i] = Integer.toString((int) varTable.get(expArray[i]));
}
}
Run Code Online (Sandbox Code Playgroud)
所以问题是,在expArray[i] == "5" )if block up up它不会进入i=1它应该的位置.任何理由/想法?