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 2 of 2

Thread: Trouble with moving array through classes

  1. #1
    Junior Member
    Join Date
    Mar 2013
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Trouble with moving array through classes

    This is in my main class and method. I let the user select a character (called a champion), and then ,depending on which character they pick, I want to assign their stats as different values in an array. Each character having different array values imported from a secondary class. I have tried it both declaring the array in the primary class and taking it of either class. I haven't had a lot of progress and wasn't sure where else to look for an answer. Please answer with code, because sometimes I get confused between the different names of things.

     
    if ( champ1 == 0 ){//decides if character has been selected or not
                            System.out.print(
                                    "Pick a character:\n"//outputs menu
                                  + "1) Miss Fortune\n"
                                  + "2) Brand\n"
                                  + "3) Annie\n\n" 
                                  + "Enter your choice: ");
     
                            champ1 = input.nextInt();
                            switch(champ1){
                                case 1: 
                                    obj.missfortune();
                                    break;
                                case 2:
                                    int champarr1[] = new int[7];
                                    obj.annie(champarr1[]);
                                    break;
                                case 3:
                                    int champarr1[] = new int[7];
                                    obj.brand(champarr1[]);
                                    break;
                                default:
                                    System.out.println("Invalide entry, try again.");
                                    break;

    This is my secondary class.

     
    //subclass of reague of regends
     
    package reagueofregends;
     
    public class Characters {
     
        public int[] champ = new int[5];
     
        public int[] missfortune(){
            int[] champ = {1,2,3,4,5,6,7};
            return (champ);
        }
        public void annie(int[] champ){
            int[] champ = {1,2,3,4,5,6,7};
        }
        public void brand(int[] champ){
            int[] champ = {1,2,3,4,5,6,7};
        }
    }
    Last edited by IkeIII; May 6th, 2013 at 05:20 PM. Reason: tried to underline inside code, oops


  2. #2
    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: Trouble with moving array through classes

    Are you getting compiler errors? If so, please copy the full text of the error messages and paste it here.

    The code with underlines is hard to read. Better to wrap the code with code tags.

    NOTE: Java passes args by value. Changing the value of the arg: champ will NOT change the the value of the variable used in the call.
    The contents of the array can be changed (for example: champ[0]) but not the variable: champ
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Trouble with using Enumeration and Inheritance in Abstract Classes
    By ws22 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 9th, 2013, 06:49 PM
  2. Trouble using aggregate classes
    By pf1matt in forum Object Oriented Programming
    Replies: 3
    Last Post: June 22nd, 2012, 11:04 AM
  3. Help With Moving A 2D Array Of Rectangles
    By billg118 in forum Collections and Generics
    Replies: 1
    Last Post: April 22nd, 2012, 02:23 PM
  4. [SOLVED] Trouble accessing variables from other classes
    By Elementality in forum What's Wrong With My Code?
    Replies: 3
    Last Post: February 5th, 2011, 11:50 AM
  5. Problem in navigation in a txt file with specific direction
    By wendybird79 in forum Collections and Generics
    Replies: 1
    Last Post: May 10th, 2009, 07:54 AM

Tags for this Thread