java 中一个名为 charAt() 的方法不起作用?



java charAt((中有一个方法在我的程序上不起作用。我用谷歌搜索了它,发现它可能位于一个名为java.lang.String;的包中。但它仍然不起作用.如果您有任何解决方案,请帮助我.我正在附加我的代码以注意问题出在哪里.. 希望,你能帮帮我...

这是我的代码

/*
package default;
import java.util.Scanner;
import java.lang.String;
public class Cryptography { //Starting of the class Cryptography 
/*The initialization  of the variables */
private int i;
private int UserChoice;
private String  UserValue;
private  char []  Word;
private char Blank;
private char IncrementedWord;
private int EncryptedWord;
/*The initialization of the variables */

Scanner GetValue = new Scanner(System.in);
public void UserPassword() { // Starting of the function UserPassword 
System.out.println("Enter your password :: ");
UserValue = GetValue.nextLine();

} //End of the function UserPassword

public void EnCryption() {  // Starting of the function Encryption
Word = new char[ UserValue.toCharArray().length + 100 ];
Blank = '';
Word = /*Blank + */ UserValue.toCharArray();

for( i = 0; i < Word.length; i++ ){
if(Character.isUpperCase(Word.charAt(i))){
char Charecter = (char)((int)Word)
}
}
}  //End of the function Encryption 
public void DeCryption() {}

public void UserChoice() { //Starting of the function UserChoice 
System.out.println("Enter 1 for EnCryption .. ");
System.out.println("Enter 2 for DeCryption .. ");
UserChoice = GetValue.nextInt();
switch( UserChoice ) { // Starting of the switch case
case 1: { // Starting of the case 1  
EnCryption();
break; 
} case 2 : { //Starting of the case 2 
DeCryption();
break;
} default : { //Starting of the default 
System.out.println(" Invalid Choice ");
} // End of the defalut 

} // End of the switch case 
} // End of the function UserChoice

} //End of the class Cryptography
*/

您的Word实例成员是char[],而不是String。它们是两个非常不同的东西。从规范:

在Java编程语言中,与C不同,char数组不是StringString数组和char数组都不会以'u0000'(NUL字符(终止。

String对象是不可变的,也就是说,它的内容永远不会改变,而char数组具有可变元素。

要在名为Wordchar[]中访问索引i处的char,请使用Word[i]

最新更新