I'm trying to get the output from a java application that would use the below code to output values
System.out.println(object.getNumber(3));
System.out.println(object.getNumber(4));
I'm using exec("somejavapath javaName", $output) and print_r($output) to get that output array to print.
I know it will get the values but I wanted to get into a certain format
So instead of
Array
(
[0] => 34
)
I want something like this
Array
(
[0] => 3
[1] => 4
)
Does anyone know what I could do to get this format?
Thanks
If you are not expecting comas in the output from java application , include a coma between the two values :
System.out.println(object.getNumber(3));
System.out.println(","); // Print a coma in between
System.out.println(object.getNumber(4));
Then
$values_array = explode( ',', $output );
You can use explode to break the content up using a delimiter, in this case, a line-break (\n).
$var = explode('\n', $output); // split output by line breaks
Related
I need to display a list in table/grid format, so I'm using String.format() as in the following example,
how to print object list to file with formatting in table format using java
My issue is that I need to force-wrap the output at 80 chars. The table's maximum width is 80, any further output must continue on the next line.
Is this possible?
Current code, without wrapping implemented:
StringBuilder sbOutput = new StringBuilder();
sbOutput.append(String.format("%-14s%-200s%-13s%-24s%-12s", "F1", "F2", "F3", "F4", "F5"));
for (MyObject result: myObjects) {
sbOutput.append(String.format("%-14s%-200s%-13s%-24s%-12s", result.getF1(),
result.getF2(), result.getF3(), result.getF3(), result.getF4()));
}
You can inject a newline into a string every 80 chars like this:
str.replaceAll(".{80}(?=.)", "$0\n");
So your code would become:
sbOutput.append(String.format("%-14s%-200s%-13s%-24s%-12s", result.getF1(),
result.getF2(), result.getF3(), result.getF3(), result.getF4())
.replaceAll(".{80}(?=.)", "$0\n"));
The search regex means "80 chars that have a character following" and "$0" in the replacement means "everything matched by the search".
The (?=.) is a look ahead asserting the match is followed by any character, which prevents output that is an exact multiple of 80 chars getting an unecessary newline added after it.
I am totally new to reg-ex and I want to get validation for the string for valid combination of logical operators like ( ! , & , ( , ) , | ) . for Example if & | combined than it should be invalid as AND OR should come together. likewise possible invalid combination are &|, |& , (), !& ,&! etc
like example of below String
1. (ABC)&(DFG)|!(ZXC) - pass because all operators are correctly combined
2. !(ABC|DKJ)&VBN - pass
3. !(ADF&(!&(BER|CTY))|DGH) = failed as !& combined
4. !(ABC&DKJ)&|VBN - failed as & | combined
I know their several ways like I can use String's contains method to get check and reject if not passed the validation. But I am looking for solution through reg-ex in java
Just to avoid matching invalid operator combos you can use negative lookahead regex like this:
^(?!.*?(&\\||\\|&|\\(\\)|!&|&!))
Use it with MULTILINE option like this for multiline inputs:
Pattern p = Pattern.compile( "(?m)^(?!.*?(&[!|]|[(|]&|\\(\\)))" );
RegEx Demo
For using it with a string input you can do:
boolean value = input.matches( "(?!.*?(&[!|]|[(|]&|\\(\\))).+" );
I have a txt file that contains the following
SELECT TOP 20 personid AS "testQu;otes"
FROM myTable
WHERE lname LIKE '%pi%' OR lname LIKE '%m;i%';
SELECT TOP 10 personid AS "testQu;otes"
FROM myTable2
WHERE lname LIKE '%ti%' OR lname LIKE '%h;i%';
............
The above query can be any legit SQl statement (on one or multiple lines , i.e. any way user wishes to type in )
I need to split this txt and put into an array
File file ... blah blah blah
..........................
String myArray [] = text.split(";");
But this does not work properly because it take into account ALL ; . I need to ignore those ; that are within ";" AND ';'. For example ; in here '%h;i%' does not count because it is inside ''. How can I split correctly ?
Assuming that each ; you want to split on is at the end of line you can try to split on each ; + line separator after it like
text.split(";"+System.lineSeparator())
If your file has other line separators then default ones you can try with
text.split(";\n")
text.split(";\r\n")
text.split(";\r")
BTW if you want to include ; in split result (if you don't want to get rid of it) you can use look-behind mechanism like
text.split("(?<=;)"+System.lineSeparator())
In case you are dynamically reading file line-by-line just check if line.endsWith(";").
I see a 'new line' after your ';' - It is generalizable to the whole text file ?
If you must/want use regular expression you could split with a regex of the form
;$
The $ means "end of line", depending of the regex implementation of Java (don't remember).
I will not use regex for this kind of task. Parsing the text and counting the number of ' or " to be able to recognize the reals ";" delimiters is sufficient.
I am using this bootstrap transfer plugin at UI which i have to populate with the help of data sent from java application. This plugin requires the data to be an array of objects with 'value' and 'content' properties.
I can easily create a list in java and convert it to JSON array, but the problem here is that this plugin requires key names as non string names. I tried using string key names but that just didn't worked. I looked up for ways to create non-string key names in JSON and the only way i could find was to write my own parser, and that was also not recommended. So how can i prepare my data in java for this plugin ??
Edit:
As mentioned in the plugin documentation, here is a sample data to populate it.
$(function() {
...
var t = $('#test').bootstrapTransfer();
t.populate([
{value:"1", content:"Apple"},
{value:"2", content:"Orange"},
{value:"3", content:"Banana"},
{value:"4", content:"Peach"},
{value:"5", content:"Grapes"}
]);
...
});
When i prepare a JSON array, it is like {"value":"1", "content":"Apple"} which doesn't work for this plugin.
You might check how it runs again when you use a quoted string. When accessing a JSON object, the quotes for keys are not needed. Take for example this code (in Nodejs):
var console = require("console");
var data = { "value1" : 13,
"value2" : "hello",
value3: 15,
value4 : "hello again" };
console.log("Value 1 = " + data.value1 );
console.log("Value 2 = " + data.value2 );
console.log("Value 3 = " + data.value3 );
console.log("Value 4 = " + data.value4 );
Some of the object is declared with quoted strings, and some are not. All are accessed without the quotes, and my console shows:
Value 1 = 13
Value 2 = hello
Value 3 = 15
Value 4 = hello again
So it really shouldn't matter how your keys are defined in java. I know that isn't an exact answer to your question of how to do it, but you really shouldn't need to.
My String is huge and it will keep changing as I read each String in a loop. It can contain any characters like " , / , \ . $ ,? , [ , & , . , ' , ) , % , ^ , + , * etc. I would like to escape all such characters that might cause a regex to fail on this string in Java. Javascript has something like this in one of the posts which goes like this-
return str.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&");
Is there something similar for Java? I'm not sure what should be the character set to escape. Would something like str.replaceAll("[^\u0000-\u00ff]+", " ") do that? (But I'm losing data here if I'm replacing ALL of them with a space, which I want to avoid)
Use this:
String myEscapedString = Pattern.quote(myRawString);