• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

Exception in thread "main" java.lang.Stack

 
Ranch Hand
Posts: 218
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I really am a beginner so please accept my apologies if this is an obvious question.

I have a class called Student. Which I am testing in the main TestStudent. Both of these are in the file saved as TestStudent.java.

I am not trying to do anything clever, but I am just getting my head around HashMaps.

In the main part TestStudent I am obtaining two details about a student. Their Name which is to be used as the key and the Course they are enrolled on. I am then passing these to a Hashmap in the Student class. This is in a for loop.

The program compiles fine. But when I enter only one student name and one student course I then get the Exception in thread "main"java.lang.Stack.

My Code is as follows:



The main TestStudent is
[CODE]
public class TestStudent
{
//This is the main program to get input using Java's JOptionPane class and call the showInputDialog method
public static void main(String [ ]args)
{
String Name; //One of the student names input by user
String Course; //One of the stuent enrolled courses input by user
int NumCt = 0; //The number of student names & enrolled courses
// input by user

//GET DATA INPUT FROM USER
for (int lp = 0; lp<=NumCt; lp++)
{
Name=JOptionPane.showInputDialog("Please Enter Student Name: \nEnter 'Exit' to end program");
Course=JOptionPane.showInputDialog("Please Enter Student Enrolled Course: ");

//EXIT LOOP IF "Exit" HAS BEEN ENTERED
if (Name.compareToIgnoreCase("Exit")==0)
{
break;
}
else
{
Student.put(Name, Course); //Store the name and enrolled course in the student class
System.out.println("The number of values in the Student map are: "+Student.StudentDetails.size( ) );
NumCt++;
}//end if else
}//end for statement
System.exit(0);//this is needed when using JOptionPane
}//end main program
}//end public class Student
/CODE]

Previously I have successfully implemented this using a Dynamic Array i.e. the Array Size was determined at run time however, I am now attempting to do it using a HashMap. But I am struggling to get my head around things.

Firstly, what does the exception in thread "main" java.lang.Stack mean? Is it to do with recursion i.e. including the class Student and main TestStudent together is not such a good idea?

Secondly, with a dynamic array you use a loop to implement the array reference for the data i.e. Name would be Name [0][0 ] the first name and Course would be [0][1] but I'm not so sure I have implemented this for a HashMap i.e. as Name is my Key and Course is my value surely I do not need a loop to increment anything in the hashmap; and if I did is my understanding correct when I think I have to use the Set i.e. Set keys = map.keySet() to get an Iterator and use the iterator to put each of the elements of the set in turn?

Thanking you in advance - much appreciated. (Again, apologies if this is so simple).
 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

you can hit StackOverflow errors on infinite recursions ... and that's what happens in your code.

Student.put() method should not call Student.put() it should call StudentDetails.put(), it seems you did a kind of spelling mistake !

Your code contains other errors, but as your are learning, I'll let you find them (or ask again here)

Happy learning, Arnaud
 
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
You're not a Computeach student doing the first programming exercise of the Java Stage 3 course by any chance? Just that your problem bears a striking resemblance to that assignment.
That assignment also requires you to develop your own method for calculating a hashcode from the Student name, rather than relying on the HashMap implementation to do the work for you. You will learn a lot more about writing code if you write your own method.
Good luck!

Dun
 
Maureen Charlton
Ranch Hand
Posts: 218
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you for your response. However could you divulge a little more on answering the original questions:

"Firstly, what does the exception in thread "main" java.lang.Stack mean? Is it to do with recursion i.e. including the class Student and main TestStudent together is not such a good idea? (Arnald Burlet - thanks for your input here).

Secondly, with a dynamic array you use a loop to implement the array reference for the data i.e. Name would be Name [0][0 ] the first name and Course would be [0][1] but I'm not so sure I have implemented this for a HashMap i.e. as Name is my Key and Course is my value surely I do not need a loop to increment anything in the hashmap; and if I did is my understanding correct when I think I have to use the Set i.e. Set keys = map.keySet() to get an Iterator and use the iterator to put each of the elements of the set in turn? (A response on this question would be appreciated!)

Dun Dagda - many thanks for your response. Yes I am a Computeach student doing the first programming exercise. I understand what the assignment is asking BUT I would like to fully understand HashMap before doing the actual assignment. It just the way I work. Small bits at a time. Are you able to assist with the questions above?
 
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Secondly, with a dynamic array you use a loop to implement the array reference for the data i.e. Name would be Name [0][0 ] the first name and Course would be [0][1] but I'm not so sure I have implemented this for a HashMap i.e. as Name is my Key and Course is my value surely I do not need a loop to increment anything in the hashmap; and if I did is my understanding correct when I think I have to use the Set i.e. Set keys = map.keySet() to get an Iterator and use the iterator to put each of the elements of the set in turn? (A response on this question would be appreciated!)


No you don't need a loop to fill a HashMap (or an Iterator or KeySet). Basically an array is a sequential list which you add to with a position (the index), so you need to say "Object A goes at position 1 in my list", which would look like this:

A HashMap is a unordered, randomly accessable table which you add to with a key. You need to say "Object A is the Object for 'Maureen', so it goes in the 'Maureen' place holder in the table", or:

Its actually a far easier (and more natural) way of handling this than using two-dimentional arrays. One caveat is that it presumes a one to one relationship between name and course - there is only one "Maureen" placeholder, so there can only be one course for "Maureen" - call map.put( ... ) again and the first value is replaced.

If you have an existing list (possibly an array) of values you want to put into a Map, I'd iterate through the list and call put for each object (as you are sort of doing). Its the most obvious way of doing this.
 
