I'm following a tutorial which asks me to create this simple calculator class that is supposed to use arguments that I entered > set project configuration > customize > arguments. I entered numbers in the arguments line. When I run the class it gives me the println and sum 0.0. When I run the project it says build successful but gives me nothing. Why doesn't it println and recognize the arguments I entered when I Run Project?
here is the class
public class calc {
public static void main(String[] arguments) {
float sum = 0;
for (String argument : arguments) {
sum = sum + Float.parseFloat(argument);
}
System.out.println("Those numbers add up to " + sum);
}
}
I'm not familiar with the path you entered to write your arguments. Maybe these are different.
You have to right-click on your project and open Properties right at the bottom. A window should open where you can choose Run. There you can see Arguments: followed by a textbox where you can put them. They're space seperated
Just two things about your code:
The name of your class should start with a capital letter.
The line where you actually sum can be written like sum += Float.parseFloat(argument);
EDIT
Okay now I found your way to the arguments and it's the same.
Could you make a screenshot from that window please? Because your code and my arguments work totally fine
Related
This question already has answers here:
Eclipse command line arguments
(4 answers)
Closed 3 years ago.
Trying to run a program that swaps names. The task is simple: input > Alice Bob Alex, output > Alex Bob Alice
P.s. Maybe the problem is stupid, but I just recently started programming, so I don't know what to do
I try to run the code in Eclipse - gives an index error. I start in the console - gives an error of the main name. Tried to pass through the internal debugger in Eclipse - writes that I am using obsolete methods. In the end, nothing is clear./
public class Noob {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.print(args[2]);
System.out.print(" " + args[1]);
System.out.println(" " + args[0]);
}
}
Error message from Eclipse:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 2 out of bounds for length 0
at noob/noob.Noob.main(Noob.java:7)
Console (the file name is Noob.java)
First I compiled src file (> javac Noob.java)
Then I ran it (> java Noob)
Error: Could not find or load main class Noob
Caused by: java.lang.NoClassDefFoundError: noob/Noob (wrong name: Noob)
You need to pass three arguments
This is the steps you need to follow, to passing arguments
1-) Click on Run -> Run Configurations
2-) Click on Arguments tab
3-) In Program Arguments section , Enter your arguments.
4-) Click Apply
You are not passing correctly the values to your program. If you call the program from command line with java myProgram Alice Bob Alex your code should work.
Moreover, you can pass the values to your code through Eclipse directly inside Run Configuration option.
Also, it would be better to not hard code the values. Try this:
for (int i=args.length-1; i>=0; i--) {
System.out.print(args[i] + ' ');
}
System.out.println("");
I am working my way through "Java: A Beginner's Guide", Sixth Edition and I've encountered an area where I'm typing precisely what the book says, but I'm getting a undesired output.
Here's my class:
// Display all command-line information
public class CLDemo {
public static void main(String args[]) {
System.out.println("There are " + args.length + " command-line arguments");
System.out.println("They are: ");
for(int i = 0; i < args.length; i++)
System.out.println("arg[" + i + "]: " + args[i]);
}
}
My console output:
There are 0 command-line arguments
They are:
Desired console output:
There are 3 command-line arguments
There are:
arg[0]: one
arg[1]: two
arg[2]: three
I'm using Eclipse IDE for Java Developers
Version: Kepler Service Release 1
Build id: 20130919-0819
Any thoughts re: why the number of arguments from my code don't match the book's count of arguments would be greatly appreciated.
Update:
The solution to my issue turned out to be very simple. I had been running the sample projects in Eclipse by pressing the "Run" button without specifying arguments, as I've done for the prior 164 pages of the book without issue. The book instructed me to execute the program from the command line as follows:
java CLDemo one two three // where one two three are the arguments passed
Thank you to those who steered me to the solution.
It seems you are running the program from Eclipse where by default zero arguments got passed.
If you want to pass your arguments, you can do it through run -- >run configuration --> arguments tab.
Guide
I am still a Java newbie and I have this code. I don't know how to pass the input file to the code. I am using Eclipse Juno.
public static void main(String[] args) {
In in = new In(args[0]); // input file
int N = in.readInt(); // N-by-N percolation system
// turn on animation mode
StdDraw.show(0);
// repeatedly read in sites to open and draw resulting system
Percolation perc = new Percolation(N);
draw(perc, N);
StdDraw.show(DELAY);
while (!in.isEmpty()) {
int i = in.readInt();
int j = in.readInt();
perc.open(i, j);
draw(perc, N);
StdDraw.show(DELAY);
}
}
Whenever I run it I get this exception:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0 at PercolationVisualizer.main(PercolationVisualizer.java:42)
What might cause this exception? Could you please be patient with me and explain the process of how to call the input file in the code?
Refer to this guide for adding arguments to your program. Alternatively, you could specify the file name directly in the code instead of reading it from the args.
Basically, the guide instructs the user to go to the Run menu, then "Run..." (actually "Run Configurations..." in recent Eclipse versions), select the appropriate run configuration for the desired project, click the Arguments tab, and enter the arguments (such as file name) in the "Program arguments" section, separated by spaces.
For those using IntelliJ you can set the program arguments via Run->Edit Configuration. Look down about the middle of the window to locate the "Program Arguments" field. Then add the path to the test file and save.
I have the following line in a batch file.
java Client "127.0.0.1" 9876
It contains the name of my java class and two arguments. My application requires these arguments to run properly.
Is there any way to pass these arguments when running the application in eclipse? It would make debugging a lot easier. Of course I could resolve the problem by using the values of the arguments in the code but I'm curious.
Instead of just hitting the "Run" icon, select the dropdown box next to it, and choose "Run Configurations". Find your application (or create a Run Configuration for it) and put the command line arguments in the "Arguments" tab. See the docs for more information. It should look like this:
See the run configurations. You can specify arguments. You can even prompt the user for arguments, along with defaults:
${string_prompt:host:127.0.0.1} ${string_prompt:port:9876}
The first prompt is host, with default value 127.0.0.1 filled in. Second pop-up has the prmpt port, with 9876 filled in
Right-click on your project.
Go to Debug As > Debug Configurations or Run As > Run Configurations.
Click the tab that says Arguments.
Enter in your Program Arguments
Click Apply or Debug
Want to add something like, how to add multiple parameters.
Right-click on your project.
Debug > Debug Configurations
Go to Arguments tab.
Enter in your Program Arguments, each separated by a new line. (e.g 3 arguments in attached image)
Click Apply or Debug
Hope it helps.
From "Run" go to debug/run configurations. Click the tab called "Arguments". You can give the program arguments there.
Run configurations > Arguments tab. Just put "127.0.0.1" 9876 in the program arguments.
Run-> Run Configurations->Arguments->Enter your arguments separated by space->Apply->Run
Ensure that the right project name and it's main method are selected under "the Main" tab under run configurations
this work for me, in public static void main method.
public static void main(String argv[]) throws Exception {
int port_com = 2;
boolean debugMode = true;
int socket = 6789;
HasarMain hasarMain = new HasarMain();
// Check if a command line argument exists
if(argv.length != 3){
System.out.println("Missing, Port - socket - debugMode!");
System.exit(0);
}
port_com = Integer.parseInt(argv[0]);
socket = Integer.parseInt(argv[1]);
debugMode = Boolean.parseBoolean(argv[2]);
Run-> Run Configurations->Arguments->Enter your arguments separated by tab->
${string_prompt:argv:"2" "6789" "true"}
I've just started using eclipse and java and I'm not used to either one of them. I wrote a simple helloworld-programm, but the next task (school) was to create a program that takes a userinput (from commandline) and responds with the highest number of two. The code I wrote looks like following:
public class Larger {
/**
* #param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
if(args.length < 2)
{
System.out.print("Too few parameters submitted.");
return;
}
int num1 = Integer.parseInt(args[0]);
int num2 = Integer.parseInt(args[1]);
System.out.print(Math.max(num1, num2));
}
}
It all works well when I hit the "run"-button in eclipse, but later when I browse the source-files and tries to run "java Larger.class 2 4" i get an error from java.exe saying that no class was found.
Any idea what this can be?
When it fails, are you invoking the Java process through Eclipse or the command line? It sounds like you're doing it from the command line. In that case, you don't specify the ".class" portion when invoking your Java program. Try :
java Larger 2 4
The "run" button launch your program with the adequate classpath (the bin folder where the .class is generated)
alt text http://ftp.sumylug.osdn.org.ua/pub/mirrors/eclipse.org/downloads/drops/R-3.2-200606291905/new_noteworthy/images/rt-classpath.png
the java needs to refer to that same bin folder, and use the class name (not the class generated binary)
java -cp bin Larger 2 4
To compile javac Large.java
To run java Larger 2 4