Win a copy of Java Mock Exams (software) this week in the Programmer Certification (OCPJP) forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Getting the size of a arraylist and making it a variable

 
David Ausere
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My arraylist is called Roster.  So here is my code for this thought.

int ID = getNumRoster();
               System.out.println("Your Student ID is: " + ID);

Obviously this is not working.  Where am I missing in my logic?

 
Liutauras Vilda
Marshal
Posts: 3385
131
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch.

The information provided is not sufficient. Please show us more code. Particularly getNumRoster() method. You have mentioned Roster as being an ArrayList - we don't see that part nowhere in the provided code either.
 
David Ausere
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Liutauras Vilda
Marshal
Posts: 3385
131
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And expand yourself on "not working" - what do you mean by that?
 
Liutauras Vilda
Marshal
Posts: 3385
131
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Earlier, I wrote wrote:Please show us more code. Particularly getNumRoster() method.
 
David Ausere
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry been working on this for hours and my head is starting to melt.  I know my method is not working as in getting the number of the size of the array and putting that into a variable.  I think I need a nap.
 
David Ausere
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is what I had before.

 
Liutauras Vilda
Marshal
Posts: 3385
131
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
David Ausere wrote: I think I need a nap.


That helps. Take some rest and only then come back to the problem. Now in order to get best help here - always describe problem in full. i.e.:

1. What is your goal
1.1 What is the expected outcome

2. What is the actual outcome
2.1 What is the error in case of it

3. Provide code which you think does that and where you think problem appears.

Will be waiting you after you take a break
 
Paul Clapham
Sheriff
Posts: 21945
36
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are indeed getting the size of an ArrayList and making it a variable. That's precisely what your code does.

But you're unsatisfied with the result of doing that, aren't you? And rightly so... if I showed up as a student and asked what my student ID is, I would be taken aback if you said "OK, there are 13 students so your student ID is 13".
 
David Ausere
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Liutaurus.

My goal is this:

Look at the size of the array and take that number and make it the student ID + 1.

This current method gives me the ilegal start of the expresion error.  Going to go eat now and nap.  Will look forward to peoples input.
 
David Ausere
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Figured it out!
 
Knute Snortum
Sheriff
Posts: 3320
84
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Great!  How would you feel about posting the solution here?  That way others can benefit from it.
 
David Ausere
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pretty simple.  If you see and failure in my logic tho, please let me know.

 
Knute Snortum
Sheriff
Posts: 3320
84
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What about using Scanner#nextInt()?
 
What are you doing? You are supposed to be reading this tiny ad!
the new thread boost feature brings a LOT of attention to your favorite threads
https://coderanch.com/t/674455/Thread-Boost-feature
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!