Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

passing arrays between claasses  RSS feed

 
Robert Butler
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator





I am required to use two classes for this and i understand how to pass a variable between them but the array seems a bit more off to me. My question is on the proper call to pass the array back and forth. Eventually the assignment will list 10 questions individually and receive a True/False (T/F) for an answer then compare with correct answer and spit out a grade with a printout of the questions, user answers and correct answers...
 
Eris Dreiling
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, you have that getAnswers() method there. You can make an object of Test and call that method, store it in some array, and now you have that data you wanted (if that was the array you were talking about?)

Also, I might be confused here, but that getQuestions() method you made isn't static, so it's kind of weird that you're calling it with the class name on line 17 of TestQuestionsTester.
 
David Hulse
Greenhorn
Posts: 24
Mac MySQL Database PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
char[] userAnswers = {ansUser[count]};

can you actually do this? you have this array declared already and it's actually just a char right now.. so I don't think you're doing it right. If you're trying to store multiple answers like I think you you should redeclare userAnswers to a char[]

use this:

userAnswers[count] = ansUser[count];

that will put them all in the userAnswers character array using your for loop.. I might be wrong but I've never seen the syntax you used.
 
David Hulse
Greenhorn
Posts: 24
Mac MySQL Database PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As far as passing the answers between arrays you might want to use a setAnswers() function in your other class and then pass it the results of getAnswers in your main function or in your constructor.
 
Robert Butler
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Eris, i was testing to see if it was possible that way but since it wasn't i had no need to change it until i felt i could call it correctly.
David, yes that is more along the lines of what i'm trying to do, no what i was doing was combining things i was seeing in different samples online, kind of just trying things to see what will make it work... finding the proper syntax is tricky... but to call the array as if i were calling an object into my TestQuestionsTester what would be the syntax for that, say to call the correctAnswers from Test?
 
David Hulse
Greenhorn
Posts: 24
Mac MySQL Database PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
WHOA.. you really ought to learn a bit more about the language before you go around just copy and pasting things. You ALWAYS want to make sure that you're not pasting malicious code into your compiler. Watch out for the javascript hoaxes on Facebook and other places on the net too. DON'T PASTE ANOTHER PERSON'S CODE IN YOUR BROWSER WITHOUT KNOWING WHAT IT'S DOING.
 
Robert Butler
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yeah taking Java 1 but it's hard to get a hold of the professor regularly so i find that i spend hours trolling for code... hence how i ended up here, got tired of staring, hunting, attempting to implement and finding out it doesn't work, then i've spent hours to learn how something doesn't work. and the API is so vague sometimes... the one call i attempted was based on the concept of calling an object(or variable i guess) from the getMethod to the other class... object.methodName(variable_being_passed)...
i posted the following on facebook for a couple of my buddies who do programming as well, this is kind of a summation of where i am confused i guess

 
David Hulse
Greenhorn
Posts: 24
Mac MySQL Database PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yeah. that's kind of the problem. what it seems like you're doing is completely relying on other people to write your code for you. You need to actually read and understand your java book. Not just copy and paste codes from the internet for an intro class. Eventually you're going to be expected to solve problems that don't just require a search on google.

What i'd like you to do is read your book starting at the first page. When you're done with that page you can flip it and read the next. Make sure you understand as much of that book before your next class period and then if you have trouble ask your professor immediately after class.. even if you'll be late for your next one.

If you want you can add me on Facebook, and I'll answer your questions in real-time. I live in Vermillion, SD.
 
Robert Butler
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the only code copied and pasted on here at all was the
{ansUser[count]};
and the
Test.getQuestions[count] which prior to was questions[count]
...
the rest of the code and the conceptual stuff are all written from my head... personally to say all i'm doing is copying and pasting is offensive. i wasn't asking for anyone to write it, i was merely asking for the proper call syntax. i was going to post on here as with what i did on facebook however, i read another post where people asked for the actual code to better understand what the author is trying to do. i'm attempting to understand it, i read the book i go to lectures and while the professor is available i pick his brain, but that's at best an hour a week that i have to share with 10 other classmates. i simply felt as if i was at a dead end trying to understand the syntax of array calls as my book talks about arrays for 2 pages and then spends the rest of the chapter referring to arraylists, can arraylists be called in the same fashion as arrays?
 
David Hulse
Greenhorn
Posts: 24
Mac MySQL Database PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ArrayList<Object> arraylistName = new ArrayList<String>();

that's how you create a new ArrayList. So it's somewhat different than an array. The best thing about ArrayLists is that they don't have to have a set capacity from initialization.

Then you call your arraylist like this

arrayListName.add(Whatyouwanttoadd);
arrayListName.get(indexofwhatyouwanttoget); <-- this part is just like an array indexes start at 0.

arrayListName.size(); will return the size of the list

Do you have Netbeans?
 
Robert Butler
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
figured it out...


  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!