Java 对照整数检查字符数组中的字符

Java 对照整数检查字符数组中的字符,java,Java,我有一个如下所示的字符数组: [0,1,1,0,1,0] 我想检查用户提供的索引是否与1匹配: System.out.println("Enter the file index you want"); int fileIndex = input.nextInt(); System.out.println("File you are loo

我有一个如下所示的字符数组:

[0,1,1,0,1,0]

我想检查用户提供的索引是否与1匹配:

                System.out.println("Enter the file index you want");
                int fileIndex = input.nextInt();
                
                System.out.println("File you are looking for is " + myFile[fileIndex]);
当我输入1时,它返回您要查找的文件是1

这就是我想要的:

但当结果为1时,我想做点什么:

                if(myFile[fileIndex] == (char)1) {
                    //Do something because the char at this index is 1
                    System.out.println("HERE")
                }
但是,即使
myFile[fileIndex]
为1,if条件也不起作用。什么是将字符与整数进行比较的正确方法?

试试以下方法:

if((char)myFile[fileIndex] == '1') {
    System.out.println("HERE")
}

字符
'1'
是整数49(以Unicode编码)。通过执行
(char)1
,您将获得标题字符的开头。改为使用
'1'
49
。您是否尝试了Integer.valueOf(character)或Integer.toString()方法?我以前尝试过if((char)myFile[fileIndex]=“1”)但不起作用。因此我们不能使用“.@goxarad784双引号用于字符串,单引号用于字符。我建议在学习更高级的东西之前先学习Java语法的基础知识