1

I'm retrieving data from a SQL database with these lines of code

$username = $_POST['username'];
$selector = "SELECT * FROM client_table WHERE SalesmanID ='" . $username . "';";
$result = mysqli_query($con,$selector);

while($row = mysqli_fetch_array($result)) {
    echo  $row['ID'] . "/" . $row['Name'] .  "/" . $row['Address'] .  "/" . $row['Zip Code'] .  "/" . $row['SalesmanID'];
    echo "\\r\\n";
}
?>

On the Java side I do

while ((bufferedStrChunk = bufferedReader.readLine()) != null) {
    stringBuilder.append(bufferedStrChunk);
}

String queryResult = stringBuilder.toString();

and the problem is that when I do

String[] results=queryResult.split("\n");

the string is not split. Could someone please help?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459

2 Answers2

2

You are not adding any line separator to stringBuilder when you are retrieving data from bufferedReader like

stringBuilder.append(bufferedStrChunk).append(System.lineSeparator())

Anyway if you want to get all lines in some sort of collection then you can simply use

List<String> lines = Files.readAllLines(Paths.get("pathToFile"));

In case of BufferedReader you can also use (since Java 8)

List<String> lines = br.lines().collect(Collectors.toList());
Pshemo
  • 122,468
  • 25
  • 185
  • 269
  • 1
    If you use `System.lineSeparator()`, you'll have to switch to a more generic regex, like `"\r?\n|\r"` or (since Java 8) `"\\R"`. And there's really no offsetting benefit; see my answer to [this queston](http://stackoverflow.com/q/247059/20938). I would just use `"\n"` in both places. But, as you said, it would be even better to bypass the string construction and save the lines directly to a List, if that's what you really want. – Alan Moore Jun 20 '15 at 11:23
  • @AlanMoore Thanks for information about `\R` in Java 8, I was not aware of that. Anyway `lineSeparator` was just an example, OP can use any separator he wants and in case of `split` in pre Java 8 we can simply use `text.split(System.lineSeparator)` (assuming that `text` was generated earlier by our app, or in our OS). And yes, I added other variants to show that we don't need to handle splitting part ourselves (we can let proper methods do that for us). – Pshemo Jun 20 '15 at 12:04
1

In PHP code you used escape to \ by \\ that makes string appended with \-char followed by n-character AND in Java code you are splitting by new line char \n. This can be cause of problem.

Try it with escaped \:

String[] results=queryResult.split("\\\\n");
//EDITED: String[] results=queryResult.split("\\n"); //In comment informed that; to escape '\' we need to use four '\\\\'

That should work.

Adarsh Rajput
  • 1,246
  • 14
  • 24
  • `split("\\n")` is the same as `split("\n")`. If you want to split on ``\`` followed by `n` character then you need to write it as `split("\\\\n")` since this method uses regex so you need to escape ``\`` twice: once in string, and once in regex engine. – Pshemo Jun 20 '15 at 10:30
  • @Pshemo: ok... as I am PHP programmer and here meant by escaping `\` in java. I am editing my answer. – Adarsh Rajput Jun 20 '15 at 10:35