简体   繁体   中英

How to read a textfile in java?

How to select numbers from a line by line text file that has both text and numbers?

For example:

[10] begin0-1-selp-2-yelp-25-jelp-21-hi-35-ou

I want to only have 0 1 2 25 21 35 printed out without the [10] . But I keep getting 10012252135 .

This is my code

try {
    Scanner scan = new Scanner(file);

    while (scan.hasNextLine()) {
        String i = scan.nextLine();
        String final_string = "";

        for (int j = 0; j < i.length(); j++) {
            char myChar = i.charAt(j);
            if (Character.isDigit(myChar)) {
                final_string = final_string.concat(Character.toString(myChar));                     
            }
        }

        System.out.println(final_string);                                                                                                       
    }

    scan.close();
} catch (FileNotFoundException e) {
    e.printStackTrace();
}
String tt = "[10] begin0-1-selp-2-yelp-25-jelp-21-hi-35-ou";
tt = tt.replaceAll("\\[.*\\]",""); // IT get rid of any [AnyNumber]
tt = tt.replaceAll("\\D+",""); // It get rid of any char that is not a letter
System.out.println(tt);

I made a regex I can't make it one line but the output is the disered. OutPut

012252135

I like the answer from Reaz Murshed, but just in case you have multiple occurenses of number enclosed in "[]" you might filter those by remembering if you are currently in just an enclosed scope or not:

char NON_NUMERIC_SCOPE_START = '[';
    char NON_NUMERIC_SCOPE_END = ']';

    try {
        Scanner scan = new Scanner(file);

        while (scan.hasNextLine()) {

            String i = scan.nextLine();

            String final_string = "";
            boolean possibleNumericScope = true;
            for (int j = 0; j < i.length(); j++) {
                char myChar = i.charAt(j);
                if (myChar == NON_NUMERIC_SCOPE_START) {
                    possibleNumericScope = false;
                } else if (myChar == NON_NUMERIC_SCOPE_END && !possibleNumericScope) {
                    possibleNumericScope = true;
                } else if (Character.isDigit(myChar) && possibleNumericScope) {
                    final_string = final_string.concat(Character.toString(myChar));
                }
            }
            System.out.println(final_string);

        }
        scan.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }

I think the first portion is the line number which can be easily ommited by splitting your String by space. Please try the following code.

String i = "[10] begin0-1-selp-2-yelp-25-jelp-21-hi-35-ou";
String final_string = "";

// Split the String with space to remove the first portion which
// might be indicating the line number or something like that.
String[] splittedArray = i.split(" ");

// Then just run the loop in the second item of the split
// `String` array.

int contCount = 0;
for (int j = 0; j < splittedArray[1].length(); j++) {
    char myChar = splittedArray[1].charAt(j);
    if (Character.isDigit(myChar)) {
        contCount = 0;
        final_string = final_string.concat(Character.toString(myChar));
    } else {
        if (contCount == 0)
            final_string = final_string + " ";

        contCount++;
    }
}

System.out.println(final_string);

I added a line to get rid of [line-number] before the loop:

    i = i.substring(i.indexOf("]")+1);                 
    for (int j = 0; j < i.length(); j++) {

This should do the trick.

If you always have the [10], then you can just edit your final_string to be everything but the first two numbers. Change System.out.println(final_string) to System.out.println(final_string.substring(2)). Then, if you need spaces, type final_string += " "; in your if statement of the for loop.

Try this:

String y = "[133] begin0-1-selp-2-yelp-25-jelp-21-hi-35-ou";
String result = y.replaceAll("\\[[0-9]*\\]|[a-zA-Z-]*", "");   

Or

String y = "[133] begin0-1-selp-2-yelp-25-jelp-21-hi-35-ou";
String result = y.replaceAll("\\[[\\d]*\\]|[^\\d]", "");

Many ways to do this :)

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