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

Thread: Issue with calling array

  1. #1
    Junior Member
    Join Date
    Jan 2014
    Location
    Washington DC
    Posts
    27
    Thanks
    11
    Thanked 0 Times in 0 Posts

    Question Issue with calling array

    I am just trying to test this array, for a locker combination program that involves classes...but the array is printing out the whacky numbers for the location. When I try to call the method in the main, it does not work. How do I call a method that exist within a class into the main method?

    public class locker {
     
        public static void main(String[] args) {
            CombinationLock();
            }
        }
     
        class CombinationLock {
     
            CombinationLock() {
                double[] combo = new double[39];
                for(int i = 0; i <= combo.length ; i++){    
                System.out.print(combo);
            }
            }
     
            public void resetDial() {
            }
     
            public void turnLeft() {
            }
     
            public void turnRight() {
            }
     
            public void openLock() {
            }
        }


  2. #2
    Member
    Join Date
    Oct 2013
    Location
    Manila, Philippines
    Posts
    241
    My Mood
    Tired
    Thanks
    6
    Thanked 57 Times in 55 Posts

    Default Re: Issue with calling array

    try this one
    locker lock = new locker();
    Combination combo = locker.new Combination();
    then you can call methods of that class using ( . ) operator
    combo.resetDial();
    it might work.

  3. The Following User Says Thank You to dicdic For This Useful Post:

    javaStooge (February 14th, 2014)

  4. #3
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    3,408
    My Mood
    Mellow
    Thanks
    83
    Thanked 473 Times in 464 Posts

    Default Re: Issue with calling array

    . . . the array is printing out the whacky numbers for the location. When I try to call the method in the main, it does not work . . .
    We get lost in the technical jargon and can't help. You need to explain or provide examples of what "whacky numbers" and "does not work" mean. Post the whacky numbers, post the results of a run that "does not work" and describe what's wrong with it and/or what it should be.

  5. The Following User Says Thank You to GregBrannon For This Useful Post:

    javaStooge (February 14th, 2014)

  6. #4
    Member
    Join Date
    Oct 2013
    Location
    Manila, Philippines
    Posts
    241
    My Mood
    Tired
    Thanks
    6
    Thanked 57 Times in 55 Posts

    Default Re: Issue with calling array

    "whacky numbers"
    I think he's talking about printing the hashcode of the array..
    since he has
    double combo = new double[39]

    and what he prints out is the variable combo without index of element.

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

    javaStooge (February 14th, 2014)

  8. #5
    Member
    Join Date
    Feb 2014
    Location
    India
    Posts
    47
    My Mood
    Bored
    Thanks
    0
    Thanked 7 Times in 7 Posts

    Default Re: Issue with calling array

    but the array is printing out the whacky numbers for the location.
    This might be happening because you are printing the array memory locations(memory addresses) and not the values stored in them. Try try replacing "combo" by "combo[i]" in the for loop.

    When I try to call the method in the main, it does not work. How do I call a method that exist within a class into the main method?
    You have not created a method in the "CombinationLock" class, it is a constructor and you cant directly call a constructor that way just by using the name. Change the name of the method and call it by creating an object of the class "CombinationLock".


    if you have any other questions, post them.

  9. The Following User Says Thank You to ankurt For This Useful Post:

    javaStooge (February 14th, 2014)

  10. #6
    Junior Member
    Join Date
    Jan 2014
    Location
    Washington DC
    Posts
    27
    Thanks
    11
    Thanked 0 Times in 0 Posts

    Default Re: Issue with calling array

    didc and ankurt, glad to hear you guys understood what I was talking about, but I will be more exact so nobody has to guess what I am referring to. Yes, the console was printing out the memory locations (whacky numbers) of the array. I did what you said ankurt and did fix the issue I was having however, I now need to assign the integers 0 - 39 to the array. I know how the long way of assigning them (inputing each number into the array), but is there a short way, that will increment the value.

        public static void main(String[] args) {
            int[] combo = new int[40]/*{0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,+
                    20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39}*/;
                for(int i = 0; i < combo.length ; i++){    
                    combo[i] = i++;
                    System.out.println("Number: " + combo[i]);            
            }
        }

    I have not tried to deal with the constructor yet, since I need to get this straightened out first.

  11. #7
    Member
    Join Date
    Feb 2014
    Location
    India
    Posts
    47
    My Mood
    Bored
    Thanks
    0
    Thanked 7 Times in 7 Posts

    Default Re: Issue with calling array

    In the for loop where you have an incrementer (here "i"). Use this incrementer to put the values.

    for(int i = 0; i < combo.length ; i++){    
                    combo[i] = i;  //or combo[i]=i+1;      by using i++ you are overall incrementing the value of i so the next array slot is not brought in the loop for assigning the values.
                    System.out.println("Number: " + combo[i]);            
            }


    --- Update ---

    I hope this solves the problem..

  12. The Following User Says Thank You to ankurt For This Useful Post:

    javaStooge (February 14th, 2014)

  13. #8
    Junior Member
    Join Date
    Jan 2014
    Location
    Washington DC
    Posts
    27
    Thanks
    11
    Thanked 0 Times in 0 Posts

    Default Re: Issue with calling array

    thanks for the help ankurt. Always something small that I miss. I don't understand why i++ does not assign any value to the array....I understood it to be the same thing as i+1 (bot increment by 1).

    Do you happen to know what dicdic was talking about, it was the second post.

  14. #9
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    3,408
    My Mood
    Mellow
    Thanks
    83
    Thanked 473 Times in 464 Posts

    Default Re: Issue with calling array

    The issue with using i++ in your approach is that the "loop control variable", i, is being modified inside the loop, so that not all values of 'i' will be used - some will be skipped. Try sketching out on paper what's happening in that loop, and the light may come on.

  15. #10
    Junior Member
    Join Date
    Jan 2014
    Location
    Washington DC
    Posts
    27
    Thanks
    11
    Thanked 0 Times in 0 Posts

    Default Re: Issue with calling array

    I have created an array of a specified size (10,000 x 3) and I am now trying to assign locker combinations to the prescribed lockers for students. The first number of array assigns locker # to student and the second number will hold the 3 integers (0-39) for locker combo. I've tried several different options and scanned the book a number of times, but I keep getting an error when I try to assign specific integers to locations within my parallel array.

    public void student(){
            String[] student = new String [10000];
            int[][] combination = new int[10000][3];
            student[100] = "Mickey";
            combination [100][] = {28,17,39}; //this is where i am getting the error, "illegal start to expression, empty statement"
            student[275] = "Donald Duck";
        }

  16. #11
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    3,408
    My Mood
    Mellow
    Thanks
    83
    Thanked 473 Times in 464 Posts

    Default Re: Issue with calling array

    You know I hate parallel arrays, right? You should be creating a Student class and a Locker class, and each Student should have an instance variable locker that describes everything you need to know about a locker, number, combo, etc.

    If you're set on this folly and can't be derailed by sensible counsel, then this is an example of a way to accomplish what you''ve posted. Note also that the variables you've created in the student() method aren't very useful, since they're local to student, but perhaps that was for demonstration:
            String[] student = new String [10000];
            int[][] combination = new int[10000][3];
            int[] combo = { 28, 17, 39 };
            student[100] = "Mickey";
            combination [100] = combo;

  17. The Following User Says Thank You to GregBrannon For This Useful Post:

    javaStooge (February 14th, 2014)

  18. #12
    Junior Member
    Join Date
    Jan 2014
    Location
    Washington DC
    Posts
    27
    Thanks
    11
    Thanked 0 Times in 0 Posts

    Default Re: Issue with calling array

    class > parallel arrays, got it. I will take a look at making a student class. Thanks for help GB.

    Can you tell me what I should put within the object locker? Should I use methods within locker to assign a combo and locker number to the students? If so, how should I record that information? I don't see any other way besides an array to record information.

    *EDIT*
    "The main method will be in a separate class from the Locker and the CombinationLock classes. This main method must complete the following:

    Creates locker number 100 for Mickey Mouse. The combination for this locker is 28, 17, 39. This locker will include 3 books.
    Create locker number 275 for Donald Duck. The combination for this locker is 35, 16, 27. This locker will include 0 books.
    Try to open the locker for Mickey Mouse using 15, 18, 18.
    Add three books to Mickey Mouse’s locker.
    Remove one book from Donald Duck’s locker.
    After all of these actions have been completed, print out the current state of both lockers to the console."

    This is the assignment as stated by the professor. This is the reason I was using the parallel array.

  19. #13
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    3,408
    My Mood
    Mellow
    Thanks
    83
    Thanked 473 Times in 464 Posts

    Default Re: Issue with calling array

    Then you're not reading carefully. The professor clearly said to use classes for Locker and CombinationLock. The third class is a test class with a main() method and is (or could be) hard coded with the given test requirements. There's no need for parallel arrays anywhere in the assignment language you've posted.

    The Locker class has a number, a combination, contents, and a state (open/closed) so has has methods that set/get those attributes, adds and removes books, an instance variable to store books, a collection of some kind. There are also methods to set the combination and to open the locker using the combination. The Locker should have a toString() method that prints out its current state, something like:

    Locker number:
    The locker is: (open/closed)
    The locker's combination is:
    The locker's contents are:

    You might also have a Book class.

  20. #14
    Junior Member
    Join Date
    Jan 2014
    Location
    Washington DC
    Posts
    27
    Thanks
    11
    Thanked 0 Times in 0 Posts

    Default Re: Issue with calling array

      public class StudentLocker {
            StudentLocker(){
                Locker mickey = new Locker("Mickey");
                mickey.setLocker(100);
                //mickey.combo = {28, 17, 39};
                mickey.setBooks(3);
                Locker donald = new Locker("Donald");
                donald.setLocker(275);
                //donald.combo = "35 16 27";
                donald.setBooks(0);
            System.out.println(mickey.getName()); 
            System.out.println(donald.getName());
            }  
     
        }
        public class Locker {                       
            String name;
            int locker;        
            int combo;
            int books;
     
            public Locker(String name) {
                    this.name = name;
            }
            public String getName(){
            return name;
            }
            public void setName(String name){
                this.name = name;
            }
            public int getLocker(){
                return locker;
            }
            public void setLocker(int locker){
                this.locker = locker;           
            }
            public int getCombo(){
                return combo;
            }
            public void setCombo(int combo){
                this.combo = combo;
            }
            public int getBooks(){
                return books;
            }
            public void setBooks(int books){
                this.books = books;
            }

    This is what I have come up with this so far, but when I tried to print the names, nothing kicked back..? Am I not passing Mickey and Donald into the class locker correctly? I'm also not sure how I should store the combination for the locker and my first thought was array, but then I'm reminded that arrays are evil and are a last resort---so there must be another way to store those numbers to be checked when the locker is trying to be accessed. I really hope I am at least on the correct path now.

    Should I create the new classes on separate pages, so they have their own .java? Right now I do not and the entire program is on one page. The reason I ask, is that the program cannot find the symbol "mickey.getName()" in the main method and I wasn't sure if it was bc the Locker class does not have it's own .java...?

  21. #15
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    3,408
    My Mood
    Mellow
    Thanks
    83
    Thanked 473 Times in 464 Posts

    Default Re: Issue with calling array

    I assume you added a main() method somewhere? When I create an instance of StudentLocker with a main() method, the names are printed.

    As for storing the combination, thanks for being sensitive to storing data in an array, but arrays are not evil. Instead, storing data in parallel arrays is not a good way to organize data in an OOP.

    Remember, there's supposed to be another class called "CombinationLock." Each Locker will have an instance variable:

    CombinationLock combinationLock; // or a name of your choice

    The combination is stored in that instance. You could store the 3 numbers of the combination as firstNumber, secondNumber, thirdNumber; x, y, z; rightOne, leftTwo, rightThree; or as elements of an array. As far as I can tell, the assignment doesn't specify, and it won't matter.

  22. #16
    Junior Member
    Join Date
    Jan 2014
    Location
    Washington DC
    Posts
    27
    Thanks
    11
    Thanked 0 Times in 0 Posts

    Default Re: Issue with calling array

    How did you get the names to print? You can see how I tried to do it in the code below.

    Why is it not good practice to use arrays for storing data in an OOP?

    I took your advice and created an instance for the combination to be stored in, but when I tried to do it within CombinationLock, I could no longer use mickey. , since it is associated with Locker. In the code below, I created it within in Locker and it will work (for now), but I will need it to be Combination Lock eventually, in order to open/close the lock, correct?

    * I know I've got a ton of questions...

    public class locker {
     
        Scanner input = new Scanner(System.in);
        int turn;
     
        public static void main(String[] args) {
            System.out.println(mickey.getName() + "is");
        }
     
        public class StudentLocker {
     
            StudentLocker() {
                Locker mickey = new Locker("Mickey");
                mickey.setLocker(100);
                mickey.setFirstNumber(28);
                mickey.setSecondNumber(17);
                mickey.setThirdNumber(39);
                mickey.setBooks(3);
                Locker donald = new Locker("Donald");
                donald.setLocker(275);
                donald.setFirstNumber(35);
                donald.setSecondNumber(16);
                donald.setThirdNumber(27);
                donald.setBooks(0);           
            }
        }
     
        class CombinationLock {
     
            CombinationLock() {
            }
     
            public void resetDial() {
            }
     
            public void turnLeft() {
                while (turn == 2) {
                }
            }
     
            public void turnRight() {
                while (turn == 1 || turn == 3) {
                }
            }
     
            public void openLock() {
                while (true) {
                }
            }
        }
     
        class Locker {
     
            String name;
            int locker;
            int combo;
            int books;
     
            public Locker(String name) {
                this.name = name;
            }
     
            public String getName() {
                return name;
            }
     
            public void setName(String name) {
                this.name = name;
            }
     
            public int getLocker() {
                return locker;
            }
     
            public void setLocker(int locker) {
                this.locker = locker;
            }
     
            public int getCombo() {
                return combo;
            }
     
            public void setCombo(int combo) {
                this.combo = combo;
            }
     
            public int getBooks() {
                return books;
            }
     
            public void setBooks(int books) {
                this.books = books;
            }
     
            locker comboLock = new locker();
                int firstNumber, secondNumber, thirdNumber;    
     
            public int getFirstNumber(){
                return firstNumber;
            }
            public int getSecondNumber(){
                return secondNumber;
            }
            public int getThirdNumber(){
                return thirdNumber;
            }
            public void setFirstNumber(int firstNumber){
                this.firstNumber = firstNumber;
            }
            public void setSecondNumber(int secondNumber){
                this.secondNumber = secondNumber;
            }
            public void setThirdNumber(int thirdNumber){
                this.thirdNumber = thirdNumber;
            }
     
            public void putBookInLocker() {
            }
     
            public void removeBookFromLocker() {
            }
     
            public void openLocker() {
            }
        }
    }

  23. #17
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    3,408
    My Mood
    Mellow
    Thanks
    83
    Thanked 473 Times in 464 Posts

    Default Re: Issue with calling array

    As I said, I added a main() method that created an instance of StudentLocker, like this:
    public class StudentLocker
    {
        StudentLocker()
        {
            Locker mickey = new Locker( "Mickey" );
            mickey.setLocker( 100 );
            //mickey.combo = {28, 17, 39};
            mickey.setBooks( 3 );
            Locker donald = new Locker( "Donald" );
            donald.setLocker( 275 );
            //donald.combo = "35 16 27";
            donald.setBooks( 0 );
            System.out.println( mickey.getName() );
            System.out.println( donald.getName() );
        }
     
        public static void main( String[] args )
        {
            new StudentLocker();
        }
     
    }
    The StudentLocker() constructor prints the names.

    --- Update ---

    I just saw the rest of your latest post . . . must have gotten distracted.

    OOP is using objects to represent, describe, and act on 'things'. Parallel arrays are a primitive form of a database that cannot efficiently be used as objects.

    As for your latest code and use of CombinationLock, not quite. Incorporating the CombinationLock class into the Locker class will look something like this:
    Notes: the instance variable combo is of type CombinationLock, the getCombo() and setCombo() methods deal with CombinationLock objects, not ints. The specifics of the CombinationLock class (constructors, etc.) will likely change some of the placeholder combo methods I've included.
    // a class to represent a locker
    public class Locker
    {
        // instance variables
        String name;
        int lockerNumber;
        CombinationLock combo;
        int books;
     
        // default constructor
        public Locker()
        {
            lockerNumber = 0;
            combo = new CombinationLock();  // this default constructor may not exist
            books = 0;
            name = "";
     
        } // end default constructor
     
        // a constructor that sets the locker owner's name
        public Locker( String name )
        {
            // call the default constructo to initialize instance variables
            this();
     
            this.name = name;
     
        } // end constructor
     
        public CombinationLock getCombo()
        {
            return combo;
        }
     
        public void setCombo( CombinationLock combo )
        {
            this.combo = combo;
        }
    // etc . . .

Similar Threads

  1. Calling up array methods from main
    By my21 in forum Collections and Generics
    Replies: 12
    Last Post: November 6th, 2013, 04:15 AM
  2. [SOLVED] Array Issue
    By Blasfemmy in forum What's Wrong With My Code?
    Replies: 2
    Last Post: January 26th, 2013, 09:26 PM
  3. [SOLVED] Cannot find symbol - Calling method passing array Pets
    By Rilstin81 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: December 11th, 2011, 02:27 PM
  4. Calling a print method from another class (printing array)
    By Kaldanis in forum Object Oriented Programming
    Replies: 7
    Last Post: November 25th, 2011, 12:32 PM
  5. Array issue
    By Mini83 in forum Collections and Generics
    Replies: 5
    Last Post: August 18th, 2011, 09:18 AM