ikhebgeenaccount
ikhebgeenaccount

Reputation: 373

Java errors with creating function

I have created a class which should create Swords, but I just get a bunch of errors...

package swords;

public class Sword {

public static void main(String [ ] args){

    public int numberOfSwords=0;

    public static void newSword(String nameSword, int damageSword){
        numberOfSwords++;

    }
  }
}

I want to, when I type newSword(Overpowered Sword, 1000000), increase the int numberOfSwords by one, the actual creation of the sword will come later :D

But I get a lot of errors:

At package swords; - The type java.lang.Object cannot be resolved. It is indirectly referenced from required .class files.

At public class Sword { - Implicit super constructor Object() is undefined for default constructor. Must define an explicit constructor

At public static void main(String [ ] args){ - String cannot be resolved to a type

At public int numberOfSwords=0; - Illegal modifier for parameter numberOfSwords; only final is permitted

And the final error at public static void newSword(String nameSword, int damageSword){ - Multiple markers at this line - Syntax error on token ",", ; expected - Syntax error on token "(", ; expected - void is an invalid type for the variable newSword - String cannot be resolved to a type - Syntax error on token ")", ; expected

I hope you can help me!

EDIT: I'm running Linux Mint, so I dont know if it could be something with my system, since I tried the given code, but get the same errors! I'm investigating now ^ ^

EDIT 2: I just ran a command which told me I didn't have a JDK installed, I think I found the problem :P I'll pay more attention to requirements in the future, sorry...

EDIT 3: Apparently I do have a JDK installed, so I dont know anymore what the problem is... Suggestions are highly appreciated!

Upvotes: 1

Views: 495

Answers (5)

Aluan Haddad
Aluan Haddad

Reputation: 31873

Try this

in Sword.Java

public class Sword{
    public Sword(string name, int damage){
        this.name = name;
        this.damage = damage;
        ++swordsCreated;
    }
    public string getName(){ return name; }
    public int getDamage(){ return damage; }
    
    private string name;
    private int damage;
    
    public static int getCountOfSwordsCreated(){ return countOfSwordsCreated; }
    private static int countOfSwordsCreated;
}

in Game.java

public class Game{
    public static void main(string [] args)
    {
        Sword mySword = new Sword("Overpowered Sword", 1000000);

        System.out.println(mySword.getName());
    }
}

Upvotes: 2

There are mainly 2 problems with your code:

Firstly, you are trying to pass a long value to an integer parameter of your function.

Next, numberOfSwords is declared and initialized in main() method, so it's a local variable. You need to make it global in order to access it from your newSword() method.

The following code snippet works perfectly in my NetBeans IDE 7.3. I think this is going to work for you.

Happy coding!!! :) :)

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package sword;

/**
*
* @author Jack Sparrow
*/
public class Sword {

/**
 * @param args the command line arguments
 * 
 */

public static void main(String[] args) {
    // TODO code application logic here
     newSword("Overpowered Sword",1000000); 

}

public static void newSword(String nameSword, long damageSword)
    {
        int numberOfSwords = 0;
        if ((nameSword == ("Overpowered Sword")) && (damageSword == 1000000))
        {
            numberOfSwords++;
            System.out.println(numberOfSwords);
        }
        else
        {
            System.out.println("Error!");
        }
    }
}

Upvotes: 0

user2481237
user2481237

Reputation: 309

Make sure your JAVA_HOME/JDK refers to valid path. When valid java not found than it gives java.lang related errors.

Upvotes: 0

user2672373
user2672373

Reputation:

Perhaps this would help.

package swords;

class Sword {
    private static int numberOfSwords = 0;

    public static void main(String[] args) {
        newSword("Overpowered Sword", 100);
    }

    public static void newSword(String nameSword, int damageSword) {
        numberOfSwords++;
    }
}

Note:

It is a good practice to keep variables private and functions public. So that the public functions are the only possible way to access the variables, ensuring security.

Ensure that you save the code as Sword.java in a folder swords

Upvotes: 0

MariuszS
MariuszS

Reputation: 31595

Im not sure what you want, something like this?

package swords;

public class Sword {

    public static int numberOfSwords = 0;

    public static void main(String[] args) {
        newSword("Overpowered Sword", 1000000);
    }

    public static void newSword(String nameSword, int damageSword) {
        numberOfSwords++;
    }

}

Upvotes: 1

Related Questions