fardinabir
fardinabir

Reputation: 124

How can I get Integer from a string containing numbers,characters?

I was trying to get an int (serial number of a student) from a string looks like "122. Fardinabir 170213" , where "122" is his serial number.

Before this, I tried using nextInt() method with a Scanner of the String, but nextInt() failed to do the job.

Then I have tried this procces...

import java.util.Scanner;
public class tempfile {
    public static void main(String[] args) {
        int serial_int = 0;
        String serial_st;
        serial_st =  find_serial("122. FardinAbir 170213") ;
        System.out.println(serial_st);
        serial_int = Integer.parseInt(serial_st);
        System.out.println(serial_int);
    }
    public static String find_serial(String st)
    {
        String[] parts = st.split(" ");   //  the first serial part will remain in parts[0]
        parts[0].replaceAll("[\\D]", ""); //  As I know it will remove non integer and the pure int serial will remain
        return parts[0];  
    }
}

But, replaceAll("[\\D]", "") is not working...

Can anyone please help me to solve this or find a way out to this job...

Thanks in advance...

Upvotes: 2

Views: 106

Answers (4)

Pavel Molchanov
Pavel Molchanov

Reputation: 2409

String line = "122. FardinAbir 170213";
Pattern pattern = Pattern.compile("^(\\d+)");
Matcher matcher = pattern.matcher(line);
if(matcher.find()) {
    int id = Integer.parseInt(matcher.group(1));
    System.out.println(id);
}

Upvotes: 3

Pieter Mantel
Pieter Mantel

Reputation: 123

This'll do:

public static int getSerialNumber() {
    String id = "122. Fardinabir 170213";
    int place = 0;
    for(int i = 0; i < id.length();i++) {
        if(id.charAt(i) == '.') {
            place = i;
            break;
        }
    }
    return Integer.parseInt(id.substring(0, place));
}

EDIT: you can also do it like this:

public static int getSerialNumber(String name) {
    return Integer.parseInt(name.substring(0, name.indexOf('.')));
}

thanks @Andreas for that solution.

Upvotes: -2

Andreas
Andreas

Reputation: 159086

Since you tried using nextInt it seems you just want leading digits, which means you can use the following regex code:

public static String find_serial(String st) {
    Matcher m = Pattern.compile("^\\d+").matcher(st);
    return (m.find() ? m.group() : null);
}

You could also rely in the serial ending with a period, though that doesn't validate that serial is all digits:

public static String find_serial(String st) {
    int idx = st.indexOf('.');
    return (idx > 0 ? st.substring(0, idx) : null);
}

Upvotes: 1

Nikky
Nikky

Reputation: 518

Assuming you also want to get the rest of the string eventually you can use Regex groups

String line = "122. FardinAbir 170213";
Pattern pattern = Pattern.compile("(\\d+)\\.\\s+([^\\s]+)\\s+(\\d+)");
Matcher matcher = pattern.matcher(line);
while (matcher.find()) {
    System.out.println("serial: " + matcher.group(1));
    int serial = Integer.parseInt(matcher.group(1));
    System.out.println("group 2: " + matcher.group(2));
    System.out.println("group 3: " + matcher.group(3));
}

nextInt() probably did not work because scanner expects newline separation

Upvotes: 3

Related Questions