Passing arrays through different classes

Viewed 74

I am having trouble getting a player input array that first sorts it from least to most then transfers from one class to a class that checks it against a random array. Here's what I have so far. The checker class is only returning 0's and int counter almost always=4. What am I doing wrong? Thank you in advance for the help!

    public static void main(String[] args) 
    {
        int[] player=new int[5]; 
        int[] computer=new int[5];
//creates random numbers to array and asks player if they want to play again
        player_input(player);
        System.out.println(java.util.Arrays.toString(player));
        checker(player,computer,cash,count1,count2,count3,count4,count5);
// calling the functions
        } 

Is it something wrong in my main method?

public static int[] player_input(int player[]) {
        String value;
        int ivalue;
        int count=0;
        int temp;
        for(int i=0;i<=4;i++) {
            count++;
            try{
                value = JOptionPane.showInputDialog(null,
                        "Enter Number "+count+": ","Input Data",JOptionPane.QUESTION_MESSAGE);
                ivalue = Integer.parseInt(value);
                player[i] = ivalue;
            }catch (NumberFormatException ex) {
                //handles non int's
            }
            }
//sorting the array so it can match
            if (ivalue > -1 && ivalue<41) {
                player[i] = Integer.parseInt(value);
                for(int j=i+1;j<=4;j++) {
                    if(player[i]>player[j]) {
                        temp=player[i];
                        player[i]=player[j];
                        player[j]=temp;
                    }
        return player;
    }

Then player input goes to the checker which is only giving out 0's and counter doesn't input the correct amount.

    public static int checker(int[] player, int computer[],int cash,int count1,int count2,int count3,int count4, int count5) {
        int counter=0;
        for (int i=0;i<=4;++i) {
            if(player[i]==computer[i]) {
                System.out.println(player[i]);
                counter++;
            }
        }
        if(counter==0) {
            JOptionPane.showMessageDialog(null,"You won nothing, peasent, your total is: $"+cash,"BAD HUMAN",JOptionPane.INFORMATION_MESSAGE);
        }
        else if(counter==1) {
            count1++;
            cash=cash+100;
            JOptionPane.showMessageDialog(null,"Congratulations! You got: "+counter+" right for $100! Your total is: $"+cash,"GOOD HUMAN",JOptionPane.INFORMATION_MESSAGE);
//... goes to counter==5
        return cash;
    }
2 Answers

Are you assigning the retun/output of the functions?

The functions public static int checker and public static int[] player_input have return types of int and int[] respectively and values are returned on both of the functions.

It would appear that in the main, both functions are called however the values returned aren't used. Should player_input(player); be replaced with player = player_input(player);

Also worth nothing that within player_input the input argument int[] player never gets updated which could also be the issue. Does the line player[i] = ivalue; need adding as the last line in the try scope giving the following

try{
    value = JOptionPane.showInputDialog(null,"Enter Number "+count+": ","Input Data",JOptionPane.QUESTION_MESSAGE);
    ivalue = Integer.parseInt(value);
    player[i] = ivalue;
} catch (NumberFormatException ex) {
    //handles non int's
}

The problem was with sorting the player array and not assigning player in player_input correctly. This is player_input working as intended.

    public static int[] player_input(int player[]) {
        String value;
        int ivalue;
        int count=0;
        for(int i=0;i<=4;i++) {
            count++;
            try{
                value = JOptionPane.showInputDialog(null,
                        "Enter Number "+count+": ","Input Data",JOptionPane.QUESTION_MESSAGE);
                ivalue = Integer.parseInt(value);
            }catch (NumberFormatException ex) {
                //handle exception here
                JOptionPane.showMessageDialog(null,"What are you doing?! Enter a number not words fool!","BAD HUMAN",JOptionPane.INFORMATION_MESSAGE);
                value = JOptionPane.showInputDialog(null,
                        "Enter Number "+count+": ","Input Data",JOptionPane.QUESTION_MESSAGE);
                ivalue = Integer.parseInt(value);
            }
        if (ivalue > -1 && ivalue<41) {
                player[i]=ivalue;
            }
        else {
              JOptionPane.showMessageDialog(null,"Please pick a number between 0-40","BAD HUMAN",JOptionPane.INFORMATION_MESSAGE);
                    value = JOptionPane.showInputDialog(null,
                            "Enter Number "+count+": ","Input Data",JOptionPane.QUESTION_MESSAGE);
                            ivalue = Integer.parseInt(value);
             }
        }
        java.util.Arrays.sort(player);
        return player;
    }
Related