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?

2 Answers 2

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());
Sign up to request clarification or add additional context in comments.

2 Comments

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. 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.
@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).
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.

2 Comments

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: ok... as I am PHP programmer and here meant by escaping `` in java. I am editing my answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.