Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 13 of 13

Thread: Help Passing Variables

  1. #1
    Member
    Join Date
    May 2012
    Posts
    36
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Help Passing Variables

    I am running this code below:
    	public static void Check(int masterNumber)
    	{
    	int reverseIt = 0;
     
    		for (int i=0; i<=masterNumber; i++)
    		{
    			int r = masterNumber%10;
    			masterNumber = masterNumber/10;
    			reverseIt = reverseIt * 10 + r;
    			i=0;
    		}
     
    	Finalize(masterNumber, reverseIt);
    	}

    masterNumber is being passed in from a module above, and of course reverse it is being declared here in this module. At the end Finalize is being called and both variables are being passed into this method. The problem I am having is masterNumber is being overwritten in this module, so when it get's passed it is showing a null value. What do I need to change in order that masterNumber keeps it's original value declared in it's module?


  2. #2
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: Help Passing Variables

    Well, first off, I'm not sure why masterNumber would be null. I don't think ints can even be null...

    Second, masterNumber is being changed in your for statement, by the line:
    masterNumber = masterNumber/10;
    The only way to keep the value that was sent to the method is by declaring another local variable, setting its value to masterNumber, and then performing your operations on the new local variable. Since int is just a primitive, saying:
    int var = masterNumber;
    will set var to the current value of masterNumber, opposed to its location in memory, meaning you can make changes to var or masterNumber without effecting the other variable.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  3. #3
    Member
    Join Date
    May 2012
    Posts
    36
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Help Passing Variables

    Quote Originally Posted by aussiemcgr View Post
    int var = masterNumber;
    .
    Such a simple solution! I don't know why I didn't think of that! One more question...I think I have my if...else statement set up improperly. What it is supposed to do is if masterNumber equals reverseIt then display they match. If masterNumber does not equal reverseIt display No Match. But it is displaing they match regardless of the case.

    		if(masterNumber != reverseIt)
     
    			JOptionPane.showMessageDialog(null, "They match");
     
    			else
     
    				JOptionPane.showMessageDialog(null, "No match");

    Does that look set up properly to you?

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Help Passing Variables

    print out the values of both variables before the if test so you can see their values and know why the if statement is doing what it is doing.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Member
    Join Date
    May 2012
    Posts
    36
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Help Passing Variables

    When I print the value of the variables, they display properly (as they should). That's why I was curious if I wrote my If statement wrong.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Help Passing Variables

    If you print the values of the variables, then you should not be surprised by what the if statement does.
    Can you explain? Post the values that are printed and what message was displayed.
    If you don't understand my answer, don't ignore it, ask a question.

  7. The Following User Says Thank You to Norm For This Useful Post:

    jo15765 (May 7th, 2012)

  8. #7
    Member
    Join Date
    May 2012
    Posts
    36
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Help Passing Variables

    I was comparing the wrong 2 variables. Beginner's error. Thank you for the help! (As well as the future tip of printing the variables when results are not looking like I think they should)

  9. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Help Passing Variables

    I use printlns a lot when I'm wondering what the computer is seeing.
    If you don't understand my answer, don't ignore it, ask a question.

  10. #9
    Member
    Join Date
    May 2012
    Posts
    36
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Help Passing Variables

    How would I use that?

  11. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Help Passing Variables

    A sample:
       System.out.println("M @ " + getPos(Mouse) + " cheese @ " + getPos(Cheese));
     
         System.out.println("  after set row=" + oldRow + ", col=" + oldCol + " sqr=" + maze[oldRow][oldCol]);
    Last edited by Norm; May 7th, 2012 at 08:19 PM.
    If you don't understand my answer, don't ignore it, ask a question.

  12. #11
    Member
    Join Date
    May 2012
    Posts
    36
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Help Passing Variables

    Cool I'll make a note of this, and use it in future coding projects!

  13. #12
    Junior Member
    Join Date
    May 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help Passing Variables

    Quote Originally Posted by jo15765 View Post
    if(masterNumber != reverseIt) JOptionPane.showMessageDialog(null, "They match");
    else JOptionPane.showMessageDialog(null, "No match");
    you wrote:

    if masterNumber is not equal to reverseIt then display "They Match"
    else (if masterNumber is equal to reverseIt) display "No Match"

    try:
    if(masterNubmer.equals(reverseIt) JOptionPane.showMessageDialog(null, "They Match!");
    else JOptionPane.showMessageDialog(null, "No Match.");
    Last edited by TheNextBillGates; May 7th, 2012 at 08:19 PM. Reason: fix tags

  14. #13
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Help Passing Variables

    if(masterNubmer.equals(reverseIt)
    @TheNextBillGates
    Do you know if the variables are object references or primitives?
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Passing variables with void methods
    By knightmetal in forum What's Wrong With My Code?
    Replies: 5
    Last Post: March 21st, 2012, 08:46 PM
  2. Differences between local variables and instance variables
    By rob17 in forum Java Theory & Questions
    Replies: 2
    Last Post: March 6th, 2012, 08:34 PM
  3. Instance Variables and local variables difference
    By dcwang3 in forum Java Theory & Questions
    Replies: 3
    Last Post: October 31st, 2011, 06:33 AM
  4. Passing a value to another class
    By Semple in forum What's Wrong With My Code?
    Replies: 1
    Last Post: December 4th, 2010, 05:49 PM
  5. Passing in more than one flag
    By anon181 in forum Loops & Control Statements
    Replies: 3
    Last Post: February 2nd, 2010, 06:37 AM