简体   繁体   English

使用Java Scanner类,如何获取特定String后面的最新数字?

[英]Using Java Scanner class ,how do I get the most recent number which comes after specific String?

I have text log files which follow this format: 我有遵循以下格式的文本日志文件:

18:27:08.231 [main] DEBUG sample_client - This is a DEBUG message, line 39 Location 7 18:27:09.231 [main] DEBUG sample_client - This is anr DEBUG message, line 39 Location 17 18:27:10.231 [main] DEBUG sample_client - This is a DEBUG message, line 56 Location 23 18:27:08.231 [main] DEBUG sample_client-这是一条DEBUG消息,第39行7位置18:27:09.231 [main] DEBUG sample_client-这是an DEBUG消息,第39行,位置17 18:27:10.231 [main] DEBUG sample_client-这是一条DEBUG消息,第56行位置23

I would like to obtain the most recent integer, that comes after the String "Location" - here it is 23. 我想获取字符串“ Location”之后的最新整数-这里是23。

The following is my Scanner class so far : 以下是我到目前为止的Scanner类:

 import org.apache.logging.log4j.flume; */
// import org.slf4j.Logger;
// import org.slf4j.LoggerFactory;


import java.sql.Timestamp;
import java.util.Date;
import java.util.*;
import java.io.*;
import java.net.*;

public class ScannerTest {  /* BEGINBRAC */



    public static void main(String[] args) throws IOException { 
        int mostRecentLocation = 0;
        Scanner scanner = new Scanner ("LogbackTutorialError.txt");         

        while (scanner.hasNextLine() ) {

            final String lineFromFile = scanner.nextLine();

            if(lineFromFile.contains("Location")){

                System.out.print("the location is " + mostRecentLocation );
            }

        }


    }

}// end class ScannerTest

How do I go about getting the most recent number after Location ? 如何获取位置信息后的最新号码?

You could do something with String.substring(int) to remove everything from Location on. 您可以使用String.substring(int)进行某些操作,以删除Location上的所有内容。 Something like, 就像是,

if (lineFromFile.startsWith("Location")) {
    int mostRecentLocation = Integer.parseInt(lineFromFile
            .substring(1 + "Location".length()).trim());
    System.out.println("the location is " + mostRecentLocation);
}

The following code shows how to create it. 以下代码显示了如何创建它。

import java.sql.Timestamp;
import java.util.Date;
import java.util.*;
import java.io.*;
import java.net.*;

public class ScannerTest {  

    public static void main(String[] args) throws FileNotFoundException { 
        int mostRecentLocation = 0;
        File newFile = new File("LogbackTutorialError.log");
        Scanner scanner = new Scanner (newFile);         
        int result = 0;
        while (scanner.hasNextLine() ) {
            String lineFromFile = scanner.nextLine();       
            Scanner lineScanner = new Scanner(lineFromFile);
            if(lineScanner.findInLine("Location") != null){
              result = lineScanner.nextInt() ;
              mostRecentLocation = result;           
            }

        }
        System.out.println(" The location is  : " + result );


    }

}// end class ScannerTest

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我如何使用Java解析字符串以获取特定信息? - How do i parse a string to get specific information using java? 如何根据文件名中的日期获取最新文件? - How do I get the most recent file based on a date in the filename? 如何让字符串扫描仪等待? - How do I get the String scanner to wait? 如何在 java 中使用扫描仪仅接受来自用户输入的特定行数 - How can I accept only a specific number of lines from user input using scanner in java 如何在Java中获取目录的最新文件 - How to get the most recent file of a directory in Java 如何在字符串中搜索关键字,然后在Java中打印该关键字之后的内容? - How do I search a string for a keyword, then print whatever comes after that keyword in java? 为什么在使用扫描仪 class 和定界符在 Java 中获得双精度值时会得到错误的数学结果? - Why do I get wrong mathematical results when using scanner class and delimiters for getting a double in Java? 如何使用扫描仪编写类以平均Java中数字的位数 - How to write a class using a scanner to average digits of a number in Java 在Java中使用扫描程序定界符,如何保留用作定界符的字符串? - Using the Scanner delimiter in Java, how do I keep the String that I am using as the delimiter? 如何使用扫描仪 class 使用 java 回答字符串问题? - How to answer a question with a string using the scanner class using java?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM