I am currently using Eclipse to code Java with. Recently my lecturer noticed that my print statements in my Java is weird and he does not know a fix to it. It shows System.outprintln() instead of System.out.println(). When I change it to System.out.println() it is underlined with a red line.
Is there a quick fix? He says that I might get marks deducted if I submit my codes in this format without changing it.
Try to use full named class:
java.lang.System.out.println("some value");
If this case would work correctly - you have some mistake with class naming (possibly in manner like in Andy Turner's comment)
PS It would be much easier to assume the reason of problem if you'll provide sketches of your sources.
out is static data member of System class having type of PrintStream.
println() is overloaded method of PrintStream class.
System.outprintln() is compiler error.
Try to compile the class using command prompt using the following command- javac myclass.java and then run it using java myclass.
System.outprintln() should throw a compiler error.
Please note that any IDE is user friendly and helps to write your code faster by providing suggestions but your assignments should not be based completely on them. :)
Related
I just started using IntelliJ and while creating my first class and method I noticed something. When I wrote GomokuClient(4000); to call on a class from a imported library I of course got a error for the code not being complete, so I pressed the little red bulb to see the issue, and I selected the recommended fix. The fix looked like this:
Now I'm wondering, how do I write the portNumber:label in the parameter myself for other methods. Looked really nice and very helpful, and I'd like to know how to do this myself.
thanks beforehand!
The fix done by the IDE was not about setting parameter label, but instead adding the new() for ensuring the constructor call.
What you additionally see there is a feature of IntelliJ to show method's parameter hints/labels for easy understanding (readability) of the code. You can read more about parameter hints here: https://www.jetbrains.com/help/webstorm/viewing-method-parameter-information.html.
I'm having trouble creating the most simple of methods in eclipse, I keep getting an error on the line that the method header is declared it looks like This(error information is in the console) This is just the code that i have to have written for the error to pop-up.) I haven't written code since before winter break so I don't know if I may have messed up my jdk or jre, but in all my past projects the methods work as they should and there are no errors, even if I create a new method.
edit: thanks dimoniy, it's been a long winter.
You cannot declare methods within other methods. Just move your method out of the main()
In Java, I'd like to find a way to allow a program to access its own source code, mainly for debugging and metaprogramming purposes (such as printing a method signature at runtime, or allowing a program to read its own comments, or allowing a Java class to print all methods of a certain type, or allowing a program to generate a new version of its own source code, etc).
Is there any way to allow a Java program to access a copy of its own source code, and read it line-by-line?
//this is the first line of the program
//this method is not implemented
public class inspectSourceCode(){
public static String getLine(int lineNumber){
//get the line of the program's own source code as a string,
//this is not currently implemented
}
//this method is implemented
public static void main(String[] args){
System.out.println(getLine(0));
//should print "//this is the first line of the program",
//if the method getLine works correctly
}
}
You could just directly access the .java file in the code. Just point it to the correct directory and access the file as you would any other.
The program is not running the java file itself, there are compiled files instead that are used at runtime.
I'm trying to set properties of each method
I'd suggest you to use annotations and then get them with Method.getAnnotation
Did you checked ASM? http://asm.ow2.org/
But I think, what you are trying to do is very cpu-expensive.
You COULD theoretically use a decompiler library in your source code to potentially get access to the classes, but keep in mind due to optimization and/or obfuscation etc you might not be able to reliably do a 1-1 translation between bytecode and Java code. Also keep in mind that you don't even necessarily have the line #s available to you if the code was not compiled with debugging information built in.
Can a Java program access its own source code?
In general no. The source code is typically not available on the execution platform.
In the sub-cases where the source code is available, then yes (of course) a program can read it using the standard Java I/O APIs. However, there are no standard APIs that are specific to the task of reading source code.
... mainly for debugging purposes (such as printing a method signature at runtime, or allowing a program to read its own comments, or allowing a Java class to print all methods of a certain type)
There is no technical reason why you could not do those things, but it strikes me that you would have a lot of work to do before such a tool got to the point of being useful. And, frankly, a typical Java IDE's source code debugger does pretty much all of these things already, so I don't really see the point of that effort.
How can I fix this? Eclipse doesn't recognize this function:
listFiles(Filter paramFileFilter)
See these screenshots:
Check the type of FileFilter; chances are that it's not java.io.FileFilter
In such cases always check the import statements for the involved method and arguments. Chances are high you imported some x.y.FileFilter, but wanted a.b.FileFilter. You can most easily do the check by hovering over the identifiers and the method call, where you will see the fully qualified name.
This error happens mostly when using the wrong quick fix when creating those imports in Eclipse, so make sure to select the correct "Import XYZ" quick fix by looking at the package name in braces at the end of the tool tip.
Many years later, but I just hit this same problem myself. My problem was that I created a file filter to use with JFileChooser, and then I tried to use the same filter with File.listFiles. The problem is that there are two different classes both called "FileFilter", or rather, a class and an interface.
JFileChooser uses javax.swing.filechooser.FileFilter. File.listFiles uses java.io.FileFilter.
But fortunately, both require a function with signature public boolean accept(File f). So the solution is easy enough. I change my file filter from "extends javax.swing.filechooser.FileFilter" to "extends javax.swing.filechooser.FileFilter implements java.io.FileFilter". Then it works for both cases.
Suppose I have a class called Foo. This class will be modified by many people, and WILL print information to the console. To this effect, we have the following method:
private void print(String message){ ... }
which prints out to the screen in the format we want.
However, while reviewing code from other devs I see that they constantly call System.out.println(...)
instead, which results in barely-readable printouts.
My question is the following: is it possible to prevent any and every use of System.out.println() in Foo.java? If so, how?
I've tried looking this up, but all I found had to do with inheritance, which is not related to my question.
Thanks a lot!
N.S.
EDIT: I know that whatever I have to do to prevent the use of a method could be removed by a dev, but we have as a policy never to remove code marked //IMPORTANT so it could still be used as a deterrent.
EDIT2: I know I can simply tell the devs not to do it or use code reviews to filter the "errors" out but 1) I'm already doing it and it costs a lot of time and 2) the question is whether this is possible or not, NOT how to deal with my devs.
public methods are just that - public. There is no way to restrict access to them.
This kind of problem is usually "solved" by setting up some code-checker like PMD or checkstyle and integrating them into the continuous integration build. So violations of these stuff will be emailed to someone with a big hammer :-)
Although communicating that developers should not use System.out directly would be preferred, you could set System.out to another PrintStream, then use the alternative PrintStream in the private method. That way, when people use System.out.println they won't output anything but you'll still be able to use the alternative PrintStream... something like they do here: http://halyph.blogspot.com/2011/07/how-to-disable-systemout.html
Pre-commit hooks for your revision control system (SVN, Git, Mercurial) can grep for uses of System.{err,out} and prevent commit if they occur.
http://stuporglue.org/svn-pre-commit-hook-which-can-syntax-check-all-files/ is an example that takes an action for different changed files based on file extension for SVN. You should be able to modify that example to take an example based on some subset of Java files and reject if something like the following is true
egrep -q '\bSystem\.(err|out)\b'
You can redirect System.out calls to a streams that ignores the output or that redirects it to your logging system.
System.setOut(printStream);
You can also kill those using System.out.println in a production environment.
You can replace the OutputStream of System with your own implementation that would either throw an exception, or redirect the call to your own print implementation (which you would need to make public).
No, it's not possible to 100% prevent a class from ever using a specific method in Java.
Having that said...
My suggestion would be to add code analysis to your build process and failing the build on any occurrence of System.out.println. A good place to start if you're interested in going this route would be to check out PMD.
Also... have some constructive discussions with your developers and talk about why they're doing what they're doing. Good luck.