-3

I want to split this two columns on java and put the french words in an arraylist and the English words in an another arraylist , thanks for help

bonjour;goodmorning
bonsoir;goodevening
chat;cat
comment;how
pourquoi;why
PM 77-1
  • 12,933
  • 21
  • 68
  • 111
user3144878
  • 47
  • 1
  • 1
  • 3
  • 1
    What have you already tried to solve the problem yourself? – PM 77-1 Dec 29 '13 at 23:35
  • 1
    Welcome to SO! Please read the [About](http://stackoverflow.com/about) page: "Don't ask about... Questions you haven't tried to find an answer for (show your work!)". – Jongware Dec 29 '13 at 23:36
  • 1
    Start by reading the [String JavaDocs](http://docs.oracle.com/javase/7/docs/api/java/lang/String.html) and try searching SO for more examples... – MadProgrammer Dec 29 '13 at 23:37
  • possible duplicate of [How to split a String in Java](http://stackoverflow.com/questions/3481828/how-to-split-a-string-in-java) – brandonscript Mar 04 '14 at 21:47

1 Answers1

1

Try something like this

private static final String safeTrim(String in) {
  if (in == null) {
    return "";
  }
  return in.trim();
}

public static void main(String[] args) {
  String in = "bonjour;goodmorning bonsoir;goodevening"
      + " chat;cat comment;how pourquoi;why";
  java.util.List<String> frenchList = new java.util.ArrayList<String>();
  java.util.List<String> englishList = new java.util.ArrayList<String>();
  String[] pairs = in.split(" ");
  // Split the input into pairs.
  for (String p : pairs) {
    // Split the pairs into words.
    String[] words = p.split(";");
    // Check that there are two words.
    if (words != null && words.length == 2) {
      if (safeTrim(words[0]).length() > 0
          && safeTrim(words[1]).length() > 0) {
        frenchList.add(words[0].trim());
        englishList.add(words[1].trim());
      }
    }
  }
  // Display the results.
  for (int i = 0; i < frenchList.size(); i++) {
    System.out.printf(
        "\"%s\" is \"%s\" en Français!\n",
        englishList.get(i), frenchList.get(i));
  }
}

Which, when I run it, outputs -

"goodmorning" is "bonjour" en Français!
"goodevening" is "bonsoir" en Français!
"cat" is "chat" en Français!
"how" is "comment" en Français!
"why" is "pourquoi" en Français!
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249