get user input through file to java program in eclipseIDE - java

In command prompt
i usually give input to java program as follows
c:/> java Myprogram < in.txt
this in.txt have input for the Myprogram class
but i cannot do the same in eclipse
i have give the parameter in program arguments in run configuration as
< in.txt
no error is coming.
simply the program ask for the input
what should i do.

The < symbol is something understood and interpreted by the command line. You can't use that in Eclipse.
You Have a look at the answer to this question:
Eclipse reading stdin (System.in) from a file

in your main Method, you can get the parameters via the String array the main method has.
public static void main(String [] args){
for(int i = 0; i <args.length; i++)
System.out.println(args[i]);
}

Related

Don't close stdin after initial value was processed in Java

I am making simple console app. My main loop looks like this.
var s = new Scanner(System.in);
while (appShouldRun) {
var action = s.nextLine();
// proccess action
}
It works fine when I run app with empty stdin.
java -jar app.jar
But I want to have some prepared scenarios in file. Run them and then wait for next user input.
Expected behaviour:
java -jar app.jar < scenario.txt
output of commands from file
| <- cursor, waiting for user input
// scenario.txt
cmd1
cmd2
cmd3
.
.
.
Problem is that when I run program with something od std in, it process commands correctly, but then it throws
java.util.NoSuchElementException: No line found
I think it's becouse stdin is closed after all lines from file is processed.
Am I wrong?
How to fix the code to get expected behaviour?
Thanks
When you redirect standard input with <, it is connected to the input file, and not connected at all to the console.
If you want your program to receive input on the console, then you can't redirect stdin. You should take the filename as a command-line argument instead. Open it, read it, close it, and then process stdin.
OR, if you really want to, and you're on a unix/linux/macOS system, you can run it like this, using the cat command to concatenate standard input to something else:
cat scenario.txt - | java -jar app.jar
I have tried do change as little code as possible. This reads the whole file like you wanted and ends without Exception
public class Main {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
boolean appShouldRun = true;
while (appShouldRun) {
String action = s.nextLine();
System.out.println("got: " + action);
appShouldRun = s.hasNext();
}
}
}

How can I pass variables from the command prompt into my java program

Is there a way to read data from the command prompt? I have a java program that relies on 4 input variables from an outside source. These variables are returned to the command prompt after I run a javascript program but i need a way to pass these variables from the command prompt into my java program, any help would be greatly appreciated!
While executing java program pass the parameters and all the parameters should be separated by space.
java programName parameter1 parameter2 parameter3 parameter4
This parameters would be available in your main method argument
public static void main(String[] args){
//This args array would be containing all four values, i.e. its length would be 4 and you easily iterate values.
for(int i=0; i<args.length; i++){
System.out.println("Argument " + i + " is " + args[i]);
}
Follow the link:
Command-Line Arguments - The Java™ Tutorials : https://docs.oracle.com/javase/tutorial/essential/environment/cmdLineArgs.html
shared by #BackSlash.
It has all the content which would help you to clear all your doubts.
The content from the link is quoted below:
Displaying Command-Line Arguments passed by user from command-line to a Java program
The following example displays each of its command-line arguments on a
line by itself:
public class DisplayCommandLineParameters {
public static void main (String[] args) {
for (String s: args) {
System.out.println(s);
}
}
}
To compile the program: From the Command Prompt, navigate to the directory containing your .java file, say C:\test, by typing the cd
command below.
C:\Users\username>cd c:\test
C:\test>
Assuming the file, say DisplayCommandLineParameters.java, is in the
current working directory, type the javac command below to compile it.
C:\test>javac DisplayCommandLineParameters.java
C:\test>
If everything went well, you should see no error messages.
To run the program: The following example shows how a user might run the class.
C:\test>java DisplayCommandLineParameters Hello Java World
Output:
Hello
Java
World
Note that the application displays each word — Hello, Java and World —
on a line by itself. This is because the space character separates
command-line arguments.
To have Hello, Java and World interpreted as a single argument, the
user would join them by enclosing them within quotation marks.
C:\test>java DisplayCommandLineParameters "Hello Java World"
Output: Hello Java World

Command-Line Arguments Being Lost?

I'm writing a Java program to clean some data.
I'm passing it the files I need, but the first file is ignored!
Minimum code to reproduce the issue:
public class Classifier {
public static void main(String[] args) throws IOException {
System.out.println(args[0]);
for (String s : args) {
System.out.println(s);
}
}
}
I'm running it with the following Command-Line Argument:
java Classifier < March.csv February.csv
And the output I'm receiving is:
February.csv February.csv
Can someone explain why this is?
The < March.csv is being interpreted by the shell as an input redirect. The contents of March.csv are sent to your program's standard input, which you are ignoring. This happens in the shell, before your Java program is even started. So, only February.csv is being sent as a command line argument to main.
Remove that <, so that all command-line arguments you intended to send to main are sent.
< operator redirects this file to stdin to this Java process
so
if you just have
java Classifier < March.csv
and try to read arguments, you would see none and if you read stdin you would read the file content

Split rule of args in main function in Java?

Here is a strange problem I ran into:
I create a single program to print all the received args, Here is the code:
public class Test {
public static void main(String[] args) {
for (int i = 0; i < args.length; i ++) {
System.out.println(args[i]);
}
}
}
Then I built a jar file of it and ran the following command:
java -jar test.jar test&1
However, it didn't print "test&1" as expected. The result of it is:
test
'1'is not recognized as an internal or external command,operable program or batch file.
So my question is: what is the seperation of args? If I really need to receive "test&1", what should I do?
Thanks!
It's nothing to do with Java. The & character is special to the Windows shell (I can tell it's Windows from the error message): It separates two commands on one line, so what you're doing is telling the shell to run java -jar test.jar test and then run 1. If you want to pass test&1 to Java, you'll have to put it in quotes:
java -jar test.jar "test&1"
The & is also special on *nix shells (but in a different way, it runs the command in a sub-shell). There, you could use quotes as above, or put an \ before the & instead. But not on Windows.

