This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Assigning values twice (I.E. Swapping values) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Assigning values twice (I.E. Swapping values)" Watch "Assigning values twice (I.E. Swapping values)" New topic
Author

Assigning values twice (I.E. Swapping values)

Rykurex Peters
Greenhorn

Joined: Oct 28, 2012
Posts: 21
Sorry about the double post, I thought everything was working smoothly until I started doing my testing

/* Author: Ryan Peters
Date: 13/12/2012
Description: Sorts post into 1 of 5 categories based on dimensions and weight. The categories are letter, large letter, packet, parcel and neither.
*/



I'm using these two methods to model turning the package around, like if you stand a book up, or let it lie flat.
The sorting method is as follows


So when I enter 100 180 4 25
I receive the message, this item is a large letter... It should swap the length and width (180 100 4 25) and return "This item is a latter."
Any advice would be greatly appreciated.

I've needed a lot of help with this one, I've tried to put a lot of what I've learned into this one program and all the little things have been catching me out so I'd just like to give a huge thanks to everybody who has helped me get this far!
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

You need to TellTheDetails(←click).

  • What exactly are the rules for what you're doing?
  • What exact difficulty are you having?
  • What do the numbers 100 180 4 25 mean with regards to wht you're trying to accomplish and what the rules are?


  • Also, rather than posting disjoint code snippets, if you post an SSCCE(←click), with just enough code to completely demonstrate the problem in one coherent piece, and nothing not directly related to the problem (for instance, hardcode values, rather than taking user input), it will make it easier for folks here to understand what you're trying to do and where you're stuck.
    Jeff Verdegan
    Bartender

    Joined: Jan 03, 2004
    Posts: 6109
        
        6

    Here are a couple of general pieces of advice though:

  • ALWAYS use braces with your if statements, even when they're not necessary.
  • If your code isn't doing what you think it should, add copious println() statements so that you can see what's happening at each step, which code paths are being taken, and what various values are. For example, in your swap methods and your if statements, print out length, width, and thickness, and in the swaps, do it for both the before and after values.
  • Ivan Franko
    Ranch Hand

    Joined: May 30, 2011
    Posts: 44
    When:

    Then will be executed second condition:


    If:

    will be executed first condition:
    Jeff Verdegan
    Bartender

    Joined: Jan 03, 2004
    Posts: 6109
        
        6

    @Ivan Franko: It's good that you want to help, but the preferred approach at this site is to give people a nudge in the right direction so they can discover the solution on their own. That's why I suggested adding the println() calls.

    @Rykurex Peters: Despite the rather more direct answer that Ivan has provided, I strongly encourage you to add the println()s anyway, see for yourself what's happening, and see if you conclusion matches what he told you. This investigative technique is one you'll use constantly throughout your programming career, whether it's just a semester or two academically or a lifetime as a professional.
    Aj Prieto
    Ranch Hand

    Joined: Sep 28, 2012
    Posts: 72

    You're not calling the switchLength or switchThickness methods, which is why it won't switch the values.


    Da mihi sis bubulae frustum assae, solana tuberosa in modo Gallico fricta ac quassum lactatum coagulatum crassum.
    Ivan Franko
    Ranch Hand

    Joined: May 30, 2011
    Posts: 44
    @Jeff Verdegan: ok, I agree with you. It is my mistake, sorry.
    Jeff Verdegan
    Bartender

    Joined: Jan 03, 2004
    Posts: 6109
        
        6

    Aj Prieto wrote:You're not calling the switchLength or switchThickness methods, which is why it won't switch the values.


    Since he just posted a couple of disjointed code snippets, we don't know that for sure. But if that is the problem, then he'll see it when he adds the println() calls I suggested. If he puts them in those switch methods, then when he sees those print statements not showing up, he'll know the methods are not being called.
    Rykurex Peters
    Greenhorn

    Joined: Oct 28, 2012
    Posts: 21
    I won't quote people directly but I've read all of the responses, I guess it's just mate who would hate looking at all of the code, but I see now how relevant it is >.<
    I've added a call to the two switch methods and I will recompile it and update you guys straight away.

    Thanks for the tip on adding braces for if statements no matter what though, I'm sure that will come in handy someday without me even knowing it!

    I've also tried the print statements along the way, I can see how this will be very useful in time to come when I move further in to loops and such




    EDIT: It works !!
    I also had to move the switch methods into the user class to keep them private, but everything is working well now
    I keep on forgetting that I have to call the method in the main method as the method will not just activate by itself >.<
    Jeff Verdegan
    Bartender

    Joined: Jan 03, 2004
    Posts: 6109
        
        6

    Rykurex Peters wrote:I won't quote people directly but I've read all of the responses, I guess it's just mate who would hate looking at all of the code, but I see now how relevant it is >.<


    Note that we don't generally want all the code, and that's not what I was asking for here. Just all the relevant code. Often this means you'll have to create a completely separate program to strip out all the stuff that's not directly related to the problem and hardcode values that would have come from those other parts.

    Glad you got it sorted out though!
     
    GeeCON Prague 2014
     
    subject: Assigning values twice (I.E. Swapping values)