I have written 2 java codes (FullTextIndexer.java and Indexer.java) FullTextIndexer has functions :
public static StringBuilder do_exist = null;
public static String do_exist_str;
public static String a_value = new String("my_string");
public static void main(String[] args){
//... here I find "do_exist" value by using "Indexer.java" and printed it with system.out.println; which is the correct value
do_exist_str = do_exist.toString();
}
public static String get_file()
{
main(args);
return do_exist_str;
}
Then I compiled it and compress it as FullTextIndexer.jar and put it into my WEB-INF/lib folder.
And in my php code :
require_once("java/Java.inc");
$session = java_session();
$t = new Java('java.lang.System');
$t=new Java("web_java.FullTextIndexer");
$gotten = java_values($t->get_file());
$a_value = java_values($t->a_value);
echo $gotten;
echo sizeof($gotten);
echo $gotten[0].$gotten[1];
I expect to get my file_name string however I always get an array like {0=0, 1=0} of size 2.
But it writes $a_value correctly.
I guess it finds its value in main(args) but erase afterwards and cannot return "do_exist_str" correctly.
I cannot find a way to get this string correctly. Any help will be appreciated. Thanks in advance
Related
I use templ4docx/Apache POI (2.0.3/3.17).
There you can set a VariablePatten like this:
private static final String START_PATTERN = "#{";
private static final String END_PATTERN = "}";
...
targetDocx.setVariablePattern(new VariablePattern(START_PATTERN, END_PATTERN));
When i use a placeholder with dots, it´s not working inside Header/Footer. In the Body with dots it works. And Images works too with placeholder and dots inside!
Example in Word-Template:
#{Person.Name} // works in Body NOT in Header/Footer!
#{Person.Name} // works in Body and Header/Footer!
#{Person} // works in Body and Header/Footer!
#{Image.Name} // works in Body and Header/Footer! Here i use ImageVariable instead of Textvariable.
I debug the code an saw the "run.setText()" is called with the right Text but in the final document it´s not.
#Override
public void insert(Insert insert, Variable variable) {
if (!(insert instanceof TextInsert)) {
return;
}
if (!(variable instanceof TextVariable)) {
return;
}
TextInsert textInsert = (TextInsert) insert;
TextVariable textVariable = (TextVariable) variable;
for (XWPFRun run : textInsert.getParagraph().getRuns()) {
String text = run.getText(0);
if (StringUtils.contains(text, textInsert.getKey().getKey())) {
text = StringUtils.replace(text, textVariable.getKey(), textVariable.getValue());
if (text.contains("\n")) {
String[] textLines = text.split("\n");
run.setText(textLines[0], 0);
for (int i = 1; i < textLines.length; i++) {
run.addBreak();
run.setText(textLines[i]);
}
} else {
run.setText(text, 0);
}
}
}
}
Any ideas why it didn´t work with Placeholder "#{Person.Name}" as a TextVariable in Header/Footer? But it works with "#{PersonName}" and ImageVariable "#{Images.Logo1}"???
It looks like Word sometimes separates the placeholders, so you'll only find parts of the placeholder in the runs.
In the class "TextInsertStrategy" i check in the loop of the runs for split placeholders and treat it accordingly. With that I could solve the problem.
I am new to MVC and following this link I have a search page for resulting pdf metadatas by using Solr. My if statement and for loop in html side do not work
Searching.java in models folder:
public class Searching {
public String q;
public String outputTitle;
public String outputAuthor;
public String outputContent;
public String outputPage;
public String outputPath;
}
search function in Application.java:
final static Form<Searching> searchForm = form(Searching.class);
final static List<Searching> searchList = new ArrayList<Searching>();
public static Result search() {
Form<Searching> filledForm = searchForm.bindFromRequest();
Searching searched = filledForm.get();
....(database connection lines)
QueryResponse response = solr.query(query);
SolrDocumentList results = response.getResults();
if(results.isEmpty())
System.out.println("SEARCH NOT FOUND");
else {
for (int i = 0; i < results.size(); ++i) {
searched.outputTitle = (String)results.get(i).getFirstValue("title");
searched.outputAuthor = (String)results.get(i).getFirstValue("author");
searched.outputPage =results.get(i).getFirstValue("pageNumber").toString();
searched.outputContent = (String)results.get(i).getFirstValue("content");
searched.outputPath = (String)results.get(i).getFirstValue("path");
searchList.add(searched);
}
System.out.println("\nresults.getNumFound(): "+ searched.outputFound);
System.out.println("results.size(): "+results.size());
}
return play.mvc.Results.ok(search.render(searched, searchForm, searchList));
}
search.scala.html
#(searched: Searching, searchForm: Form[Searching], searchList: List[Searching])
.. some buttons,a search bar...
#if(searchList.isEmpty()) {
<h1>Error</h1>
} else {
#for(search <- searchList) {
<ul>Title: #search.outputTitle</ul>
<ul>Author: #search.outputAuthor <a href="#search.outputPath" download>Download PDF</a></ul>
<ul>Number of Page(s): #search.outputPage</ul>
}
}
Java code works well. I can see outputs on the terminal, but my html side has problem and it shows one book many times according to size of searchList
I am posting the answer explicitly even though I was able to help the OP in the chat - maybe somebody else is running into such problem but did not check the chat:
The problem is even though you have the line in the for-loop you are still using the same searched variable. What you have to do is to reinitialize the variable when entering the loop. Something like:
for (...) {
searched = new Searching();
searched.outputTitle = (String)results.get(i).getFirstValue("title");
....
searchList.add(searched);
}
This solves the problem with the duplicates and everything is fine now.
I write the code in Java to get the arguments by scanner. I have several classes : ChartToHtml.java, ExecutionProperties.java, ExecutionV2.java, TestCase.java, TestCaseList.java, Section.java and all of them will be called from ImplTest.java.
They are working fine when I execute either from eclipse or command line by scanner. The problem is when I want to execute them via program arguments and pass the arguments in one single line. It considers the input as single String but I have to use a String[] as input for Section class.
Here are my Section class and ImplTest classes
public class Section {
Ini.Section root;
ArrayList<String> StringList = new ArrayList<String>();
ArrayList<TestCase> TCList = new ArrayList<TestCase>();
String sectionSearched;
String section;
String filenameSearched;
public Section (){
}
public Section (String filenameSearched, String sectionSearched) {
this.sectionSearched = sectionSearched;
this.filenameSearched = filenameSearched;
}
public ArrayList<String> findSection(String filename, String... wanted) throws IOException, IOException {
Ini myIni = new Ini(new File (filename));
for (String d : wanted) {
root = myIni.get(d);
for (String name : root.keySet()){
StringList.add(root.get(name));
}
}
return StringList;
}
public ArrayList<TestCase> convertStringToTestCase(ArrayList<String>StringList){
for ( int i = 0; i < StringList.size(); i++) {
String name = StringList.get(i) ;
TestCase tc = new TestCase (name);
TCList.add(tc);
}
return TCList;
}
public String[] getSection(int NumOfSec){
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.println("Input section name:");
section = scanner.nextLine();
for (int i =0; i<NumOfSec; i++){
String token[]= section.split(" ");
System.out.println(Arrays.toString(token));
return token;
}
}
}
}
My Main class
public class ImplTest {
public static void main(String[] args) throws IOException, ConfigurationException, TemplateException {
ExecutionV2 ex = new ExecutionV2();
TestCaseList tc = new TestCaseList();
Section s = new Section();
ChartToHtml chr= new ChartToHtml();
ExecutionProperties ep = new ExecutionProperties();
ImplTest imp = new ImplTest();
String filename = ex.getcfg();
String []sec = ex.getSection();
int it = ex.getIterationMax();
String runTCpath =ex.getRunTCdir();
String dir = chr.getChartDir();
ArrayList<TestCase> TClist = s.convertStringToTestCase(s.findSection(filename, sec));
ex.executeTestCaseList(TClist, it , runTCpath);
ex.getTCAttribute(TClist);
ep.setConfigProperties(tc.getTCPassed(), tc.getTCFailed());
chr.generateHistoryTable();
chr.generateChartAndTableTemplate(tc.getTCPassed(), tc.getTCFailed(),ex.getNameList(), ex.getPassedList().toString(), ex.getItList().toString(),dir);
}
}
Then I modified the main class to pass the arguments via run configuration and pass this single line:
ArrayList<TestCase> TClist = s.convertStringToTestCase(s.findSection(**args[0]**, **args[1]**));
ex.executeTestCaseList(TClist, Integer.parseInt(**args[2]**) , **args[3]**);
ex.getTCAttribute(TClist);
ep.setConfigProperties(tc.getTCPassed(), tc.getTCFailed());
chr.generateHistoryTable();
chr.generateChartAndTableTemplate(tc.getTCPassed(), tc.getTCFailed(),ex.getNameList(), ex.getPassedList().toString(), ex.getItList().toString(), **args[4]**);
and pass this singe line into program arguments
C:\\Users\\syuniyar\\.EasyTest\\4\\ccl\\config\\test2.cfg 12346 5 C:\\EasyTest\\4\\bin\\runTC.exe C:\\Users\\syuniyar\\EclipseWS\Test3\\chart.html
it is working fine. However, when I modify the input from ...12346... to ...12346 12345..., I get such error:
Exception in thread "main" java.io.IOException: Cannot run program "5": CreateProcess error=2, The system cannot find the file specified
I also try with VM arguments but the option of System.getProperty() is only for single string.
I know why I get this error because it reads 12345 as 'it' which is not correct. What I wanna ask :
Is it possible to have an array as single argument in main method?
To directly answer your question, "Is it possible to have an array as [a] single argument in [a] main method?", no. The main method accepts arguments in the form of one array of String objects (usually called "args"). Each of these strings is considered an argument.
When executing the main method from the command line, these arguments come after the program name and are delimited by spaces. They are loaded into an array and passed into the main method.
As mentioned in the comments (esp. #Ismail Kuruca), if it is important to you to pass in several strings as one argument, you can concatenate the strings to make your arguments technically one String, and thereby treated as one argument.
I am new in this Java journey, at College they are asking me to
"Define five String variables in the main method called: shipmentNum, supplierName, revDate, revTime, employeeNum." And assign the following text:99, Costco, 12/15/2011, 10:25 AM, 33."
I have this so far, but is giving an error message: "the local variable shipmentNum is never read", I don't see why am I getting this error message.
package c1;
public class ShipmentApp {
public static void main(String[] args){
String shipmentNum = "99";
String supplierName = "Costco";
String revDate = "12/15/2011";
String revTime = "10:25 AM";
String employeeNum = "33";
System.out.println("99");
System.out.println("Costco");
System.out.println("12/15/2011");
System.out.println("10:25 AM");
System.out.println("33");
}
}
What you are seeing is a compiler warning, not an error. This is basically Java trying to help you find flaws in your code by analyzing what you wrote and detecting common mistakes.
In this case, Java recognized that you assigned values to a bunch of variables, but after that never use those variables again.
You probably want to write out the values of the variables, not the assigned values again.
public static void main(String[] args){
String shipmentNum = "99";
String supplierName = "Costco";
String revDate = "12/15/2011";
String revTime = "10:25 AM";
String employeeNum = "33";
System.out.println(shipmentNum );
System.out.println(supplierName );
System.out.println(revDate );
System.out.println(revTime );
System.out.println(employeeNum );
}
Thats warning not error but try this.
public static void main(String[] args){
String shipmentNum = "99";
String supplierName = "Costco";
String revDate = "12/15/2011";
String revTime = "10:25 AM";
String employeeNum = "33";
System.out.println(shipmentNum);
System.out.println(supplierName);
System.out.println(revDate);
System.out.println(revTime);
System.out.println(employeeNum);
}
or just try:
package c1;
public class ShipmentApp{
public static void main(String[] args){
System.out.println("99");
System.out.println("Costco");
System.out.println("12/15/2011");
System.out.println("10:25 AM");
System.out.println("33");
}
}
What you are receiving are warnings because you are not actually ever reading any of the variables you have declared. You could correct this by passing the variables to println instead of typing out the strings twice.
Being that these are only warnings, they should not affect the ability of the program to compile and/or execute. While you could conceivably ignore such warnings, it's usually wise to at least analyze what they are being caused by.
It's a warning that the java compiler tells you that you defined a variable but never used it.
The purpose of a variable, is that it stores information that will be used at some later point in your code. Java gives you a warning, because if you define a variable but never use it you've likely made a mistake. This is because variables that are never used are basically nonsense, and should never have been declared.
Try these print statements:
System.out.println(shipmentNum);
System.out.println(supplierName);
System.out.println(revDate);
System.out.println(revTime);
System.out.println(employeeNum);
That's is not an Error,It is just a warning shown by the IDE or compiler. There is no issue in this code to compile and run.
You may want to do as follows
String shipmentNum = "99";
String supplierName = "Costco";
String revDate = "12/15/2011";
String revTime = "10:25 AM";
String employeeNum = "33";
System.out.println(shipmentNum);
System.out.println(supplierName);
System.out.println(revDate);
System.out.println(revTime);
System.out.println(employeeNum);
You are writing the text out, not the value of the variables. It isn't an error, just a warning. You should probably change your code to this:
package c1;
public class ShipmentApp{
public static void main(String[] args){
String shipmentNum = "99";
String supplierName = "Costco";
String revDate = "12/15/2011";
String revTime = "10:25 AM";
String employeeNum = "33";
System.out.println(shipmentNum);
System.out.println(supplierName);
System.out.println(revDate);
System.out.println(revTime);
System.out.println(employeeNum);
}
}
this message is just to notice you, you have some variables haven't been used. It is not a error. if you dont want to see this warning or you cannot stand it, you can add #supresswarnings annotation at the beginning of the class. or you just follow others' suggestion to use the variables you have created.
DurationOfRun:5
ThreadSize:10
ExistingRange:1-1000
NewRange:5000-10000
Percentage:55 - AutoRefreshStoreCategories Data:Previous/30,New/70 UserLogged:true/50,false/50 SleepTime:5000 AttributeGet:1,16,10106,10111 AttributeSet:2060/30,10053/27
Percentage:25 - CrossPromoEditItemRule Data:Previous/60,New/40 UserLogged:true/50,false/50 SleepTime:4000 AttributeGet:1,10107 AttributeSet:10108/34,10109/25
Percentage:20 - CrossPromoManageRules Data:Previous/30,New/70 UserLogged:true/50,false/50 SleepTime:2000 AttributeGet:1,10107 AttributeSet:10108/26,10109/21
I am trying to parse above .txt file(first four lines are fixed and last three Lines can increase means it can be more than 3), so for that I wrote the below code and its working but it looks so messy. so Is there any better way to parse the above .txt file and also if we consider performance then which will be best way to parse the above txt file.
private static int noOfThreads;
private static List<Command> commands;
public static int startRange;
public static int endRange;
public static int newStartRange;
public static int newEndRange;
private static BufferedReader br = null;
private static String sCurrentLine = null;
private static List<String> values;
private static String commandName;
private static String percentage;
private static List<String> attributeIDGet;
private static List<String> attributeIDSet;
private static LinkedHashMap<String, Double> dataCriteria;
private static LinkedHashMap<Boolean, Double> userLoggingCriteria;
private static long sleepTimeOfCommand;
private static long durationOfRun;
br = new BufferedReader(new FileReader("S:\\Testing\\PDSTest1.txt"));
values = new ArrayList<String>();
while ((sCurrentLine = br.readLine()) != null) {
if(sCurrentLine.startsWith("DurationOfRun")) {
durationOfRun = Long.parseLong(sCurrentLine.split(":")[1]);
} else if(sCurrentLine.startsWith("ThreadSize")) {
noOfThreads = Integer.parseInt(sCurrentLine.split(":")[1]);
} else if(sCurrentLine.startsWith("ExistingRange")) {
startRange = Integer.parseInt(sCurrentLine.split(":")[1].split("-")[0]);
endRange = Integer.parseInt(sCurrentLine.split(":")[1].split("-")[1]);
} else if(sCurrentLine.startsWith("NewRange")) {
newStartRange = Integer.parseInt(sCurrentLine.split(":")[1].split("-")[0]);
newEndRange = Integer.parseInt(sCurrentLine.split(":")[1].split("-")[1]);
} else {
attributeIDGet = new ArrayList<String>();
attributeIDSet = new ArrayList<String>();
dataCriteria = new LinkedHashMap<String, Double>();
userLoggingCriteria = new LinkedHashMap<Boolean, Double>();
percentage = sCurrentLine.split("-")[0].split(":")[1].trim();
values = Arrays.asList(sCurrentLine.split("-")[1].trim().split("\\s+"));
for(String s : values) {
if(s.startsWith("Data")) {
String[] data = s.split(":")[1].split(",");
for (String n : data) {
dataCriteria.put(n.split("/")[0], Double.parseDouble(n.split("/")[1]));
}
//dataCriteria.put(data.split("/")[0], value)
} else if(s.startsWith("UserLogged")) {
String[] userLogged = s.split(":")[1].split(",");
for (String t : userLogged) {
userLoggingCriteria.put(Boolean.parseBoolean(t.split("/")[0]), Double.parseDouble(t.split("/")[1]));
}
//userLogged = Boolean.parseBoolean(s.split(":")[1]);
} else if(s.startsWith("SleepTime")) {
sleepTimeOfCommand = Long.parseLong(s.split(":")[1]);
} else if(s.startsWith("AttributeGet")) {
String[] strGet = s.split(":")[1].split(",");
for(String q : strGet) attributeIDGet.add(q);
} else if(s.startsWith("AttributeSet:")) {
String[] strSet = s.split(":")[1].split(",");
for(String p : strSet) attributeIDSet.add(p);
} else {
commandName = s;
}
}
Command command = new Command();
command.setName(commandName);
command.setExecutionPercentage(Double.parseDouble(percentage));
command.setAttributeIDGet(attributeIDGet);
command.setAttributeIDSet(attributeIDSet);
command.setDataUsageCriteria(dataCriteria);
command.setUserLoggingCriteria(userLoggingCriteria);
command.setSleepTime(sleepTimeOfCommand);
commands.add(command);
Well, parsers usually are messy once you get down to the lower layers of them :-)
However, one possible improvement, at least in terms of code quality, would be to recognize the fact that your grammar is layered.
By that, I mean every line is an identifying token followed by some properties.
In the case of DurationOfRun, ThreadSize, ExistingRange and NewRange, the properties are relatively simple. Percentage is somewhat more complex but still okay.
I would structure the code as (pseudo-code):
def parseFile (fileHandle):
while (currentLine = fileHandle.getNextLine()) != EOF:
if currentLine.beginsWith ("DurationOfRun:"):
processDurationOfRun (currentLine[14:])
elsif currentLine.beginsWith ("ThreadSize:"):
processThreadSize (currentLine[11:])
elsif currentLine.beginsWith ("ExistingRange:"):
processExistingRange (currentLine[14:])
elsif currentLine.beginsWith ("NewRange:"):
processNewRange (currentLine[9:])
elsif currentLine.beginsWith ("Percentage:"):
processPercentage (currentLine[11:])
else
raise error
Then, in each of those processWhatever() functions, you parse the remainder of the line based on the expected format. That keeps your code small and readable and easily changed in future, without having to navigate a morass :-)
For example, processDurationOfRun() simply gets an integer from the remainder of the line:
def processDurationOfRun (line):
this.durationOfRun = line.parseAsInt()
Similarly, the functions for the two ranges split the string on - and get two integers from the resultant values:
def processExistingRange (line):
values[] = line.split("-")
this.existingRangeStart = values[0].parseAsInt()
this.existingRangeEnd = values[1].parseAsInt()
The processPercentage() function is the tricky one but that is also easily doable if you layer it as well. Assuming those things are always in the same order, it consists of:
an integer;
a literal -;
some sort of textual category; and
a series of key:value pairs.
And even these values within the pairs can be parsed by lower levels, splitting first on commas to get subvalues like Previous/30 and New/70, then splitting each of those subvalues on slashes to get individual items. That way, a logical hierarchy can be reflected in your code.
Unless you're expecting to be parsing this text files many times per second, or unless it's many megabytes in size, I'd be more concerned about the readability and maintainability of your code than the speed of the parsing.
Mostly gone are the days when we need to wring the last ounce of performance from our code but we still have problems in fixing said code in a timely manner when bugs are found or enhancements are desired.
Sometimes it's preferable to optimise for readability.
I would not worry about performance until I was sure there was actually a performance issue. Regarding the rest of the code, if you won't be adding any new line types I would not worry about it. If you do worry about it, however, a factory design pattern can help you separate the selection of the type of processing needed from the actual processing. It makes adding new line types easier without introducing as much opportunity for error.
The younger and more convenient class is Scanner. You just need to modify the delimiter, and get reading of data in the desired format (readInt, readLong) in one go - no need for separate x.parseX - calls.
Second: Split your code into small, reusable pieces. They make the program readable, and you can hide details easily.
Don't hesitate to use a struct-like class for a range, for example. Returning multiple values from a method can be done by these, without boilerplate (getter,setter,ctor).
import java.util.*;
import java.io.*;
public class ReadSampleFile
{
// struct like classes:
class PercentageRow {
public int percentage;
public String name;
public int dataPrevious;
public int dataNew;
public int userLoggedTrue;
public int userLoggedFalse;
public List<Integer> attributeGet;
public List<Integer> attributeSet;
}
class Range {
public int from;
public int to;
}
private int readInt (String name, Scanner sc) {
String s = sc.next ();
if (s.startsWith (name)) {
return sc.nextLong ();
}
else err (name + " expected, found: " + s);
}
private long readLong (String name, Scanner sc) {
String s = sc.next ();
if (s.startsWith (name)) {
return sc.nextInt ();
}
else err (name + " expected, found: " + s);
}
private Range readRange (String name, Scanner sc) {
String s = sc.next ();
if (s.startsWith (name)) {
Range r = new Range ();
r.from = sc.nextInt ();
r.to = sc.nextInt ();
return r;
}
else err (name + " expected, found: " + s);
}
private PercentageLine readPercentageLine (Scanner sc) {
// reuse above methods
PercentageLine percentageLine = new PercentageLine ();
percentageLine.percentage = readInt ("Percentage", sc);
// ...
return percentageLine;
}
public ReadSampleFile () throws FileNotFoundException
{
/* I only read from my sourcefile for convenience.
So I could scroll up to see what's the next entry.
Don't do this at home. :) The dummy later ...
*/
Scanner sc = new Scanner (new File ("./ReadSampleFile.java"));
sc.useDelimiter ("[ \n/,:-]");
// ... is the comment I had to insert.
String dummy = sc.nextLine ();
List <String> values = new ArrayList<String> ();
if (sc.hasNext ()) {
// see how nice the data structure is reflected
// by this code:
long duration = readLong ("DurationOfRun");
int noOfThreads = readInt ("ThreadSize");
Range eRange = readRange ("ExistingRange");
Range nRange = readRange ("NewRange");
List <PercentageRow> percentageRows = new ArrayList <PercentageRow> ();
// including the repetition ...
while (sc.hasNext ()) {
percentageRows.add (readPercentageLine ());
}
}
}
public static void main (String args[]) throws FileNotFoundException
{
new ReadSampleFile ();
}
public static void err (String msg)
{
System.out.println ("Err:\t" + msg);
}
}