Hey everyone I was programing a small game and I am trying to figure out how to send an int value to a class and return a String from it.
This is my code:

This is my main.java class

public class GamGUI {
    /**
     * @param args
     */
    public static void main(String[] args) {
        int char1;
        String Playername;

        System.out.println("Welcome to Finaly Fantsy XXVIIV");
        System.out.print("Please enter your name: ");
        Scanner scan = new Scanner(System.in);
        Playername = scan.nextLine();

        System.out.println("Please select the Character you want to be");
        System.out.println("Enter 1 for WARRIOR");
        System.out.println("Enter 2 for BLACK MAGE");
        System.out.println("Enter 3 for MONK RANGER");
        char1 = scan.nextInt();
        User.Player(char1);

        System.out.println("Welcome " + Playername + " the " + User.Player(playerChar));

        }
}

This i my User.java class

public class User {

    public static String[] Player(int playerNumber) {

        int playerHealth = 100;
        int playerLevel = 1;
        int playerDamage;
        int playerHit;

        String playerChar;

        switch(playerNumber) {
        case 1: playerChar = "Warrior";
            break;
        case 2: playerChar = "Black Mage";
            break;
        case 3: playerChar = "Monk Ranger";
            break;

        return playerChar;
        }
    }
}

in my main class I am trying to output " System.out.println("Welcome " + Playername + " the " + User.Player(playerChar));"... the playerChar is the string name returned from the User.java.

Recommended Answers

All 6 Replies

and what exactly is your question?
it's clear you need a lot of additonal info on static and instance methods.
actually, what you are asking for.. you're already doing.- (almost) you have declared the return value as an array of Strings.. set it to a simple String.

I did that, I have tried everything I could think off just made it String and still eclipse is giving me errors in the main.java on last system.out.println line.

Okay,in the Player function why do you return String[]?

When you return a String it is just return String. When you return a Char that has more than one characters in then your return the array of characters it's not a array of string.

Also don't make the function static. Create a private variable and then you make that variable static if you want to save that players 'type'. Then also playerChar won't be declared in the scope of the main? If you want to return that string you need to define a getter function to return the String....

*method, not function
also: whether the method is static or not, doesn't change the "it 'll work or not" concept.

My bad getter *method. :)

That is correct what you said - it is more just what I would've done and that is why I suggested it.

I dont have the slightest clue what you are trying to acomplish. That said...

You can do in your Player function:

return playerChar.split("(?!^)");

Then in your main:

String[] PeopleDoSomeWeirdShit=User.Player(1);

And it will work. To access the result:

System.out.println(PeopleDoSomeWeirdShit[0]); //Will print out "W"
System.out.println(PeopleDoSomeWeirdShit[1]); //Will print out "a"
System.out.println(PeopleDoSomeWeirdShit[2]); //Will print out "r"
System.out.println(PeopleDoSomeWeirdShit[3]); //Will print out "r"
System.out.println(PeopleDoSomeWeirdShit[4]); //Will print out "i"
System.out.println(PeopleDoSomeWeirdShit[5]); //Will print out "o"
System.out.println(PeopleDoSomeWeirdShit[6]); //Will print out "r"

For prettier stuff:

for (int i=0;i<PeopleDoSomeWeirdShit.length();i++
{
    System.out.print(PeopleDoSomeWeirdShit[i]); //Will straight out print "Warrior"
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.