Results 1 to 3 of 3
  1. #1
    2potatocakes is offline Member
    Join Date
    Sep 2008
    Rep Power

    Default Help with Array output Needed!

    Hi guys,

    This problem has been driving me nuts for a couple of days now, thought I'd post it up and see if you whizzes can tell me what I'm doing wrong?

    My problem is that I am passing an array to my method and when I output the array the first time it outputs as normal but then if I pass that array to another method and then output its value again the original value of the array has changed and it's not supposed to..? Anyway's here the gist of the code:

    public void trachPrimeFactor (byte [] numToFactor)
    while ((record = br.readLine()) != null)
    lineToByte = convertStringToByte(record);
    outputArray(numToFactor); <-- works fine here
    outputArray(trachModulus(numToFactor, lineToByte));
    outputArray(numToFactor); <-- output is wrong here

    Output looks something like this:


    0 <--this output is correct, basically I'm just asking 83720%2 at the moment

    00000 <-- but I want this to say 83720 as it did before

    I thought at first that perhaps the method trachModulus might be changing the array within the method so I just tried:

    public byte [] trachModulus (byte[] origNumSent, byte[] divideByThis)
    byte [] origNum = origNumSent;

    and just used origNum within trachModulus but the result is the same, the original array passed to trachModulus changes.

    Can any of you guys see what I'm doing wrong??

    Really appreciate any help you can give guys!

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Blog Entries
    Rep Power


    your assignment statement:
    Java Code:
    byte [] origNum = origNumSent;
    does not copy the array but points the origNum ref variable to the very same object. You should look at using the arrayCopy(...) method instead.

    Also, when posting code here, please use code tags so that your code will retain its formatting and thus will be readable -- after all, your goal is to get as many people to read your post and understand your code as possible, right?

    To do this, highlight your pasted code (please be sure that it is already formatted when you paste it into the forum; the code tags don't magically format unformatted code) and then press the code button, and your code will have tags.

    Another way to do this is to manually place the tags into your code by placing the tag [cod&#101;] above your pasted code and the tag [/cod&#101;] below your pasted code like so:

    Java Code:
      // your code goes here
      // notice how the top and bottom tags are different

  3. #3
    angryboy's Avatar
    angryboy is offline Senior Member
    Join Date
    Jan 2009
    Rep Power

Similar Threads

  1. Java, output string, getting correct output? HELP!
    By computerboyo in forum New To Java
    Replies: 2
    Last Post: 02-25-2009, 11:44 PM
  2. output from an array
    By in forum New To Java
    Replies: 5
    Last Post: 08-15-2008, 08:26 AM
  3. JSP to output Java String Array
    By irenavassilia in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 03-31-2008, 04:11 PM
  4. Array problem.. help needed please!
    By SCS17 in forum New To Java
    Replies: 3
    Last Post: 03-06-2008, 10:30 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts