简体   繁体   中英

read specific substring with condition

I have this from text file:

134.897 134.4565 135.134

I read them using :

  while (( line = buffreader.readLine()) != null) {

                String[] parts = line.split(" ");
                    String x1 = (parts[0]);   
                String x2 = (parts[1]);  
                    String x3 = (parts[2]); 

From the String in the text file :

134.897
134.4565
135.134

I just want to take the different number between these three number :

4.897
4.4565
5.134

Given more example :

Text file :

101.435 
101.423 
101.322

Number I want to take :

435
423 
322

My plan is I want to compare each number with others,

101.435//x1
101.423//x2
101.322//x3

if x1.substring(0)=x2.substring(0)=x3.substring(0)
then remove substring(0).

I want to loop this "if" condition until the substring are not same.

Please give me some idea, thanks

Here's the skeleton of an algorithm that could be used:

List<String> inputs = ...;
int index = 0;
while (allTheStringsHaveTheSameCharacterAtIndex(inputs, index)) {
    index++;
}
List<String> outputs = takeSubstringOf(inputs, index);

I'll let you fill the blanks, which are very easy to implement with the methods offered by the String class, and loops.

You didn't answer @Steve about scenario of different length of numbers, I'll assume you want to compare numbers according to characters order. Also, I assume that the file always contains three strings in one line, saperated by one white space.

Given this, I hope this helps:

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;

public class FindDifference {
    static File f = null;
    ArrayList<String> inputs = new ArrayList<String>();
    static String x1 = null;
    static String x2 = null;
    static String x3 = null;

    private static ArrayList<String> getDifferenceList() {
        ArrayList<String> ret = new ArrayList<String>();
        if (x1 == null || x1.isEmpty()
                || x2 == null || x2.isEmpty()
                || x3 == null || x3.isEmpty()) {

            ret.add(x1);
            ret.add(x2);
            ret.add(x3);

            return ret;
        }

        int index = 0;

        while (index < x1.length()
                && index < x2.length()
                && index < x3.length()) {
            if (x1.charAt(index) != x2.charAt(index)
                    || x1.charAt(index) != x3.charAt(index)) {
                break;
            }
            index++;
        }

        ret.add(x1.substring(index, x1.length()));
        ret.add(x2.substring(index, x2.length()));
        ret.add(x3.substring(index, x3.length()));

        return ret;
    }

    public static void main(String[] args) {
        if (args.length > 0) {
            f = new File(args[0]);
        } else {
            System.out.println("Please supply file path.");
            return;
        }

        String line;

        BufferedReader buffreader = null;

        try {
            buffreader = new BufferedReader(new FileReader(f));
            while (( line = buffreader.readLine()) != null) {
                String[] parts = line.split(" ");
                x1 = (parts[0]);   
                x2 = (parts[1]);  
                x3 = (parts[2]); 
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        ArrayList<String> output = getDifferenceList();
        try {
            System.out.println(output.get(0));
            System.out.println(output.get(1));
            System.out.println(output.get(2));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }   
}

I made it SSCCE. You can use only getDifferenceList (supplying x1,x2,x3 as fields) in your code (delete "static" wherever needed).

EDIT

As I said, you can use getDifferenceList in any scope (including Android application). I copy this method separately, simply copy-paste it into any class you want, and call it passing the file path:

    ArrayList<String> getDifferenceList(String filePath) {
        File f = new File(filePath);

        String line, x1 = null, x2= null, x3 = null;

        BufferedReader buffreader = null;

        try {
            buffreader = new BufferedReader(new FileReader(f));
            while (( line = buffreader.readLine()) != null) {
                String[] parts = line.split(" ");
                x1 = (parts[0]);   
                x2 = (parts[1]);  
                x3 = (parts[2]); 
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        ArrayList<String> ret = new ArrayList<String>();
        if (x1 == null || x1.isEmpty()
                || x2 == null || x2.isEmpty()
                || x3 == null || x3.isEmpty()) {

            ret.add(x1);
            ret.add(x2);
            ret.add(x3);

            return ret;
        }

        int index = 0;

        while (index < x1.length()
                && index < x2.length()
                && index < x3.length()) {
            if (x1.charAt(index) != x2.charAt(index)
                    || x1.charAt(index) != x3.charAt(index)) {
                break;
            }
            index++;
        }

        ret.add(x1.substring(index, x1.length()));
        ret.add(x2.substring(index, x2.length()));
        ret.add(x3.substring(index, x3.length()));

        return ret;
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM