I am just trying to create an example program to help me remember how to operate for loops, when I ran it through the compiler. The Compiler said missing return statement. Where do I add it?
Here is the code:
public class LoopExample {
public String bam() {
for (int i = 0; i < 8; i++) {
System.out.println(i);
}
}
}
EDIT
I received an answer, but now the main says 'cannot find symbol'... here is the code for the main:
public class LoopExampleTestDrive {
public static void main(String[] args) {
bam looper = new bam();
System.out.println(looper);
}
}
I would advise you to try to understand how Object Oriented languages work first.
That being said, the main reason why your code doesn't work, is because you try to make an object of the class bam with new bam(). This class unfortunately doesn't exist as it is only a method in a class. My solution would look like:
public class LoopExample {
public void bam() {
for (int i = 0; i < 8; i++) {
System.out.println(i);
}
}
public static void main(String[] args) {
new LoopExample().bam();
}
}
As I said: try to understand object oriented programming first, before trying to continue programming in Java. It is too essential to be able to write working code.
PS: just to be complete, the best way to write what you want to do, would look as follows.
public class LoopExample {
public static void main(String[] args) {
for(int i = 0; i < 8; i++) {
System.out.println(i);
}
}
}
Change the signature of your method. Replace public String bam() with public void bam(). voidmeans that you return nothing instead of a String as before.
For further information see http://www.tutorialspoint.com/java/java_methods.htm
Related
I wonder if there is a way (a gradle script or any script or any other way without an IDE) to remove methods annotated with certain annotations. Example:
class x {
public static void main(String[] args) {
int x = getValue();
System.out.println(x);
}
#RemoveEnabled(id = "getValueMethod1", return = "10")
int getValue() {
return 20;
}
}
Now when I run the script or gradle target, it should remove the getValue() method, and the output code should become:
class x {
public static void main(String[] args) {
int x = 10;
System.out.println(x);
}
}
Is there an existing script or way to achieve this? It might be achievable with grep and String parsing etc., but I'm looking for a cleaner solution which is able to get all methods by an annotation id, and remove them with formatting. I tried searching on Google, Stack Overflow etc., but couldn't find a solution.
I wrote a module to process similiar task.
https://github.com/KnIfER/Metaline
#StripMethods(keys={"ToRemove","AlsoRemove"})
public class YourCLZ{
void ToRemove(){} // will be removed
void AlsoRemove(){} // will be removed
#StripMethods(strip=true)
void AnotherTest(){} // will also be removed
}
in your case
#StripMethods(keys="getValue")
class yourx {
public static void main(String[] args) {
int x = getValue();
System.out.println(x);
}
int getValue() {
return 20;
}
}
will become:
class yourx {
public static void main(String[] args) {
System.out.println(x);
}
}
you will get a compilation error since for now my module simply remove any methods or method body statements that contain the key you specified.
I am working on my final project in my OOP1 class. The language is java.
I'd like to know how I invoke the following method inside my constructor:
public Garden (int size) {
garden=new char[size][size];
this.initializeGarden(garden[][]);
}
private void intializeGarden(char [][]garden) {
for(int i=0;i<garden.length;i++)
for(int j =0;j<garden.length;j++)
garden[i][j]='-';
}
this.initializeGarden(garden[][]); is one of several failed attempts.
I've tried a few variations, and eclipse didn't like any of them.
public class Garden {
char[][] garden;
public Garden (int size) {
garden=new char[size][size];
this.initializeGarden(garden);
}
private void initializeGarden(char [][]garden) {
for(int i=0;i<garden.length;i++)
for(int j =0;j<garden.length;j++)
garden[i][j]='-';
}
public void display(){
for(int i=0;i<garden.length;i++){
for(int j =0;j<garden.length;j++){
System.out.print(garden[i][j]);
}
System.out.println();
}
}
public static void main(String[] args) {
new Garden(20).display();
}
}
Your private method intializeGarden appears to have a typo in it.
So the call would look like intializeGarden(garden)
Simply change
this.initializeGarden(garden[][]);
to
this.initializeGarden(garden);
The above code will pass the garden variable as an argument to the initializeGarden method.
I have the following code:
public class main {
public static void main(String[] args) {
Thready howdy = new Thready();
Thread howYouDo = new Thread(howdy);
howYouDo.start();
}
}
class Thready implements Runnable{
int hi = 1;
public Thready(){}
public void run() {
for(int i = 0; i < 10; i++) {
System.out.println(++hi);
}
}
}
It runs fine in IntelliJ regardless of the #Override annotation. In Eclipse, it forces you to remove the #Override annotation, and it works. In NetBeans, it warns you to add an #Override annotation, and the code runs, but the THREAD IS NEVER STARTED in NetBeans no matter what. Meaning the console does not print out anything.
What is going on?
I am trying to do following in gui class for notifying registred observers.
public class GUI extends javax.swing.JFrame implements Observer {
public notImportantMethod() {
t = new Thread() {
#Override
public void run() {
for (int i = 1; i <= 10; i++) {
myObject.registerObserver(this);
}
}
};
t.start();
}
}
It gives me error: incompatible types: cannot be converted to Observer How can I use this? I know inside of run there is another context but how could I access it?
this now refers a Thread. You should be able to call GUI.this. For more info, see here .
If you're looking for quick and dirty: (this is not good practice)
public notImportantMethod() {
final GUI self = this;
t = new Thread() {
#Override
public void run() {
for (int i = 1; i <= 10; i++) {
myObject.registerObserver(self);
}
}
};
t.start();
}
}
Otherwise I would recommend looking up a tutorial on multi-threading and/or concurrency in java, like this one: http://winterbe.com/posts/2015/04/30/java8-concurrency-tutorial-synchronized-locks-examples/
#Ishnark has answered it correctly. You should be able to access it via GUI.this, that's all that you need to do.
I have a main class in which I am calling java method with #Asynchronous annotation on it. I have imported javaeeAPI6.jar for it but the method runs synchronously. Please tell me what is not right. I have added a long for loop to check whether the code goes into next statement past the asynch asyncrounousCall() call but it hangs on it
import javax.ejb.Asynchronous;
public class TestClass
{
public static void main (String[] args)
{
System.out.println("synch");
createAsyncrounousDocument("", "");
System.out.println("synch");
}
#Asynchronous
public static void asyncrounousCall()
{
for(int i = 0; i < 9999999999L ; i++)
i = i;
System.out.println("asynch");
}
}
Thank you
Aiden