Arnaud Burlet
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry, but I'm not that good at english, I'll try to answer your 2nd question, but I'm not sure I understood it well !

Originally posted by Maureen Charlton:
"Firstly, what does the exception in thread "main" java.lang.Stack mean? Is it to do with recursion i.e. including the class Student and main TestStudent together is not such a good idea? (Arnald Burlet - thanks for your input here).


That exception means you eated all the available RAM. (see below)
It has to do with recursion yes, but "including the class Student and main TestStudent together" is not the problem, that's normal use. Your problem is INFINITE recursion! Let me explain that quickly.
Whenever you call a function - like System.out.println() or Student.put() - you need some memory on the stack to save informations. The stack is a special zone of memory, a part of your RAM. Each function call needs some space on the stack and if a function call can't obtain the needed space on the stack, you get a java.lang.StackOverflow exception (the one you got running your program).
Now, how is it your program eated all the stack space ? This is because you call infinitely Student.put() (remember each call need some space on the stack), so after some number of function calls, your stack gets full and you get the exception!

got it ?

Originally posted by Maureen Charlton:
Secondly, with a dynamic array you use a loop to implement the array reference for the data i.e. Name would be Name [0][0 ] the first name and Course would be [0][1] but I'm not so sure I have implemented this for a HashMap i.e. as Name is my Key and Course is my value surely I do not need a loop to increment anything in the hashmap; and if I did is my understanding correct when I think I have to use the Set i.e. Set keys = map.keySet() to get an Iterator and use the iterator to put each of the elements of the set in turn? (A response on this question would be appreciated!)



No, you don't need anything special to put values in a HashMap. The way you do it is perfectly correct.
But I think you have a problem, something like only one name,course in the hashmap ?
If that's the case, check your Student.put() method, your body (after my first correction) should be

And that's where the problem resides ... What are you passing to the put() method ? You are not passing the name and course parameters, your are always ordering the hashmap to store the same Strings ! just remove the "" so that your code looks like :


Arnaud

[edit : ubb hates me today, polishing]
[ October 15, 2004: Message edited by: Arnaud Burlet ]
 
Maureen Charlton
Ranch Hand
Posts: 218
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Paul Sturrock and Arnaud Burlet
THANK YOU!
I really was having problems understanding why one would wish to use a HashMap - seemed simple to use a dimensional array. Things are clearer.

However, a HashMap does seem more complicated to code. Especially when trying to print things out and testing.

To print everything out then i.e. the key and the value I assume you have to do the following:



Seems alot of additional code and effort to me.
Is this worthwhile? Or am I missing something?
I guess I've got hooked into dimensional arrays!
 
Dun Dagda
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think you are making life a bit too complicated for yourself. To solve this exercise, you do not need to bother about HashMaps, etc. All you need to do is define your Student class properly.
This will be something like:

Once you have your Student class properly defined:
In your Storage class, you can then define an array of Student objects
that will store a reference to each Student object, which in turn will hold the name and course information for each student that you want to store.


This should give you a basic idea to get started, and is the approach I used to pass the assignment when I did it. Don't worry about multidimensional arrays and HashMaps, they are only a distraction at this stage. The key point is that once you have your Student object properly defined, you can make an array out of it.

Good luck with your studies,

Dun Dagda
 
Maureen Charlton
Ranch Hand
Posts: 218
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Dun Dagda,

Thank you very much for your response. Sounds like I have gone way, way to far than what is needed for the actual assignment. However, I've got hooked on finding out about this HashMap now. I'll get there in the end.

Incidently, when you did this assignment. How come you didn't use a dimensional array or a HashMap when you didn't know how many student details to enter? Hence you are unable to initialise it to a size. I noticed above you initialised it to 3 i.e. the number of students you wanted to store?
 
Paul Sturrock
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes Maureen it is a little code-heavy iterating through a HashMap and churning out its contents. Its like this because of what a HashMap is designed for: namely a collection of values to be randomly accessed by key. So sequential access is a little awkward.

You could use Dun Dagda's alternative, except it has two problems. It presumes there is a one to one relationship between Name and Course (as does a HashMap, true), so there still can only be one course for "Maureen". Also it requires you know how big your collection will be before you use it, since arrays are not "growable", unlike Collection objects (Map, List and Set - as you've correctly noticed).
[ October 18, 2004: Message edited by: Paul Sturrock ]
 
Ranch Hand
Posts: 1923
Scala Postgres Database Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I would suggest to rethink your commenting-style.

Look at this:

and that:

and decide what's more readable.
Note, that your comment 'Public members' is wrong.
Sections are used in c++ - in java there is no section-idea.
 
Maureen Charlton
Ranch Hand
Posts: 218
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Stefan,

I note your comment about my comments on Public members being wrong.
I took this straight from my Computeach book. I guess for work I do for them I will have to use it but going forward I will take it out.

Thanks again for all your help. Much appreciated.

 
Dun Dagda
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I only gave three students as an example. For the actual implementation in my own assignment, I used JOptionPane to prompt the user to enter the number of students they wanted to store.

 
Dun Dagda
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The other thing I wanted to mention was that you don't need to worry about HashMap and its relatives at this stage. You will find out all about these when you come to the third assignment on the Computeach Java Stage 3 course, The Police Database.

Concentrate on defining your methods and calling them properly, as I see you are being advised to do on another thread in this forum.

DD
 
Maureen Charlton
Ranch Hand
Posts: 218
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
All,

Many thanks for your help - this thread can now be closed. But I don't know what I need to do for this to happen?
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic