Results 1 to 6 of 6
  1. #1
    Sutured is offline Member
    Join Date
    Mar 2012
    Posts
    30
    Rep Power
    0

    Default Really new to Java. Array question

    Hello. My name is John. I was 'programming' when I was 10-15 years old at QBasic and now that I got into again I am trying to learn java. The reason is that I just got a really nice board game and thought of having it at a pc application.

    First one goes like this.

    I want to send an array to another class. There I will add 1 to all values and return the result to main.


    Java Code:
    import java.util.*;
    
    public class Start {
    	public static void main(String args[]){
    		Scanner input=new Scanner(System.in);
    		
    		
    		int[] deck={1,2,9,4,-8};
    		
    		
    		for (int counter=0 ; counter <= deck.length-1 ; counter = counter + 1){
    			System.out.println("Spot " + counter + " = " + deck[counter]);
    		}
    		
    		ClassA ClassA = new ClassA();
    		
    		deck = Plus.ClassA(deck);
    		
    		for (int counter=0 ; counter <= deck.length-1 ; counter = counter + 1){
    			System.out.println("Spot " + counter + " = " + deck[counter]);
    		}
    		
    		
    	}
    
    }

    and the class is:


    Java Code:
    public class ClassA {
    	
    	public int[] Plus(int[] cards){
    		
    		for (int count = 0 ; count <= cards.length-1 ; count += 1) {
    			
    			cards[count] = cards[count] + 1;
    		}
    		
    
    		return cards;
    
    		
    	}
    	
    	
    	
    	
    	
    }

    Eclipse pops up error at line 17 at main

    deck = Plus.ClassA(deck);

    But there might be errors in ClassA of course.

    OOP is hard to grasp at this point, coming from Qbasic.

    Thank you people.

    I hope you can handle real noobish questions from me.
    Last edited by Fubarable; 03-09-2012 at 04:51 PM. Reason: code tags added

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Really new to Java. Array question

    First off, when asking about error messages, it's always a good idea to post the error message itself and notify us which line is causing it (you've done the latter). Next, you should go through some Java tutorials from the beginning to learn how to code Java, and more importantly, to (sort of) unlearn what you've learned before from coding QBasic.

    But the main problem I see is here:

    Java Code:
    		
    		ClassA ClassA = new ClassA(); // (A)
    		
    		deck = Plus.ClassA(deck); // (B)
    On line (A) you're creating a new ClassA object but giving it the exact same name, capitalization and all. To avoid confusing us and the compiler, don't do this. All variable names should begin with a lower-case letter, so change the variable name to classA.

    On line (B) you're calling the method backwards. It should be classA.plus(); (note that plus should also begin with a lower case letter).

    Luck.

  3. #3
    Sutured is offline Member
    Join Date
    Mar 2012
    Posts
    30
    Rep Power
    0

    Default Re: Really new to Java. Array question

    Yes Fubarable.

    Thank you for the corrections. How stupid of me to call the method backwards.

    I changed the case letters at my program also and I will always use them like this from now on. I edited the previous post and saw how you coded the program as well, for my next posts.

    I am 32 and I used a lot of easy and small QBasic programs to my university also. I want to know if you can 'teach an old dog new things' by learning OOP and Java.

    I don't know how easy it is to unlearn Qbasic at this point. At least I will try and post my results here :)

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Really new to Java. Array question

    Quote Originally Posted by Sutured View Post
    I am 32 and I used a lot of easy and small QBasic programs to my university also. I want to know if you can 'teach an old dog new things' by learning OOP and Java.

    I don't know how easy it is to unlearn Qbasic at this point. At least I will try and post my results here :)
    If it's any consolation, I learned Java at about 50 years of age and had to unlearn Pascal knowledge from years and years ago.

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default Re: Really new to Java. Array question

    I started Java at the age of 55 and was still writing programs in VFP, including routines that invoked VBA functions in Word and Excel dociments.

    All that it really takes is that programming interests you, and that you enjoy it.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  6. #6
    Sutured is offline Member
    Join Date
    Mar 2012
    Posts
    30
    Rep Power
    0

    Default Re: Really new to Java. Array question

    Then you guys. I think you gave me the little push I needed to say that I am officially starting to learn Java.

Similar Threads

  1. Java Question [Parsing ...2D ARRAY]
    By joker760 in forum New To Java
    Replies: 1
    Last Post: 02-19-2012, 02:06 PM
  2. Question about a 2d array example
    By silverglade in forum New To Java
    Replies: 8
    Last Post: 06-06-2011, 11:56 PM
  3. Array question..
    By gerarda in forum New To Java
    Replies: 14
    Last Post: 03-02-2011, 01:42 AM
  4. array question
    By dazednconfused in forum New To Java
    Replies: 4
    Last Post: 09-15-2009, 05:44 AM
  5. Array question
    By McChill in forum New To Java
    Replies: 5
    Last Post: 02-20-2009, 02:18 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
  •