输入字符串长度超过60

时间:2015-05-08 18:58:04

标签: java string

我正在编写一个程序来计算给定序列中氨基酸代码的概率。当我尝试从用户获取输入时,它显示以下错误

Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 407
    at java.lang.String.charAt(String.java:646)
    at BNC.Sequence.<init>(Sequence.java:50)
    at BNC.Navie_Bayesian_Classifier.main(Navie_Bayesian_Classifier.java:34)
407Java Result: 1

我的代码是

      try(Scanner scan=new Scanner(new File("F:\\Thiyaga\\ProteinSequence\\Test.txt")))
    {


    line = scan.nextLine().trim();
    length=line.length();
 //   line=reader.readLine();
    }
          System.out.print(length);
    for(i=0;i<20;i++)
    {
        count[i]=0;
        probility[i]='\0';
    }
            for(int k=0;k<length;k++)
    {
        switch(line.charAt(k))
                    {

它在开关或长度显示错误

我的输入是这种形式

  

MVKETKFYDILGVPVTATDVEIKKAYRKCALKYHPDKNPSEEAAEKFKEASAAYEILSDPEKRDIYDQFGEDGLSGAGGAGGFPGGGFGFGDDIFSQFFGAGGAQRPRGPQRGKDIKHEISASLEELYKGRTAKLALNKQILCKECEGRGGKKGAVKKCTSCNGQGIKFVTRQMGPMIQRFQTECDCHGTGDIIDPKDRCKSCNGKKVENERILEVHVEPGMKDGQRIVFKGEADQAPDVIPGDVVFIVSERPHKSFKRDGDDLVYEAEIDLLTAIAGGEFALEHVSGDWLKVGIVPGEVIAPGMRKVIEGKGMPIPKYGGYGNLIIKFTIKFPENHFTSEENLKKLEEILPPRIVPAIPKKATVDECVLADFDPAKYNRTRASRGGANYDSDEEEQGGEGVQCASQ

1 个答案:

答案 0 :(得分:0)

执行所呈现的代码不会导致错误。以下是给定代码段的简化版本,它与给定的输入文件非常吻合:

import java.util.Scanner;
import java.io.File;
import java.io.IOException;

public class XXX{

  public static void main(String[] args){
    try(Scanner scan=new Scanner(new File("Test.txt"))){
      String line = scan.nextLine().trim();
      int length=line.length();
      for(int k=0;k<length;k++)
        System.out.println(k + " " +  line.charAt(k));
     } catch (IOException e){
         e.printStackTrace();
     }
   }
 }