How to read and write text files from the main in java

The static method main, which receives an array of strings. The array should have two elements: the path where the files are located (at index 0), and the name of the files to process (at index 1). For example, if the name was “Walmart” then the program should use “Walmart.cmd” (from which it will read commands) and “Walmart.pro” (from which it will read/write products).
I don't want anyone to write the code for me because this is something I need to learn. However I've been reading this through and the wording is confusing. If someone could help me understand what it wants from me through pseudo-code or an algorithm it would be greatly appreciated.
Where I'm confused is how to initialize arg[0] and arg[1] and exactly
what they are being initialized to.
The main method's String array input argument consists of whatever String arguments you pass to the program's main method when you run the program. For example, here is a simple program that loops over args and prints a nice message with each argument's index and value on a separate line:
package com.example;
public class MainExample {
public static void main(String[] args) {
for (int i = 0; i < args.length; i++) {
System.out.printf("args[%d]=%s\n", i, args[i]);
}
}
}
Once you've compiled the program, you can run it on the command-line and pass it some arguments:
java -cp . com.example.MainExample eh? be sea 1 2 3 "multiple words"
Output:
args[0]=eh?
args[1]=be
args[2]=sea
args[3]=1
args[4]=2
args[5]=3
args[6]=multiple words
So lets explain to you
Create a class Inventory : if you don't know how to create a class google it just as is
The static method main: Every executable class in java (at least from the console) has the main method you should google java main method and propably in the same place you find it you will see the default arguments that it receives
When you learn about the default arguments of method main you will undertand about the 'args' that has to be on it
You will have t study the class String google it "java String class"
You will have to study the class File google it "java File class"
At the end everything else would be just logic and I beleave you have learned some at this point.
public class Inventory { // class inventory
public static void main(String[] args) // main method
{
if(args.length==2){ // check if args contains two elements
String filePath = args[0];
String fileName = args[1];
filePath+= System.getProperty("file.separator")+fileName;
File fileCMD = new File(filePath+".cmd");
//fileCMD.createNewFile();
File filePRO =new File(filePath+".pro");
//filePRO.createNewFile();
}
else {
//write the code to print the message Usage: java Inventory Incorrect number of parameters for a while and exit the program.
}
}
This is what I've understood. Basically you have to write a program to create two files, one called fileName.cmd and the other fileName.pro. You have to construct the path of the files using the arguments (input parameters of the main method) and system's file separator. If the arguments don't have two elements you have to print the 'invalid' message. That's it.
Where I'm confused is how to initialize arg[0] and arg[1] and exactly
what they are being initialized to.
You have to use command line to pass the arguments and launch the program , something like the following code in cmd or terminal:
java inventory thePath theFileName
That's how it get initialized.

Categories

Resources