Results 1 to 6 of 6
- 03-09-2012, 04:33 PM #1
Member
- Join Date
- Mar 2012
- Posts
- 27
- Rep Power
- 0
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
-
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.
.gif)
But the main problem I see is here:
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.Java Code:ClassA ClassA = new ClassA(); // (A) deck = Plus.ClassA(deck); // (B)
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.
- 03-09-2012, 05:18 PM #3
Member
- Join Date
- Mar 2012
- Posts
- 27
- Rep Power
- 0
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 :)
-
Re: Really new to Java. Array question
- 03-09-2012, 05:38 PM #5
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.
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 03-09-2012, 05:52 PM #6
Member
- Join Date
- Mar 2012
- Posts
- 27
- Rep Power
- 0
Similar Threads
-
Java Question [Parsing ...2D ARRAY]
By joker760 in forum New To JavaReplies: 1Last Post: 02-19-2012, 02:06 PM -
Question about a 2d array example
By silverglade in forum New To JavaReplies: 8Last Post: 06-06-2011, 11:56 PM -
Array question..
By gerarda in forum New To JavaReplies: 14Last Post: 03-02-2011, 01:42 AM -
array question
By dazednconfused in forum New To JavaReplies: 4Last Post: 09-15-2009, 05:44 AM -
Array question
By McChill in forum New To JavaReplies: 5Last Post: 02-20-2009, 02:18 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks