0

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.

Guglix
  • 3
  • 4
  • Although your question is very unclear, I *think* you expect that the argument you pass into `pocketNumber()` will be modified, and that indeed is not true. Google around for java pass-by-value explanation. – President James K. Polk Sep 06 '17 at 14:46
  • What are you trying to do in this method? Just print a value or are you trying to do something else in this method? @Guglix – cunniemm Sep 06 '17 at 14:53
  • @cunniemm I am attempting to use return to modify the value of `int maxquantityPockets` @JamesKPolk I might look into that - I don't know if Java has an alternative method for what I'm trying to do though, and if it can do so in the first place. – Guglix Sep 06 '17 at 15:22

3 Answers3

2

There are a few things that don't make sense. First, you take userQu as an argument to the function, but then reassign userQu to the user input. Either don't take the input in this function or get rid of the argument parameter. Other than that, the code will certainly return an integer, maybe your ide is saying that you are not using what is returned, but an int is definitely returned

As for memory management, everything is taken care of automatically so don't worry about it. you can use your loop just fine and maybe do something like do while userQu is less than 0 or greater than 7. This loop will terminate as soon as you return though and you'll have to initialize userQu before the loop to do this.

Dom
  • 69
  • 4
  • I tried using a separate variable for the argument, but it is still not used. https://pastebin.com/EUWSnn4q What is supposed to happen is that before `Please enter the pocket you want to work with:` is printed, a list of pockets is to be printed. It's not happening since the value of `public int maxquantityPockets;` is unchanged. – Guglix Sep 06 '17 at 14:59
  • I can't access pastebin right now, you could add the code to the question though. And you need to understand the code currently in the question doesn't make sense. You take userQu as an argument but then reassign the value to something unrelated – Dom Sep 06 '17 at 15:05
  • The character limit appears to be too short for me to post the entire thing, so I'll post what I've added/changed: ` public int pocketNumber(int q) {` ` int repeat = 0;` ` do {` ` ....` ` } while (repeat == 1);` ` return q;` The output is still the same. I'm supposed to have the following, which does not print because of the aforementioned `int` variable having its value unchanged: ` Pocket 1` ` Pocket 2` ` Pocket 3` ` .....` – Guglix Sep 06 '17 at 15:11
  • I see in one of your other comments you think return will modify the value. It will not. You should have something like int variable = pocketNumber(6); somewhere else in your code. when you use return it will assign whatever was returned to variable – Dom Sep 06 '17 at 15:26
0

If you want to improve your resource management you should better use a boolean value for repeat, instead of an integer with values 0 and 1.

boolean repeat = false;
while(!repeat){
     //do something
}

And, as Dom already answered, please always remember, that the sequence return value; is passing the value and stopping the procedure.

  • I'm not sure if I understand what you mean by "passing the `value`", and are you suggesting I use the boolean in place of a `do while` function? – Guglix Sep 06 '17 at 15:19
  • No, I suggest using the boolean in place of the int. Like in the Code snippet i posted. ;) And with "passing the `value`" I wanted to explain the expression `return value;`. – chrismoe92 Sep 06 '17 at 15:24
0

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

dmorrow
  • 29
  • 4
  • I attempted using the getter and the setter, and initialising a variable with a value as what @Dom said (`public int maxquantityPockets = 7;`), but the setter ends up with an unused return variable. ` public int getMaxquantityPockets() { return maxquantityPockets; }` ` public int setMaxquantityPockets(int z) { this.maxquantityPockets = z; return z; }` – Guglix Sep 06 '17 at 15:56
  • Your setters should usually be void methods and getters should be of the type that you are returning. You should revise the setter to this: public void setMaxquantityPockets(int z) { this.maxquantityPockets = z; } – dmorrow Sep 06 '17 at 15:58
  • Updated my code (https://pastebin.com/cU18r7Ae, http://simp.ly/p/bV7tNk), but the output remains somewhat the same: http://simp.ly/p/8Vlk9y – Guglix Sep 06 '17 at 16:13
  • I've added some sample content to my answer to hopefully clear up some things. I'm sorry but I don't have any more time to help. Good luck! – dmorrow Sep 06 '17 at 16:29