I found codes getting input as command line arguments.I am using Eclipse but the example code in Algorithms 4th Edition accepts args[0] and so on.I knew we can input argument in Eclipse.
Edit starts
In Eclipse,I need to go to the Run Configuration in order to enter the argument when there is a line of code like this int T = Integer.parseInt(args[0]);
But it's obvious that if we write the codeint T = scanner.nextInt();
I don't even need to go to Run Configurationto enter the Program Arguments and all I need is just click the run button and input the value in the console of Eclipse.
Edit ends.
So,My question is:
1)What is the function(s) for using command line arguments when we can solve the problem using Scanner class?
Thanks for any explanations!
That is just an another way of doing things. Sometimes command line arguments are useful for quickly checking how your program responds with different inputs. I guess you just started your programming journey and that is why you are asking question like this.
And let me tell you there is a difference between argument and input. Scanner class helps you to take input from the console whereas command line arguments are passed as an argument to your main function.
Here are some advantages of using command line arguments.
You can pass any number of arguments and you do not need to define variables for them.
The next benefit is you can pass any data type with command line and then you can code your functions accordingly.
And the final answer for you, don't think about why this when we already have this. It is programming and you should learn as much as you can. You will find 1000s ways of doing the same thing. So enjoy learning.
When you compile your code, you execute javac MyClass.java
When you run the code, you execute java MyClass.
Well, MyClass.java is a command line argument to the javac command, and MyClass is a command line argument to the java command.
Do you think those commands would be better if they instead stopped and asked for the values they need? Especially considering there are a lot of options that can be set, so should they ask for each option, one at a time, so you'd have to press Enter 20+ times before the compilation started?
That is just 2 examples for command-line arguments, and how useful they can be.
Let's use another example. We want to create a Zip file, which at its simplest need a name for the Zip file, and a list of file names to add to the Zip file.
Using Scanner, that might look like this:
java CreateZip
Enter name of Zip file: foo.zip
Enter name of file to include, blank when done: Hello.txt
Enter name of file to include, blank when done: Yeehaa.txt
Enter name of file to include, blank when done: bar.doc
Enter name of file to include, blank when done: baz.png
Enter name of file to include, blank when done:
Zip file created
Or you could use command-line arguments:
java CreateZip foo.zip Hello.txt Yeehaa.txt bar.doc baz.png
Zip file created
Which approach is better? What if you want to do that from a script? How can the script answer questions?
Related
After making an installer to my app, I can make it associate with a file extension, which is great, but I can't figure out how can I detect the path of the file that my program was opened with.
Any help is appreciated!
For anyone having this problem, the file path is the first argument in getParameters().getRaw()
Javadoc description of the function.
Retrieves a read-only list of the raw arguments. This list may be empty, but is never null. In the case of a standalone application, it is the ordered list of arguments specified on the command line. For named parameters, each <name,value> pair is represented as a single argument of the form: "--name=value".
Thanks, James_D!
I have a java program that is known to be functional. I am trying wrap a bash script around it to pass in each index in the associative array as a parameter. When the java program is run, Maven writes output to the console.
What I want is for bash to wait to see the line "[INFO] BUILD SUCCESS" in the standard output before moving on. Once it has that confirmation that the java process ran successfully, I perform some tasks on the text file the java program created. Only then do I want to go to the next iteration of the loop.
I have an associative array of parameters:
params=([1]cat [2]dog [3]fish)
Loop logic:
for i in "${!params[#]}"
do
mvn exec:java -Dexec.mainClass="com.company.ProgramMainClass" -Dexec.args="$i '2015-11-01'" | /usr/bin/expect "[INFO] BUILD SUCCESS"
mv /tmp/outputfile.csv /path/to/directory/${params[$i]}_outputfile.csv
done
I cannot figure out the syntax to make expect work on standard output. I've read several examples and pawed through the expect manual, but I'm just not understanding how it works.
I feel like I should be able to pipe standard output to expect and have the script wait until expect sees the given string. But it's not working. Any advise? Thanks.
If you want you can use expect like this:
#!/usr/bin/expect
set timeout -1
expect "*\\\[INFO\\\] BUILD SUCCESS*"
The triple backslashes are needed for square bracket protection in Tcl.
A few days back I wrote a Textinterpreter plugin in Eclipse which basically takes a text file and simply printout it's content in the console. It does this by first taking a text file and converts it to a string.
then it makes an Arraylist out of it from which each line is printed out in the console.
List<String> mLines = new LinkedList<String>(Arrays.asList(string)
while(!mLines.isEmpty())) {
String line = mLines.remove(0);
if(line.equals("Stop...")){
debug(DebugAction.Suspend);
}
System.out.println(">>> " + line + " <<<");
}
You can see an if statement in code above which checks whether "Stop..." is written on any line in the text file and if it is then the debug() funtion is called(which suspends running unless the user press resume() button in debugmode.)
Now I want to do the same for .java files. i.e write a Java interpreter plugin which execute a java file normally until it finds "Stop..." written in code.
Any Suggestions?
I don't think you really want to implement a Java interpreter, that'd be a huge project keeping you busy for some years maybe.
The most natural solution for you task might be to scan the Java source file and automatically create breakpoints at each Stop statement. Then run the application in debug mode and you get the desired behaviour. Since you only need the line number for creating the breakpoint you can actually keep reading/scanning files line-by-line.
To get additional statements executed (like calling debug(..)) add your snippet as a breakpoint condition (followed by return true; to tell the debug to stop indeed).
i need some buildit method of any class that can give output me like described in below figure
(in xyz.txt) file . i tried by using my own user define method when i pass the ResultSet object to it. It will going to save all record in txt file using FileWriter but some time order changed or even columns not fit in area etc. so i need java buildit method like SQL Command line Client which give output like below then definately there is java method available for save the output in txt file
in need like this method public void convert(ResultSet rs,String fileoutputname){}
and if rs="select * from employees" output save in fileoutput.txt
If you are looking for the exact output of the mysql command line tool, I would recommend simply executing that command and capturing its output.
Presumably you know the SQL that you want to run, so that can be interpolated into the command you execute (via the --execute option). You can then save the output to a file or perform any other transformation on said output.
Here is a link with some options/examples for running system commands in Java:
http://www.mkyong.com/java/how-to-execute-shell-command-from-java/
I would like to read a file from last line using RandomAccessFile. Is this possible or do I have to use another class?
Beside this file changes during the time so the last line doesn't remain last forever. During the reading another, java program write on it. My question is: the program will see in the same time another java program write on the file, the changes?
Edit
Well suppose I have a server that write its faults in a error log file during it's running.another program reads every line.which should be the best way?
Yes reading a file from the bottom up is possible using RandomAccessFile:
Reading the Last Line of a File in Java through Random Access
as for the other part of your question:
Beside this file changes during the time so the last line doesn't
remain last forever.During the reading another java program write on
it.My question is: the program will see in the same time another java
program write on the file, the changes?
I would propose a SSCCE in which you show what you are trying to accomplish and the problem
EDIT:
As Jon Skeets comment suggests, I found a link to a similar question answered by him: Quickly read the last line of a text file?
EDIT 2:
I think I got your second question, I'm not sure it's possible, as a single file cant be accessed by 2 different streams at the same time, one will just throw an error when trying to open the file. Ypu can however monitor if changes occur after the file has been read using Java.NIO Directory Watcher, Unless I misunderstood you.