Command line arguments not getting counted - java

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

Related

Can't understand where is the problem (Java) [duplicate]

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("");

How to make input ( Scanner ) work on Sublime Text 3 when using JAVA?

I just started learning JAVA and Sublime Text 3 was proposed to me as a great compiler for JAVA code. I downloaded it, started programming and set my build system as JavaC. I wanted to create a quick program adding two numbers given by the user and displaying the result but nothing comes up in the "build" section. Do you have any idea to make that work ?
Here is the code I wanna try:
import java.util.Scanner;
public class Example
{
public static void main(String[] args)
{
int a,b;
Scanner input=new Scanner(System.in);
System.out.println("Enter a number:");
a=input.nextInt();
System.out.println("Enter a number:");
b=input.nextInt();
System.out.println("sum=" + (a+b));
}
}
For future references on how this windows command line or command prompt in windows works.
First check if your computer have the JRE (required to run a Java program) and JDK ( required compile and run Java programs) already install to verify this input the text "java -version" and javac -version into the Command Line. If the Javac is already intalls, however, doesn't show in the command line then you need to follow this sites on how to set up the path depending on your OS.

Run Project - doesn't recognize custom project arguments - Java - Netbeans 8.2

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

java assert not throwing exception [duplicate]

This question already has answers here:
How to enable the Java keyword assert in Eclipse program-wise?
(6 answers)
Closed 8 years ago.
I am checking if number is between 1-10 using assert, but if I enter a number beyond 10 it still gives me the result rather than throwing an exception. What am I doing wrong?
import java.util.Scanner;
public class xina {
public static void main(String[] args) throws Exception {
System.out.println("enter any number");
Scanner input = new Scanner(System.in);
int num = input.nextInt();
assert ( num >= 0 && num <= 10 ) : "bad number: " + num;
System.out.println("You entered " + num);
}
}
Assertions are disabled by default in java. You need to manually enable them by adding -ea to your command-line arguments when you invoke the java compiler. I can't tell you how to do this without knowing what compiler/environment you're using.
Edit:
In eclipse, go to the run menu, and click on run configurations. Select the arguments tab and type -ea into the VM arguments.
Are assertions enabled (-ea flag when running program) ?
By default, they are not enabled by the virtual machine.
This article shows you how to do it in eclipse. Could be helpful for you
Go to Run->run configuration
select java application in left nav pan.
right click and select New.
select Arguments tab
Add -ea in VM arguments.
How to enable the Java keyword assert in Eclipse program-wise?

How to recompile with -Xlint:unchecked?

I keep getting this message in BlueJ/Java.
http://cache.gyazo.com/19c325e77bbc120892d1035dcfda5377.png
I know there are several other questions like this already on StackOverflow, but none of the answers were specific enough for me, a Java noob. For example, one of them said to add something on the javac command line," and I have no idea what is that. So use that information to know how specific you must be with me. Sorry. Thanks!
open up command prompt in the directory with your source files. then type javac -Xlint:unchecked *.java
Please refer to the BlueJ FAQ which has the exact answer.
Edit:
Sorry for the runaround. This is what the FAQ says for Windows. Go to your Bluej installation directory and open lib\bluej.defs file. Then go to the section that says bluej.windows.vm.args and add the value the other user said.
So you have:
bluej.windows.vm.args=-Xlint:unchecked
I would listen to paulsm4's advice and start learning from command-line if you really want to understand java. This is the best I can do.
#viggom555:
Here is a complete command line example:
1) Create the file "ATest.java" (EXAMPLE: notepad ATest.java):
import java.util.*;
public class ATest {
public static void main (String[] args) {
ArrayList<String> test = new ArrayList<String>();
System.out.println ("My array has " + test.size() + " items");
test.add ("abc");
System.out.println ("My array has " + test.size() + " items");
}
}
2) Compile (EXAMPLE: "javac -Xlint:unchecked ATest.java"; you don't really need the "XLint" in this example; I'm just showing you where it would go if you wanted):
C:\temp>javac -Xlint:unchecked ATest.java
3) Run the test program:
C:\temp>java ATest
My array has 0 items
My array has 1 items
I hope that helps .. PSM

Categories