Search code examples
javaclassintellij-ideamethodsreturn

Java - my code doesn't return any non-void values to a class attribute


I'm writing some Java code as a personal project, and have been learning it from the start via an online source (I've had some previous programming experience, but I believe that I'm not very confident in my programming skills). It's run fine so far, except I can't make the code return any non-void value to a class attribute. I am using the IntelliJ IDEA IDE for it, and the methods get highlighted, with the IDE telling me that the "Return value of the method is never used". Below is one of the parts of the code I'm concerned with, as it isn't returning any values within the arguments in the brackets:

public int pocketNumber(int userQu) {

//    do {
        System.out.print("Please select the number of pockets you want to work with: ");
        Scanner qu = new Scanner(System.in);
        userQu = qu.nextInt();

        if (userQu > 7 || userQu < 1) {
            System.out.println("Sorry, that is not a valid number of pockets. ");

        } else {
            System.out.println("You have chosen " + userQu + " pockets to work with. ");
            return userQu;
        //    break;
        } // once this complete, values erased.
//    } while (repeat == 1);

    return userQu;
}

This is within public static class topClothing, and I am attempting to use this within main with a variable from this class: public int maxquantityPockets;, the latter whose value is used by a new instance of an array for its length, in the same class.

I wanted the code to loop, but I found out somewhere that because of Java's automatic memory management system, I believed that using the loop, which prevents userQu from being accessed from the method and the return statement and hence would require me to initialise another int variable with the same value, causes the value to be lost: Deleting a class object in java

As of yet, I don't know how to override this, and I speculated that this was the issue, on how memory management being an issue with this code not returning the value processed within the method.

Here is the full code that I have been working on: https://pastebin.com/0ALgUWuU

Edit: I believe that I might have made some errors under managePockets, by not putting the appropriate code under the else condition. The code is fixed here: https://pastebin.com/cU18r7Ae. This leaves me with how maxquantityPockets remains unmodified.


Solution

  • The issue that you are running into is that in Java, parameters are passed by value, not by reference. So when you change the value and return it in the pocketNumber method it is simply throwing a number back and not doing anything with it. To do what you're looking to do you could do either of the following:

    //I know you don't have a setPockets method, but you would  need one for this route
    shirt.setPockets(shirt.pocketNumber(shirt.getMaxquantityPockets));
    

    Or you could change the method itself to be void instead of returning a value:

    public static class topClothing {
    
        private int numberOfPockets;
    
        public void pocketNumber(int userQu) {
    
            do {
                System.out.print("Please select the number of pockets you want to work with: ");
                Scanner qu = new Scanner(System.in);
                userQu = qu.nextInt();
    
                if (userQu > 7 || userQu < 1) {
                    System.out.println("Sorry, that is not a valid number of pockets. ");
    
                } else {
                    System.out.println("You have chosen " + userQu + " pockets to work with.     ");
                    numberOfPockets = userQu;
            } while ((userQu < 1) || (userQu > 7));
        }
    }
    

    I think a simple example might help us see things more clearly. See the example below:

    public static void main(String[] args) {
        Baseball ball = new BaseBall(8,"green");
        System.out.println("Our ball has " + ball.getStitches() + " stitches and is " + ball.getColor());
    
        ball.setStitches(9);
        ball.setColor("white");
        System.out.println("Our ball has " + ball.getStitches() + " stitches and is " + ball.getColor());
    }
    
    public class Baseball
    {
        private int numberOfStitches = 0;
        private String colorOfBall = "";
    
        //This is a constructor which is used to set initial values of an object when the object is created
        public Baseball(int newNumberOfStitches, String newColor)
        {
            numberOfStitches = newNumberOfStitches;
            colorOfBall = newColor;
        }
    
        //This is a getter that returns an int value.
        public int getStitches()
        {
            return numberOfStitches;
        }
    
        //This is a setter, which is of type void and doesn't return any value
        public void setStitches(int newNumberOfStitches)
        {
            numberOfStitches = newNumberOfStitches;
        }
    
        public String getColor()
        {
            return colorOfBall;
        }
    
        public void setColor(String newColor)
        {
            colorOfBall = newColor;
        }
    }
    

    The output of the above program will read:

    Our ball has 8 stitches and is green

    Our ball has 9 stitches and is white