Java 添加子字符串以省略部分输出

Java 添加子字符串以省略部分输出,java,Java,下面是我的代码。。。 下面的代码是一些辐射读数的.txt文件。我的工作是在5个计数内找到文件中每分钟的最大计数数。 我已经让它工作了,但是我需要省略行中的一部分,所以我想我可以制作这段代码: /* String temp = new String(data) * temp=list.get(i); * System.outprintln(temp.substring(0,16) +" "); */ 并将其整合到。我一直在尝试几个案例,但我没有思考。有什么建议吗 `import java.

下面是我的代码。。。 下面的代码是一些辐射读数的.txt文件。我的工作是在5个计数内找到文件中每分钟的最大计数数。 我已经让它工作了,但是我需要省略行中的一部分,所以我想我可以制作这段代码:

/* String temp = new String(data) 
* temp=list.get(i);
* System.outprintln(temp.substring(0,16) +"   ");
*/
并将其整合到。我一直在尝试几个案例,但我没有思考。有什么建议吗

`import java.util.*;
//Import utility pack, *look at all classes in package.
import java.io.*;
//Good within directory.
public class counterRadiation {

private static String infile = "4_22_18.txt";
//Input
private static String outfile = "4_22_18_stripped.txt";
private static Scanner reader;
//Output


public static void main(String[] args) throws Exception{
    //throw exception and then using a try block

    try {
        //Use scanner to obtain our string and input.
        Scanner play = new Scanner(new File(infile));

        /* String temp = new String(data)
         * temp=list.get(i);
         * System.outprintln(temp.substring(0,16) +"   ");
         */

        Writer writer = new BufferedWriter(new OutputStreamWriter(
                new FileOutputStream(outfile), "utf-8"));

        String lineSeparator = System.getProperty("line.separator");
        play.useDelimiter(lineSeparator);



        while (play.hasNext()) {
            String line = play.next();

            if (line.matches(dataList)) {
                writer.write(line + "\r\n");
            }
        }
        writer.close();
        play.close();



        try {
            reader = new Scanner(new File(infile));
            ArrayList<String> list = new ArrayList<String>();
            while (reader.hasNextLine()) {
                list.add(reader.nextLine());
            }
            int[] radiCount = new int[list.size()];
            for (int i = 0; i < list.size();i++) {
                String[] temp = list.get(i).split(",");
                radiCount[i] = (Integer.parseInt(temp[2]));
            }
            int maxCount = 0;
            for (int i = 0; i < radiCount.length; i++) {
                if (radiCount[i] > maxCount) {
                    maxCount = radiCount[i];
                }
            }

            for (int i = 0;i < list.size() ;i++) {
                if(radiCount[i] >= maxCount - 4) {
                System.out.println(list.get(i)+"  "+ radiCount[i]);



                }
            }





        }catch(FileNotFoundException e){

        }
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}
}`
`import java.util.*;
//导入实用程序包,*查看包中的所有类。
导入java.io.*;
//好的目录。
公共级反辐射{
私有静态字符串infle=“4_22_18.txt”;
//输入
私有静态字符串outfile=“4_22_18_stripped.txt”;
专用静态扫描仪;
//输出
公共静态void main(字符串[]args)引发异常{
//抛出异常,然后使用try块
试一试{
//使用扫描仪获取字符串和输入。
扫描仪播放=新扫描仪(新文件(内嵌));
/*字符串温度=新字符串(数据)
*temp=列表获取(i);
*系统输出ln(温度子串(0,16)+”);
*/
Writer Writer=new BufferedWriter(new OutputStreamWriter(
新文件输出流(输出文件),“utf-8”);
字符串lineSeparator=System.getProperty(“line.separator”);
play.useDelimiter(行分隔符);
while(play.hasNext()){
字符串行=play.next();
if(行匹配(数据列表)){
writer.write(行+“\r\n”);
}
}
writer.close();
play.close();
试一试{
读卡器=新扫描仪(新文件(填充));
ArrayList=新建ArrayList();
while(reader.hasNextLine()){
list.add(reader.nextLine());
}
int[]radiCount=新int[list.size()];
对于(int i=0;imaxCount){
最大计数=半径计数[i];
}
}
对于(int i=0;i=maxCount-4){
System.out.println(list.get(i)+“”+radiCount[i]);
}
}
}catch(filenotfounde异常){
}
}捕获(IOE异常){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
}
}`

虽然不太清楚要删除什么,但可以使用
.indexOf(String str)
定义要排除的子字符串的第一次出现。例如,在您的代码中:

     String data = "useful bit get rid of this";
     int index = data.indexOf("get rid of this");
     System.out.println(data.substring(0,index) + "are cool");
      //Expected result:
      //"useful bits are cool"

什么是您的输入字符串,什么是预期的输出?”我没有想到“对不起,但是思考是获得帮助的先决条件。