2017-02-16 1 views
0

에서 읽을 때이 탭으로 구분 된 그것이 ID (int)를 가지고 파일 내용 (String)를 읽고 다음과 같은 방법. 내 방법은 라인으로 파일 라인을 읽고, 탭 구분 기호를 사용하여, 정말 같은 이중 연결리스트에 ID와 문자열을 구문 분석 :는, ArrayIndexOutOfBoundsException 파일

void readAndAssemble(String fileName) 
    { 
     Scanner sc; 
     try 
    { 
     sc = new Scanner(new File(fileName)); 
     while (sc.hasNextLine()) 
     { 
      String line = sc.nextLine(); 
      String lines[] = line.split("\t"); 
      int packetID = Integer.parseInt(lines[0]); 
      // ----------------------------------------- 
      String packetContent = lines[1]; // gives error in terminal 
      // ----------------------------------------- 
      DLLNode curr = header.getNext(); 
      DLLNode prev = header; 
      while (packetID > curr.getPacketID()) 
      { 
       prev = curr; 
       curr = curr.getNext(); 
      } 
      DLLNode newNode = new DLLNode(packetID, packetContent, prev, curr); 
      prev.setNext(newNode); 
      curr.setPrev(newNode); 

     } 
     sc.close(); 
    } catch (FileNotFoundException e) 
    { 
     System.out.println("File does not exist"); 
    } 

} 

를이 방법은 내가 실행할 때 완벽하게 이클립스 작동 내가 javac의를 사용하고 터미널에서 실행할 때,하지만 날이 오류를 제공합니다

내 MessageAssembler 클래스는 다음과 같습니다
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1 
at DLL.readAndAssemble(DLL.java:40) 
at MessageAssembler.main(MessageAssembler.java:11) 

:

public class MessageAssembler 
{ 
    public static void main(String[] args) 
    { 
     DLL myDLL = new DLL(); 
     myDLL.readAndAssemble("Mystery3.txt"); 
     myDLL.printContent(); 
    } 
} 

무엇이 원인 일 수 있습니까?

+0

은 아래 라인의 어느 쪽이든 당신은 문제가있을 수 있습니다 INT packetID = 시도 정수 unix 파일

에 스캐너를 사용에 문제가 발생한 것 같습니다 일을하려고 .parseInt (lines [0]); //---------------------------------------- String packetContent = lines [ 1]; // 터미널에 에러를 준다. 코드에서 줄 번호 11을 확인하십시오. – GrabNewTech

+0

파일의 일부를 게시 할 수 있습니까? – BrunoDM

+0

에서 찾을 수 있습니다. http://www.eecs.yorku.ca/course_archive/2016-17/W/2011/Mystery.txt –

답변

1

파일에 내용을 이해하지 못하는 것 같습니다.

String lines[] = line.split("\t"); 
if (lines.length < 2) { 
    System.err.println ("error with line " + line); 
    continue; 
} 

FileInputStream fstream = new FileInputStream("c:/temp/a.txt"); 
    BufferedReader br = new BufferedReader(new InputStreamReader(fstream)); 

    String line; 

    //Read File Line By Line 
    while ((line = br.readLine()) != null) { 
     // Print the content on the console 
     System.out.println (line); 
     String [] lines = line.split ("\t"); 
     if (lines.length < 2) { 
      System.err.println ("error with line " + line); 
      continue; 
     }   

    } 

    //Close the input stream 
    br.close(); 
+0

http://stackoverflow.com/a/8331416/2310289에서 중복 파일 찾기 @HovercraftFullOfEels –

관련 문제