这可能是一个简单的问题。我正在从文件中读入一个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文件,我无法访问或修改。 &#34;标志&#34;变量有时会因设计而变空。我的程序抛出一个错误,当它没有看到标志应该在哪里时停止。
答案 0 :(得分:1)
我们真的不清楚为什么你在没有任何代码的情况下遇到错误,但是如果你使用Scanner
并使用next()
方法,你将免费忽略空格。
String current;
Scanner in = new Scaner(new File("MyFileName"));
while(in.hasNext()){
current = in.next();
// Process the current String
//...
}
答案 1 :(得分:0)
if (myString.trim().isEmpty()) {
continue;
}