Java 子串与空白

Java 子串与空白,java,string,Java,String,这可能是一个简单的问题。我正在从文件中读取一个1645字符的字符串。所有内容都分解为特定大小的子字符串。假设我有一个18、30和1个字符的子字符串。他们沿着那个条路一直走到绳子的尽头。有时,1个字符的子字符串中没有任何内容,这很好,因为不会总是有。如何跳过空白,继续循环?现在,当我的循环到达1个字符的子字符串时,如果那里有空白,它会出错并说超出范围 try{ //open input stream for reading the text file InputStream is

这可能是一个简单的问题。我正在从文件中读取一个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();
}
试试看{
//打开输入流以读取文本文件
InputStream is=新文件InputStream(“try34.txt”);
//创建新的输入流读取器
InputStreamReader instrm=新的InputStreamReader(is);
//创建bufferReader对象的对象
BufferedReader br=新的BufferedReader(instrm);
//一次读一行
而((strLine=br.readLine())!=null)
{
System.out.println(strLine);//打印读入内容的完整字符串
parcelNumber=strLine.substring(0,14);//获取15个字符的地块号
System.out.printf(“包裹号%s\n”,parcelNumber);//打印包裹号
width=Integer.parseInt(strLine.substring(13,19));//从strLine字符串获取宽度
height=Integer.parseInt(strLine.substring(19,24));//从strLine字符串获取高度
System.out.printf(“宽度%05d\n”,宽度);//打印宽度
System.out.printf(“高度%05d\n”,高度);//打印高度
String getR;//用于保存字符串以计算有多少个矩形的变量
getR=strLine.substring(241645);//从位置58开始查找矩形的“R”
String strippedR=getR.replaceAll(“\\s+”,“”);//删除getR字符串中的空格。如果保留空格,则它将停止在非矩形建筑物上
系统输出打印LN(剥离器);
//这个循环只通过子串24到1645来查找“R”,并计算有多少个
for(char ch:getR.toCharArray()){
如果(ch='R'){
计数++;
}
}
System.out.println(count);//打印找到了多少个R
字符串优先;//1621个字符串,用于保存每个矩形x、y、尺寸、标志和矩形数量的完整字符串
String tln;//18个字符的字符串,用于保存点和维度
int mRec=0;//初始化起始子字符串
first=strLine.substring(241645);//我们从中解析x、y、维度、标志和矩形数的完整字符串
System.out.println(第一);//打印1621字符串
//循环,它遍历1621字符串并将其拆分为18个字符串,然后
//列出绘制地图所需的所有信息

对于(int i=0;i来说,不清楚为什么会在没有任何代码的情况下遇到错误,但是如果使用a并使用
next()
方法,将免费忽略空白

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

请告诉我们您尝试了什么我们不知道您是如何读取数据的,因为您没有告诉我们任何事情。
String current;
Scanner in = new Scaner(new File("MyFileName"));
while(in.hasNext()){
   current = in.next();
// Process the current String 
//...
}