2016-02-26 46 views
-1

我有一个实验室,我必须为我的计算机类做我有一个错误,我似乎无法弄清楚。我收到第一个if声明的错误,if(something.indexOf(x) = "a")。我想将其他if声明更改为该形式。阵列发生意外的类型错误

我得到的错误是:

意想不到的类型 要求:变量:发现;值

Scanner in = new Scanner(System.in); 
String[] input = new String[1000]; 
String[] output = new String[1000]; 
int x = 0;// All purpose counter 
int y = 0;//Second purpose counter 
boolean ends = false; 
boolean starts = false; 
/** 
* This method is supposed to take the dna array and create an rna array from it to return 
* to the main method wherever this method is called. 
* 
* @param String[] input  The array that contains the dna sequence 
* @return String[] output The array that contains the mRNA we just created in this method 
*/ 
public void makeRNA() 
{ 
    System.out.println("Enter a simple DNA Sequence, make sure the amount of variables are a multiple of 3."); 
    String something = in.nextLine(); 
    while(x < 1000) 
    { 
     if(something.indexOf(x) = "a") 
     { 
      output[x] = "u"; 
     } 
     else if(input[x] == "c") 
     { 
      output[x] = "g"; 
     } 
     else if(input[x] == "g") 
     { 
      output[x] = "c"; 
     } 
     else if(input[x] == "t") 
     { 
      output[x] = "a"; 
     }    
     x++; 
    } 
    for(x = 0 ; x < 1000; x++) 
    { 
     System.out.println(output[x]); 
    } 

} 

回答

3

这个问题似乎是在这里:if(something.indexOf(x) = "a")

  • 要获取指数x你需要使用charAt()的字符。
  • 而不是赋值运算符,您需要使用==(比较运算符)。因此charAt()返回一个字符。因此请将"a"更改为'a'

所以,你的语句应该终于看起来像: if(something.charAt(x) == 'a')

1

if(something.indexOf(x) = "a")=是赋值运算符。除非赋值结果为布尔值,否则在if语句中需要==运算符。

indexOf()返回int,所以你不能用"a"使用==,使用equals()字符串比较。

java if语句不能像c或C++一样工作。

+0

然后我得到的错误不兼容的类型:INT和Java。 lang.String –

+0

这是因为'indexOf()'返回一个int并且“a”是一个字符串 – Ramanlfc

+0

我猜你打算使用'.charAt()'? –

0

Ramanlfc是说使用==而不是正确的,因为=只是一个单一的等号是赋值运算符。

但是,我不确定你的IF语句是否在做你想让他们做的事情。 indexOf()方法返回一个整数,你试图使用==(等于)将它与一个字符串(一个对象)进行比较。如果你想比较两个字符串,使用.Equals()方法。你不能在对象上使用==,这是一个字符串。但是,您可以在字符上使用==,因为它们是基本类型。要指定一个char使用单引号而不是双引号(双引号指定一个字符串,该字符串当前是如何设置if语句的)。我假设java将使用char的十六进制值与数字进行比较。再次,我不确定你想要达到什么,但只是一些有用的建议!

我假设你想要的东西,像下面这样: 如果(stringMsg.charAt(INDEXVALUE)==“A”)

这得到字符在串并检查是否在规定值它是一样的(等于)char a。记住字符串中的字符是数字0到(长度 - 1)。

0

问题是这行代码:

if(something.indexOf(x) = "a") // it should be "==" instead of "=" 

正确的代码是:

if(something.indexOf(x) == "a") 

请注意,if(something.indexOf(x) = "a") will always return true in java.

+0

你的答案不会被编译。 'indexOf()'将返回一个'int',并且将它与'String'进行比较。 – Atri