2014-05-19 3 views
-2

이것은 아마도 간단한 질문 일 것입니다. 파일에서 1645 자의 문자열을 읽었습니다. 모든 것이 특정 크기의 하위 문자열로 나뉩니다. 18, 30 및 1 문자의 하위 문자열이 있다고 가정 해 보겠습니다. 그들은 문자열의 끝까지 그런 식으로 반복합니다. 때로는 1 문자의 하위 문자열에는 아무 것도 없습니다. 항상 존재하지는 않을 것이기 때문에 괜찮습니다. 공백을 건너 뛰고 루프를 계속하려면 어떻게합니까? 바로 지금 거기에 공백이 있다면 내 루프가 1 문자 하위 문자열에 도착하면 오류가 발생하고 범위를 벗어납니다.부분 문자열과 공백

try{ 
    //open input stream for reading the text file 
    InputStream is = new FileInputStream("try34.txt"); 

    //create new input stream reader 
    InputStreamReader instrm = new InputStreamReader(is); 

    // create object of bufferReader object 
    BufferedReader br = new BufferedReader(instrm); 



    //Read one line at a time 
    while((strLine = br.readLine()) !=null) 
    { 

     System.out.println(strLine); //prints full string of what was read in 
     parcelNumber = strLine.substring(0,14); //gets 15 character parcel  number 
     System.out.printf("parcel number %s\n",parcelNumber); //prints parcel number 

     width = Integer.parseInt(strLine.substring(13,19)); //gets width from strLine string 
     height = Integer.parseInt(strLine.substring(19,24)); //gets height from strLine string 

     System.out.printf("width %05d\n", width); //prints width 
     System.out.printf("height %05d\n", height); //prints height 



     String getR; //variable to hold string to count how many rectangles there are 
     getR = strLine.substring(24,1645); //starts at position 58 to find "R" for rectangle 
     String strippedR = getR.replaceAll("\\s+", ""); //removes spaces in the getR string. If white space was left it would stop at non rectangle buildings 
     System.out.println(strippedR); 

     //this loop goes through only substring 24 to 1645 to look for "R" and counts how many there are 
     for(char ch: getR.toCharArray()){ 
      if(ch == 'R'){ 
       count++; 
      } 
     } 
     System.out.println(count); //prints how many R's were found 



     String first; //1621 character string, complete string to hold each rectangles x's,y's,dimensions,flags,and number of rectangles 
     String tln;//18 character string that holds points and dimensions 
     int mRec = 0; //initializes the starting substring 
     first = strLine.substring(24,1645); //complete string where we parse the x,y,dimension,flag, and number of rectangles from 
     System.out.println(first); // prints the 1621 character string 


     //loop that goes through the 1621 character string and breaks it into 18 characters strings and then pulls 
     //out the all of the info needed to draw the map 
     for (int i=0; i<count; i++){ 
      tln = first.substring(mRec,mRec+18); //pulls out chunks of 18 character long sub strings 
      x1[i] = Integer.parseInt(tln.substring(0,3)); //gets first x value 
      y1[i] = Integer.parseInt(tln.substring(3,6)); //gets first y value 
      x2[i] = Integer.parseInt(tln.substring(6,9)); //gets second x value 
      y2[i] = Integer.parseInt(tln.substring(9,12)); //gets second y value 
      dim1[i] = Integer.parseInt(tln.substring(12,15)); //gets first dimension for rectangle, the length 
      flag[i] = tln.substring(15,16); //gets the "R" for rectangle 


      finalWidth = (width*10)+(x1[0]*10)+4; //multiplies width by 10 and adds 4 for padding to the index of the rectangle its currently working with 
                //used for drawing the correct size window 
      finalHeight = (height*10)+(y1[0]*10+4); //multiplies height by 10 and adds 4 for padding to the index of the rectangle its currently working with 
                //used for drawing the correct size window 

      System.out.println(tln); //prints each 18 character sub string 
      System.out.printf("x1 %03d\n",x1[i]); //prints each x value 
      System.out.printf("y1 %03d\n", y1[i]); //prints each y value 
      System.out.printf("x2 %03d\n", x2[i]); //prints each x2 value 
      System.out.printf("y2 %03d\n" ,y2[i]); //prints each y2 value 
      System.out.printf("length %03d\n", dim1[i]); //prints the length, or first dimension 
      System.out.printf("flag %s\n", flag[i]); //prints the R 
      System.out.printf("parcel count %02d\n", lPoint[i]); //prints the number of the rectangle associated with the dimensions above 

      mRec +=18; //increments starting substring by 18 to get to the next "record" 

      } 
    } 
    br.close(); 
}catch(Exception e){ 
    e.printStackTrace(); 
} 

그래서이 프로그램은 몇 년 전에 작성된 다른 프로그램에서 txt 파일을 가져오고 있으며 액세스하거나 수정할 수 없습니다. "플래그"변수는 때때로 비어있을 수 있습니다. 내 프로그램은 오류를 던지고 플래그가 있어야하는 곳이 보이지 않으면 멈 춥니 다.

+2

음 ... 코드 하시겠습니까? –

+0

pls 우리는 무엇을 시도했는지 보여줍니다 – sunleo

+1

** 당신이 아무 것도 말하지 않았기 때문에 ** 당신이 데이터를 어떻게 읽는지 모르겠습니다 **. –

답변

0
if (myString.trim().isEmpty()) { 
    continue; 
} 
1

당신이 어떤 코드없이 오류가 발생하는 이유는 정말 명확하지 않다, 그러나 당신이 Scanner를 사용하고 next() 방법을 사용하는 경우, 당신은 공백 무료로 무시 얻을 것이다.

String current; 
Scanner in = new Scaner(new File("MyFileName")); 
while(in.hasNext()){ 
    current = in.next(); 
// Process the current String 
//... 
} 
관련 문제