Results 1 to 3 of 3
  1. #1
    aeon1613 is offline Member
    Join Date
    Jan 2011
    Posts
    2
    Rep Power
    0

    Default Help displaying an array from a seperate class

    Okay, What I'm trying to do is take data from an array in my main class, and display it in a different class.
    here is my main class:

    Java Code:
    public class CharCreate
    {
    
        public static void main(String args[])throws Exception
        {
            int AddSkillPoints = 5;
            
            int[] dials = new int[5];
            dials[0] = 5;
            dials[1] = 5;
            dials[2] = 5;
            dials[3] = 5;
            dials[4] = 5;
            
            char Input;
            
            System.out.println("-----");
            System.out.println("");
            System.out.println("Press 1 to assign skill points");
            System.out.println("");
            System.out.println("-----");
            
            do{
                Input = (char) System.in.read();
                
                
    
                if(Input == '1')
                {
                    System.out.println("-----");
                    System.out.println("");
                    System.out.println("Press A to add 1 point to Dialouge.     Current Dialouge points: " + dials[0]);
                    System.out.println("");
                    System.out.println("Press B to add 1 point to Intellegince.      Current Intellegince points:" + dials[1]);
                    System.out.println("");
                    System.out.println("Press C to add 1 point to Agility.      Current Agility points: " + dials[2]);
                    System.out.println("");
                    System.out.println("Press D to add 1 point to Luck.     Current Luck points: " + dials[3]);
                    System.out.println("");
                    System.out.println("Press E to add 1 point to Strength.     Current Strength points: " + dials[4]);
                    System.out.println("");
                    System.out.println("Unassigned points remaining: " + AddSkillPoints);
                    System.out.println("");
                    System.out.println("-----");
                }
                
                else if(Input == 'A')
                {
                    dials[0] = dials[0] + 1;
                    AddSkillPoints = AddSkillPoints - 1;
                    
                    System.out.println("-----");
                    System.out.println("");
                    System.out.println("Press A to add 1 point to Dialouge.     Current Dialouge points: " + dials[0]);
                    System.out.println("");
                    System.out.println("Press B to add 1 point to Intellegince.      Current Intellegince points:" + dials[1]);
                    System.out.println("");
                    System.out.println("Press C to add 1 point to Agility.      Current Agility points: " + dials[2]);
                    System.out.println("");
                    System.out.println("Press D to add 1 point to Luck.     Current Luck points: " + dials[3]);
                    System.out.println("");
                    System.out.println("Press E to add 1 point to Strength.     Current Strength points: " + dials[4]);
                    System.out.println("");
                    System.out.println("Unassigned points remaining: " + AddSkillPoints);
                    System.out.println("");
                    System.out.println("-----");
                }
                   
                else if(Input == 'B')
                {
                    dials[1] = dials[1] + 1;
                    AddSkillPoints = AddSkillPoints - 1;
                    
                    System.out.println("-----");
                    System.out.println("");
                    System.out.println("Press A to add 1 point to Dialouge.     Current Dialouge points: " + dials[0]);
                    System.out.println("");
                    System.out.println("Press B to add 1 point to Intellegince.      Current Intellegince points:" + dials[1]);
                    System.out.println("");
                    System.out.println("Press C to add 1 point to Agility.      Current Agility points: " + dials[2]);
                    System.out.println("");
                    System.out.println("Press D to add 1 point to Luck.     Current Luck points: " + dials[3]);
                    System.out.println("");
                    System.out.println("Press E to add 1 point to Strength.     Current Strength points: " + dials[4]);
                    System.out.println("");
                    System.out.println("Unassigned points remaining: " + AddSkillPoints);
                    System.out.println("");
                    System.out.println("-----");
                }
                
                else if(Input == 'C')
                {
                    dials[2] = dials[2] + 1;
                    AddSkillPoints = AddSkillPoints - 1;
                    
                    System.out.println("-----");
                    System.out.println("");
                    System.out.println("Press A to add 1 point to Dialouge.     Current Dialouge points: " + dials[0]);
                    System.out.println("");
                    System.out.println("Press B to add 1 point to Intellegince.      Current Intellegince points:" + dials[1]);
                    System.out.println("");
                    System.out.println("Press C to add 1 point to Agility.      Current Agility points: " + dials[2]);
                    System.out.println("");
                    System.out.println("Press D to add 1 point to Luck.     Current Luck points: " + dials[3]);
                    System.out.println("");
                    System.out.println("Press E to add 1 point to Strength.     Current Strength points: " + dials[4]);
                    System.out.println("");
                    System.out.println("Unassigned points remaining: " + AddSkillPoints);
                    System.out.println("");
                    System.out.println("-----");
                }
                
                else if(Input == 'D')
                {
                    dials[3] = dials[3] + 1;
                    AddSkillPoints = AddSkillPoints - 1;
                    
                    System.out.println("-----");
                    System.out.println("");
                    System.out.println("Press A to add 1 point to Dialouge.     Current Dialouge points: " + dials[0]);
                    System.out.println("");
                    System.out.println("Press B to add 1 point to Intellegince.      Current Intellegince points:" + dials[1]);
                    System.out.println("");
                    System.out.println("Press C to add 1 point to Agility.      Current Agility points: " + dials[2]);
                    System.out.println("");
                    System.out.println("Press D to add 1 point to Luck.     Current Luck points: " + dials[3]);
                    System.out.println("");
                    System.out.println("Press E to add 1 point to Strength.     Current Strength points: " + dials[4]);
                    System.out.println("");
                    System.out.println("Unassigned points remaining: " + AddSkillPoints);
                    System.out.println("");
                    System.out.println("-----");
                }
                
                else if(Input == 'E')
                {
                    dials[4] = dials[4] + 1;
                    AddSkillPoints = AddSkillPoints - 1;
                    System.out.println("-----");
                    System.out.println("");
                    System.out.println("Press A to add 1 point to Dialouge.     Current Dialouge points: " + dials[0]);
                    System.out.println("");
                    System.out.println("Press B to add 1 point to Intellegince.      Current Intellegince points:" + dials[1]);
                    System.out.println("");
                    System.out.println("Press C to add 1 point to Agility.      Current Agility points: " + dials[2]);
                    System.out.println("");
                    System.out.println("Press D to add 1 point to Luck.     Current Luck points: " + dials[3]);
                    System.out.println("");
                    System.out.println("Press E to add 1 point to Strength.     Current Strength points: " + dials[4]);
                    System.out.println("");
                    System.out.println("Unassigned points remaining: " + AddSkillPoints);
                    System.out.println("");
                    System.out.println("-----");
                }
                if(AddSkillPoints == 0)
                {
                    Review Review = new Review();
                }
            }while(Input != '*');
        }
    }

    and the other class

    Java Code:
    public class Review
    {
    
        public static void Review(String args[])throws Exception
        {
            
            char Input;
            
            System.out.println("-----");
            System.out.println("");
            System.out.println("Your Final Skill levels are:");
            System.out.println("");
            System.out.println("Dialouge: " + CharCreate.dials[0]);
            System.out.println("");
            System.out.println("Intellegince: " + CharCreate.dials[1]);
            System.out.println("");
            System.out.println("Agility: " + CharCreate.dials[2]);
            System.out.println("");
            System.out.println("Luck: " + CharCreate.dials[3]);
            System.out.println("");
            System.out.println("Strength: " + CharCreate.dials[4]);
            System.out.println("");
            System.out.println("---");
            System.out.println("");
            System.out.println("Are you happy with these results?");
            System.out.println("");
            System.out.println("A. Yes");
            System.out.println("");
            System.out.println("B. No");
            System.out.println("");
            System.out.println("-----");
            
            do{
                Input = (char)System.in.read();
            
                
                if(Input == 'A')
                {
                    System.out.println("Done");
                }
                else if(Input == 'B')
                {
                    CharCreate CharCreate = new CharCreate();
                }
            }while(Input != '*');
        }
    }
    I want to take the array "dials" from the main class and display it in the "Review" class.
    When I compile "CharCreate.dials[]" " get a message saying "cannont find variable dials"
    What is the correct syntax for this?

  2. #2
    travishein's Avatar
    travishein is offline Senior Member
    Join Date
    Sep 2009
    Location
    Canada
    Posts
    684
    Rep Power
    5

    Default

    declare the variable outside of the main() method,

    Java Code:
    public class CharCreate
    {
    +        public static int[] dials = new int[5];
    
        public static void main(String args[])throws Exception
        {
            int AddSkillPoints = 5;
            
    -        int[] dials = new int[5];
            dials[0] = 5;
            dials[1] = 5;
            dials[2] = 5;

  3. #3
    aeon1613 is offline Member
    Join Date
    Jan 2011
    Posts
    2
    Rep Power
    0

Similar Threads

  1. Displaying an image through an array
    By BariMutation in forum New To Java
    Replies: 0
    Last Post: 12-04-2010, 04:20 AM
  2. Keyboard Input In A Seperate Class?
    By FatalSylence in forum New To Java
    Replies: 6
    Last Post: 10-12-2010, 05:29 AM
  3. Adding a menu from a seperate class to a JFrame
    By minime12358 in forum Advanced Java
    Replies: 2
    Last Post: 07-23-2010, 05:50 PM
  4. Manipulating components of a seperate GUI Class
    By ribbs2521 in forum New To Java
    Replies: 1
    Last Post: 10-20-2009, 10:53 PM
  5. Problems displaying and array of JPanels
    By dousedfirepants in forum New To Java
    Replies: 3
    Last Post: 11-13-2008, 05:47 AM

Posting Permissions

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