Brady
Brady

Reputation: 29

Passing an int array to a method

I am trying to pass the values of an array to my second method instead of using switch statements but I do not know what I am doing wrong. It keeps telling me that an identifier is expected but I didn't think I had to declare the variables again if I am passing them from one method to the other.

public static int infoObtainer()
{ 
    int g1, g2, ng1, ng2;
    String sgg, sgng, sngg, sngng;
    sgg=JOptionPane.showInputDialog("Enter number of years if you both plead guilty: ");
    g1=Integer.parseInt(sgg);
    sgng=JOptionPane.showInputDialog("Enter number of years if you plead guilty and he pleads innocent: ");
    g2=Integer.parseInt(sgng);
    sngg=JOptionPane.showInputDialog("Enter number of years if you plead innocent and he pleads guilty: ");
    ng1=Integer.parseInt(sngg); 
    sngng=JOptionPane.showInputDialog("Enter numner of years if you both plead innocent: ");
    ng2=Integer.parseInt(sngng);

    int[] jailtime = {g1,g2,ng1,ng2}; 

    return jailtime;
}

public static void decisionMaker(jailtime[])
{
    if (g1<=ng1)
    {
        if (g2<=ng2)
        {
            if (g1<=g2)
            {
                Sytem.out.println(" Plead Guilty");
            }
            else
            {
                System.out.println("Plead Gulity");
            }
        }
        else
            if (g1<=ng2)
            {
                System.out.println("Can't help");
            }
    }
    else
    {
        if (g2<=ng2)
        {
            if (ng1<=g2)
            {
                System.out.println("Can't help");
            }
            else
            {
                System.out.println("Plead Guilty");
            }
        }
        else
            if (ng1<=ng2)
            {
                System.out.println("Can't help");
            }
            else
            {
                System.out.println("Plead Guilty");
            }
    }
}

Upvotes: 1

Views: 84

Answers (2)

Joachim Huet
Joachim Huet

Reputation: 422

Check how a function should be defined in Java here.

And check your method's signature again. You should read some basics tutorials, it is fast and useful

public static void myFunction (Object myObj) {}

Or in your specific case

public static void decisionMaker (int[] jailtime) { ... }

And NB that the modified values of the passed array in the method will also change for your array in main.

Upvotes: 2

Neuron
Neuron

Reputation: 5833

Java arrays like their non-array counterparts need a type definition.

change

public static void decisionMaker( jailtime[])

to

public static void decisionMaker(int[] jailtime)

Upvotes: 3

Related Questions