Page 1 of 2 12 LastLast
Results 1 to 20 of 22
Like Tree2Likes

Thread: Arrays and passing values to methods help please.

  1. #1
    toppcon is offline Member
    Join Date
    May 2011
    Posts
    46
    Rep Power
    0

    Default Arrays and passing values to methods help please.

    I have to call 2 methods from the main that loads arrays and prints them. I have tried everything to return the array values from the load method to the print method...nothing I do works. Here is my simple code:
    Java Code:
    import java.util.Arrays;
    
    
    public class CardDeckApp{
    
    
    
    	
    	public static void main(String[] Args){
    		
    		
    		
    		CardsLoad();
    		cardPrint();
    			}
    	
    	
    	public static void CardsLoad(){
    		int[] cardNumber=new int[15];
    			for(int y=1;y<14;y++){
    				cardNumber[y]=y;
    				System.out.print(cardNumber[y]); //just checking if it loaded
    			}
    		
    
    	}
    public static void cardPrint(){
    		int[] cardNumber=new int[15];
    		String[] suits={"H", "D", "C", "S"};
    		for(int p=0;p<4;p++){
    			for(int y=1;y<14;y++){
    				System.out.print(suits[p]+" "+cardNumber[y]+",");
    			}
    		
    
    	}	
    }
    }
    I get the H,C,D,S to print but all "0" on the other array.

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default

    You have declared 2 arrays called cardNumber. Note I said 2. Just because they have the same name does not mean that they are magically connected. If you create the array in one method then you need to pass it to the other method. You can do this by having the load method return the array back to main and then have main pass it as a parameter to the print method. Otherwise (since Java is an OO language) make an object and have the array as an instance variable that is accessible by both methods.

  3. #3
    toppcon is offline Member
    Join Date
    May 2011
    Posts
    46
    Rep Power
    0

    Default

    I was trying to do that, but java didn't like my syntax for the return. I am very new to Arrays keep in mind here. I used return cardNumber; on one try. Also, the last statement you made about make an object and have it accessible by both methods. You mean declare the arrays to be global?

  4. #4
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default

    Quote Originally Posted by toppcon View Post
    I was trying to do that, but java didn't like my syntax for the return.
    Well obviously you did it wrong. Do it right instead. Unless you show your attempts we cannot help you more. Just a thought, did you change the return type of the method?

    Also, the last statement you made about make an object and have it accessible by both methods. You mean declare the arrays to be global?
    Java does not have global variables. It does have instance variables.

  5. #5
    toppcon is offline Member
    Join Date
    May 2011
    Posts
    46
    Rep Power
    0

    Default

    So I created this:
    [Quoute]
    public class CardDeckApp{
    int[] cardNumber = new int[4000];
    String[] suits = new String[4];
    [/Quote]

    and because of static it doesn't work.

  6. #6
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    When posting code, please use code tags

    [code]
    YOUR CODE HERE
    [/code]

    If you have two instance variables and non static methods you can have the methods initialize the instance variables correctly. Once the variables are correctly initialized you can easily print them. Perhaps it's above your skill level, but it would be beneficial to think in object oriented programming(oop). Perhaps created a car class which represents a single card, and a deck class which is a deck of cards will be helpful.

  7. #7
    stchman's Avatar
    stchman is offline Member
    Join Date
    Apr 2011
    Location
    Saint Louis, Missouri
    Posts
    97
    Rep Power
    0

    Default

    You were somewhat close. I took your code and modified it.

    Java Code:
    public class CardDeckApp {
        public static void main(String[] Args) {
            CardDeckApp m = new CardDeckApp();
            
            int[] cards = m.cardsLoad();
            m.cardPrint( cards );
        }
        
        public int[] cardsLoad() {
            int[] cardNumber = new int[ 15 ];
            for( int y = 1; y < 14; y++ ) {
                cardNumber[ y ] = y;
            }
            
            return cardNumber;
        }
        
        public void cardPrint( int[] cardNumber ) {
            String[] suits={ "H", "D", "C", "S" };
            for( int p = 0; p < 4; p++ ) {
                for( int y = 1; y < 14; y++ ) {
                    System.out.println( suits[ p ] + " " + cardNumber[ y ] + "," );
                }
            }    
        }
    }
    First thing I did was instantiate your class CardDeckApp. This allowed me to transition to non-static methods.

    I then made your cardsLoad return an int array back to the int[] cards. I then called your cardsPrint method using the returned array as an argument. No need for an Arrays class.

  8. #8
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default

    @stchman

    Please do not spoonfeed code.

  9. #9
    toppcon is offline Member
    Join Date
    May 2011
    Posts
    46
    Rep Power
    0

    Default

    Thanks much stchman...I'll give it a go.

  10. #10
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default

    Quote Originally Posted by toppcon View Post
    and because of static it doesn't work.
    Probably because you left your methods static. You need to go back and review the subjects of Class, Object, instance vs static.

  11. #11
    stchman's Avatar
    stchman is offline Member
    Join Date
    Apr 2011
    Location
    Saint Louis, Missouri
    Posts
    97
    Rep Power
    0

    Default

    Quote Originally Posted by Junky View Post
    @stchman

    Please do not spoonfeed code.
    I learn by examples. If the OP can see some examples of the correct way to program in Java, he/she will learn. I cannot begin to tell you the hours I spent looking at examples of code and studying the API until the whole concept of Object Oriented Programming finally sunk in.

    SUN/Oracle has many, many examples of source code for a user to look at, are they spoon feeding them?

    Basically demeaning a person is not a good method of teaching.

    The OP did about 80% of the work, I simply nudged them in the right direction.

  12. #12
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    The oracle tutorials are not spoon feeding because they are examples, this is most likely an exercises, and while you may think examples are the best way to learn, many here will disagree with you. Learning by doing is an equally effective (if not better) way to learn, taking the time to re think what you are doing, and what you want to do is invaluable. Spoon-feeding doesn't help, it simply gives someone the oppurtunity to say "screw it, I got the answer from so and so, no one else will know".

    Some good texts involve many exercises to allow you to put the knowledge to use, doing things incorrectly will help re enforce mistakes to avoid, and practices to use. It's also generally accepted around here to not spoonfeed and instead nudge the person in the correct direction. Your spoonfed answer was not a simple nudge, while he may have done a decent amount of work, the op is still (potentially) losing some good learning experiences. This also can lead to him returning later with another question where he repeats the same mistake because he didn't solve this problem himself.
    Last edited by sunde887; 07-28-2011 at 02:56 AM.
    DarrylBurke likes this.

  13. #13
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default

    Firstly your example is not a good one so you are imposing your bad habits onto others.

    Secondly, in my experience people do not learn from spoonfed code. They simply copy and paste the code and hand it in as their own. This is cheating, plagiarism and students can be expelled. Even worse I have witnessed on several occassion where someone copy and pastes spoonfed snippet of code into their project without trying to understand what it does or how it works. Then come back to the forum and whine about the code not working.

  14. #14
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default

    Quote Originally Posted by stchman View Post
    I simply nudged them in the right direction.
    No you didn't. You provided a complete working solution. What I and others try to do is nudge them by getting them to think for themselves and arrive at the solution themselves. That is a far better way to learn.

  15. #15
    stchman's Avatar
    stchman is offline Member
    Join Date
    Apr 2011
    Location
    Saint Louis, Missouri
    Posts
    97
    Rep Power
    0

    Default

    Quote Originally Posted by Junky View Post
    Firstly your example is not a good one so you are imposing your bad habits onto others.
    That is your opinion, and I see no "bad" habits in the code.
    Secondly, in my experience people do not learn from spoonfed code. They simply copy and paste the code and hand it in as their own. This is cheating, plagiarism and students can be expelled. Even worse I have witnessed on several occassion where someone copy and pastes spoonfed snippet of code into their project without trying to understand what it does or how it works. Then come back to the forum and whine about the code not working.
    Maybe I gave the OP too much, but it wasn't like he said "I need someone to write a program that counts cards." and I wrote it for him.

    I just got fed up with so much elitism from people that know how to do something and make people feel like idiots when they don't get it.

  16. #16
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default

    Quote Originally Posted by stchman View Post
    That is your opinion, and I see no "bad" habits in the code.
    Yes my opinion is that you have provided code that is halfway between non-OO and full OO code. Why did you not do as I suggested and make both arrays instance variables? Why did you not create an instance of the class and pass all control to that class instead of retaining control in the main method? Your post in the other thread claims that is what you usually do so why didn't you do that here?

    Maybe I gave the OP too much, but it wasn't like he said "I need someone to write a program that counts cards." and I wrote it for him.
    Ahhhh! So just because the OP did not ask for you to write the code then it is ok to do it for them?

    I just got fed up with so much elitism from people that know how to do something and make people feel like idiots when they don't get it.
    Elitism? Far from it. We just want people to be able to learn for themselves instead of relying on others to do their work. Would you be happy to have a fellow workmate continuously coming to you and asking you to do their work?

  17. #17
    return is offline Member
    Join Date
    Jul 2011
    Posts
    11
    Rep Power
    0

    Default

    I try to learn by reading every post here but all the time some one is arguing. Don't you realize everyone is reading this? If you want to argue do it outside of here. Why you do this? Oh you want moderator status that's why :D Well this won't get you it. Good luck.

    - Rose

  18. #18
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    I can't really speak for junky, but I'd imagine he is less concerned with becoming a moderator, and more interested in ensuring that correct information is being given. I think it's safe to wager that many moderators will also agree that spoon-feeding is not helpful(and is frowned upon).

    I'm here to help people, but not do there work for them. I want to help them learn to solve the problem, not to help them learn to beg for an answer(I'm not accusing the op of this).

    If you are reading the posts here to learn, would you rather the posts be littered with falsities, or contain truths? I expect people to be able to help without handing the code to people. Generally spoon fed code is written poorly(I'm not reflecting on the posters spoonfed code here, just a general observaion).

    That being said, I believe people here should act as a community and catch others mistakes, rather than letting them continue on merrily. Allowing mistakes to be held as truths only hurts the software industry. All criticism should be taken, constructive criticism should be the norm, but people should be able to learn from harsh criticism as well.

    This thread has really gotten off topic, and seeing as the answer has been given to the op, perhaps it should be locked to avoid further squabbling?

  19. #19
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default

    @ Rose
    I'm terribly sorry that your delicate sensitivities have been upset. However, if someone posts bad or incorrect information then more knowledgable people have a repsonsibilty to correct them. Those that are corrected should take that on board on learn to be a better programmer instead of getting their noses out of joint and arguing that they are correct. I myself have posted incorrect information in the past, have accepted the correction gracefully and now I am passing on my increased knowledge to others. If you do not like my attitude then don't stay.

    @stchman

    Not that it is any of your business but the reason I have no friends is that I reject all requests. This is a Java forum not Facebook.
    DarrylBurke likes this.

  20. #20
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Not that it is any of your business but the reason I have no friends is that I reject all requests. This is a Java forum not Facebook.
    So if I sent you a friend request you would deny it?

Page 1 of 2 12 LastLast

Similar Threads

  1. passing arguments to other methods
    By popeyito18 in forum New To Java
    Replies: 2
    Last Post: 07-03-2011, 12:01 AM
  2. generics and passing arrays
    By TopNFalvors in forum New To Java
    Replies: 0
    Last Post: 03-17-2011, 05:19 PM
  3. Passing values between classes problem.
    By alin_ms in forum New To Java
    Replies: 8
    Last Post: 12-12-2008, 07:49 PM
  4. passing dynamically generated values
    By abhiN in forum Advanced Java
    Replies: 1
    Last Post: 01-20-2008, 04:21 AM
  5. Passing the Values
    By Lagarto in forum JDBC
    Replies: 1
    Last Post: 07-16-2007, 04:03 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
  •