2

I have a text file with the name of "w1_slave" as follows:

3c 00 4b 46 ff ff 0d 10 16 : crc=16 YES

3c 00 4b 46 ff ff 0d 10 16 t=29937

From the above, I only need to extract the integer number 29937, and my code is:

public static void main(String[] args) throws IOException {
    // create token1
    String token1 = "";

    // create Scanner inFile1
    @SuppressWarnings("resource")
    // detect delimiter "," and " "
    Scanner w1Slave = new Scanner(new File("C:/Users/Hamid/Desktop/w1_slave.txt")).useDelimiter(",| ");

    List<String> temps = new ArrayList<String>();

    // while loop
    while (w1Slave.hasNext()) {
      // find next line
      token1 = w1Slave.next();
      temps.add(token1);
    }
    w1Slave.close();

    String[] tempsArray = temps.toArray(new String[0]);
    int tempsArrayLength = tempsArray.length - 1;
    System.out.println(tempsArray[tempsArrayLength]);

The output is t=29937. Could you tell me how I can extract just 29937 as an integer or double number, please?

4 Answers 4

5

The easiest way would be to add = as a delimiter - replace

.useDelimiter(",| ");

with

.useDelimiter(",|= ");

To convert it to a double, use

double value = 0;
try {
    value = Double.parseDouble(temps.get(temps.size() - 1));
} catch (NumberFormatException e) {
    // not a number - do something
}

You don't need to convert the list to an array - you can delete this part of your code:

String[] tempsArray = temps.toArray(new String[0]);
int tempsArrayLength = tempsArray.length - 1;
System.out.println(tempsArray[tempsArrayLength]);
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you so much. The delimiter works, but the conversion does not work. it returns 0 instead of 29937.
That's because your conversion from List to array is not correct, and unnecessary. I've updated my answer.
Thanks. It works, but with double not with integer. so I change the codes.
1

My suggestion would be to use the substring function.

String strNumber = tempsArray[tempsArrayLength].substring(2);

Then use parseInt to get the integer value.

int nNumber = Integer.parseInt(strNumber);

Comments

1

You was very near to your code. i think you need to modify your code like this

public static void main(String[] args) throws IOException {
        // create token1
        String token1 = "";

        // create Scanner inFile1
        @SuppressWarnings("resource")
        // detect delimiter "," and " "
        Scanner w1Slave = new Scanner(new File(C:/Users/Hamid/Desktop/w1_slave.txt")).useDelimiter(",| ");

        List<String> temps = new ArrayList<String>();

        // while loop
        while (w1Slave.hasNext()) {
          // find next line
          token1 = w1Slave.next();
          temps.add(token1);
        }
        w1Slave.close();

        String[] tempsArray = temps.toArray(new String[0]);
        int tempsArrayLength = tempsArray.length - 1;
        String str[]=tempsArray[tempsArrayLength].split("=");
        // System.out.println(tempsArray[tempsArrayLength]);
        int i=Integer.parseInt(str[1]);
        System.out.println(i);

}

Comments

0

Use the parseInt or parseDouble functions:

int number = Integer.parseInt(tempsArray[tempsArrayLength]);

or

double number = Double.parseDouble(tempsArray[tempsArrayLength]);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.