coding95
coding95

Reputation: 27

Java return multiple strings in one method

I am attempting to write a program which asks users what their pet name is, species, finds out thirst level and gives a response accordingly.

I would appreciate if someone could help me with a problem im having, in each of the 2 methods askpetname and thirstlevel there are 2 strings i want accessible throughout the entire class without using global variables.

Can someone tell me what it is i am doing incorrectly or point me in the right direction.

Also, i understand that my excess use of methods for tedious tasks is bad practice but it helps with memorising syntax.

Thanks.

class dinoo
{
public static void main(String[] p)
{   


    explain();
    output();

    System.exit(0);

}

public static void explain()
{

    print("The following program demonstrates use of user input by       asking for pet name.");

    return;
}

public static String askpetname()
{
    Scanner scanner = new Scanner(System.in);

    print("Name your dinosaur pet!");

    String petname = scanner.nextLine();

    print("Awesome, cool dinosaur name, what species is " + petname+ " ?");

    String petspecies = scanner.nextLine();

    return petname, petspecies;
}

public static int thirstlevel()
{
    Random ran = new Random();

    int thirst = ran.nextInt(11);
    int hunger = ran.nextInt(11);


    return thirst,hunger;
}

public static String anger(int thirst, int hunger)
{
    double angerscore = (thirst+hunger)/2;
    String temper;      

    if(angerscore<=2)
    {
        temper = "Serene";
    }

    else if(3<=angerscore<=6)
    {
        temper= "Grouchy";
    }

    else if(6<angerscore)
    {
        temper = "DANGEROUS";
    }

    return temper;
}


public static String warning()
{
    if (temper.equals("Serene"))
    {
        print("He's looking happy!");
    }
    else if(temper.equals("Grouchy"))
    {
        print("Ahhh hes a bit "+temper+", you better start feeding him before he gets mad!");
    }

    else if(temper.equals("DANGEROUS"))
    {
        print("GET OUT OF THERE, HES " + temper+"!!!. He will have to be put down for everyones safety.");
    }

}
public static void output()
{
    print(askpetname() + "'s, thirst level is "+thirstlevel()+"/10");

    return;
} 



public static String print(String message)
{
    System.out.println(message);

    return message;
}

}

Upvotes: 0

Views: 3501

Answers (2)

a.atlam
a.atlam

Reputation: 742

First, I would recommend shooting through a tutorial first before attempting this, do all the hello worlds covering scope, objects, arrays and functions. Get familiar with Object Oriented Style, although thats not even procedural programming ... nothing returns 2 objects ... always 1 (it could be an array containing many objects, but an array is a single object)

Moving on,although this is terrible coding practice, but its ok for a beginner,since your functions are all static, create a private static variable inside each function and create getter functions

//convert
String petname = scanner.nextLine();
// To this
private static String petname = scanner.nextLine();
// Then add this below it
public static String getPetName()
{
 return petname;
}

and same for every piece of data you need.

Now remove the return statement from all of your functions and declare return type as void

Then call all functions from Main,

askpetname();
thirstlevel();

then print final output (after you have called the functions) as such

System.out.println("Petname: " + getPetname + " ThirstLevel: " + getThirstLevel() + " HungerLevel: " + getHungerLevel);

Upvotes: 1

Hovercraft Full Of Eels
Hovercraft Full Of Eels

Reputation: 285405

That code won't compile since you can't have:

return string1, string2;

or

else if(3<=angerscore<=6)

Instead of trying to return multiple Strings, your best bet is to create a class, say called Pet, one that holds String fields for the pet's name, a Species field for its species, as well as any other fields for hunger, thirst ... that would best encapsulate all the data that makes up one logical "pet" as well as a methods such as getAnger() that returns a value for anger depending on the Pet's state. Then you can create and return a viable Pet object from your creational method.

Also, your code has lots of compilation errors, suggesting that you could improve the way that you create your code. Never try to add new code to "bad" code, to code that won't compile. If possible, use an IDE such as NetBeans, Eclipse, or IntelliJ to help you create your programs. The IDE's will flag you if any of your code contains compilation errors, and then the key is: don't add new code until you've first fixed the existing compilation error. If you can't use an IDE, then you must compile early and often, and do the same thing -- fix all errors before adding new.

Upvotes: 4

Related Questions