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?
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 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.
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
This question already has answers here:
The method printf(String, Object[]) in the type PrintStream is not applicable for the arguments (...)
(4 answers)
Closed 5 years ago.
I'm doing a intro to programming course and I'm having issues with my Eclipse that doesn't seem to want to run printf in even its simplest form.
My code is:
package Practice;
import java.io.*;
public class Printf {
public static void main(String[] args) throws IOException
{
int num1 = 54;
int num2 = 65;
int sum = num1 + num2;
System.out.printf("Sum of %d and %d is %d.", num1, num2, sum);
}
}
The error is as saying
The method printf(String, Object[]) in the type PrintStream is not applicable for the arguments (String, int, int, int)
I have the Java compiler compliance level set to 1.8 so it should have no issues as I have read in previous posts. Kepler version of Eclipse has had the Java 8 patch applied (so I could comply with 1.8)
No site I have found has given me any other clues as to what the issue could be?
Turns out the project folders in Eclipse run on their own properties seperate from the Eclipse main settings .. in order for them to compile on version 1.6/1.7/1.8 you have to change their properties seperatly from the main settings...
Basically instead of going to windows > preferances ... to change and update the compiler version you right click on the project folder in Package Explorer > Properties > Java Compiler then check the Enable project specific settings and then Change the settings below to the compiler compliance level of 1.5 or higher in order for this code above to work.
Why it does that i have no idea makes no sence but it at least works now :)
Try using System.out.format("Sum of %d and %d is %d.", num1, num2, sum); instead of printf
right click on project folder -> properties -> java compiler -> mark(Enable project specific settings) -> set compiler compliance level : 1.6 -> enjoy eclipse
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.