-3

I am doing an android app, where i read the text from an image and i get an output string like below,

NAME : michael /nPHONE NO: 771234521/n e-Mail: michael@gmai|.com /nCompany: Google/n

I want to extract them individually as a string, and assign to a variable like,

String Name = "michael"; String Phone = "771234521";

how can i achieve like this,here is my piece of code,

input.setText(recognizedText);
String[] separated = recognizedText.split("/");
for (String s: separated)
{

}
Sajeetharan
  • 216,225
  • 63
  • 350
  • 396

5 Answers5

1
public static void main(String... args) {
        String s = "NAME : michael /nPHONE NO: 771234521/n e-Mail: michael@gmai|.com /nCompany: Google/n";

        String[] str = s.split("/n");
            for(int i=0;i<str.length;i++)
             {
             str[i]=str[i].trim();
             }
        for (int k = 0; k < 4; k++) {
            System.out.println(str[k]);
    }
}

O/P :

NAME : michael 
PHONE NO: 771234521
e-Mail: michael@gmai|.com 
Company: Google
TheLostMind
  • 35,966
  • 12
  • 68
  • 104
1

I think want to parse your fields into a Map, and that they are separated on the newline character (\n). Perhaps like this -

// Convert the string in to a Map<String, String>.
public static Map<String, String> toMap(String in) {
  // the return map.
  Map<String, String> map = new HashMap<String, String>();
  // a tokenizer on newline
  StringTokenizer st = new StringTokenizer(in, "\n");
  while (st.hasMoreTokens()) {      // while there are lines.
    String token = st.nextToken();  // get the line.
    String[] kv = token.split(":"); // split on colon.
    if (kv.length > 1) {            // check that there's a key and a
                                    // value.
      map.put(kv[0].trim(), kv[1].trim()); // add it to
                                           // the map.
    }
  }
  return map; // return the map.
}

public static void main(String[] args) {
  String str = "NAME : michael \nPHONE NO: 771234521\n"
      + " e-Mail: michael@gmai|.com \nCompany: Google\n";
  Map<String, String> map = toMap(str);
  System.out.println(map);
}

Which outputs this here -

{NAME=michael, e-Mail=michael@gmai|.com, Company=Google, PHONE NO=771234521}
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
0

You should use String Tokenizer like this

Tokenize the string based on your delimeter /

Community
  • 1
  • 1
Viswanath Lekshmanan
  • 9,945
  • 1
  • 40
  • 64
0
String src = "NAME : michael /nPHONE NO: 771234521/n e-Mail: michael@gmai|.com /nCompany: Google/n";
        String noLineSrc = src.replace("/n", ",");
        String[] couples = noLineSrc.split(",");
        Map<String, String> result = new HashMap<String, String>();
        for(String couple : couples) {
            String[] coupleValue = couple.split(":");
            result.put(coupleValue[0].trim(), coupleValue[1].trim());
        }
Longwayto
  • 396
  • 2
  • 6
0

The keys are: "NAME:", "PHONE NO:", "e-Mail:", and "Company:".

After splitting the string with split("/n"), you can do a str[i].contains("Name:") to check for the key.

The value comes after the ":". So, int pos = str[i].indexof(":") + 1 will be the starting position of the value.

String value = str[i].substring(pos) gives you the value.

Then value.trim() to remove spaces.

Rick Falck
  • 1,778
  • 3
  • 15
  • 19