How JustAnswer Works:
  • Ask an Expert
    Experts are full of valuable knowledge and are ready to help with any question. Credentials confirmed by a Fortune 500 verification firm.
  • Get a Professional Answer
    Via email, text message, or notification as you wait on our site. Ask follow up questions if you need to.
  • 100% Satisfaction Guarantee
    Rate the answer you receive.
Ask TheDoctor Your Own Question
TheDoctor, Professional w/Adv. Degree
Category: Homework
Satisfied Customers: 1757
Experience:  M.S. in Internet Information Systems
Type Your Homework Question Here...
TheDoctor is online now
A new question is answered every 9 seconds

Write a program which returns the name of every teacher "Student1"

This answer was rated:

Write a program which returns the name of every teacher "Student1" has had from these pairs. In Java. Please solve it in 2 ways:
1. Using loops/arrays
2. Using hashmaps/hashtable.

(Teacher1, Student1) , (Teacher2, Student1), (Teacher3, Student2), (Teacher4, Student1)
Hello and thank you for your question. It would be my pleasure to assist you with this.

I will contact you once this is complete.

Thank you
Would you like the hashmap solution and the array solution demonstrated within the same program? Or would you prefer them to be separate programs?
Customer: replied 4 years ago.

separate please.

Hello again!

Please download the files here:


In this zip file, you will find three different java files. They are: -- The HashMap version of the code -- The arrays version of the code -- A version that does both

I created one that does both for you just in case you wanted it. You still have the two that are separate.

I have fully commented the code. In all of the files.

Let me know if you need anything or have any questions. I'm here to help!

If you need any assistance in the future, please feel free to start your questions with **For TheDoctor** to request me directly.

Please remember to rate my answer. Thank you so much and have a wonderful day!
Customer: replied 4 years ago.

for the array, could you do it without using built-in functions?


using a logic approach? I need to understand how it works.




for (int i=0; i<myArray.length; i++)


myArray[1]= ........



Customer: replied 4 years ago.

perhaps using this multi-dimensional array:


String[][][][] myArray= {{{{"Teacher1", "Student1"} , {"Teacher2", "Student1"}, {"Teacher3", "Student2"}, {"Teacher4", "Student1"}}}} ;

First, what do you mean by built in functions? There are no special built in functions being used.

Second, that multidimensional array is not accurate, but I will see what I can do...
Customer: replied 4 years ago.

Thanks. If you can break down the program as much as possible to a logical level, that would be great. I need to see something like myArray [1] which would be the index locations...etc

When you loop through an array, you usually use:

myArray[variable], not an actual number. That usually does not make any sense.

Take your above code:

for(int i = 0; i < myArray.length; i++) {


Now, in that loop, you will ALWAYS be referring to the SECOND element of the array (arrays always start at index 0). The entire purpose of looping through an array is to get the NEXT ELEMENT of the array on each loop through. That is why the loop variable i exists. When you make a call to:


It isn't any special built in function. The first time it loops, that is just myArray[0], then myArray[1], then myArray[2] and so on and so forth. It is the correct way to loop through an array. If you explicitly put an integer constant in the index value, then you are not looping through the array correctly.

You loop through arrays with index variables, not index constants. If I had a student who did that, they would receive serious deductions because the program simply wouldn't work correctly. That's not how loop through an array. It won't work.

All that said, you could use a two-dimensional array, loop through the dimensions, and then make an explicit call to the indexes at the deepest level (student/teacher pair). It is not "more logical". It's just adding an additional layer of data.

Take a look at this file:


In this one, we use a two-dimensional array in order to specify the teacher/student pair. Then we MUST loop through the first dimension and explicitly reference the second dimension with index constants. It is closer to what you were asking for and is also a completely valid way of doing this. Though the Hashmap is really the preferred method is cases of key/value pairs.

I don't think we should change it beyond this, as it will start to become convoluted and you will likely receive very low marks. this code is as "logic" and broken down as it gets. It is text-book array looping.

Let me know if this is what you were looking for and please remember to rate my answer.

Thank you again!
Customer: replied 4 years ago.

This looks great, one question, is this expressions just looping through, I have worked with colons in a for loop before, is there another way to replace this expression:


for(String[] teacherStudent : students)

Looping through an array using the colon like this is done when there is absolutely no need to reference the index of the first dimension with a variable (which you seem to want to avoid anyway). The way that I did it is the cleaner, better way of doing it. Whenever you can do it like this, you should. It is the equivalent of C#'s and PHP's foreach function. It is a staple of good programming.

But yes, you can do it a dirty way, which will require you to use index variables. like this:

for(int xy = 0; xy < students.length; xy++) {

if(students[xy][1] == students) {




This does the exact same thing as the code that I just sent you, but uses index variables instead of member instances. As I said, it is dirtier way. The only real reason to do it this way is if you needed the array index (xy) for some reason. And you MUST use xy was the array index. In increments on each loop through (0 - 3), hence the xy++ in the for statement. The first time it loops through, it will perform the code on students[0][1], then students[1][1], then students[2][1], the students[3][1]. By looping, we can use the index incrementing loop variable xy so that we only have to write the conditional code once. Otherwise, we wouldn't be using a loop at all, and that wouldn't meet your requirements.

The only real reason to do it this other was is if you needed the index for something -- which you don't in this case. But either way works.

As a project lead and lead engineer on many projects, I would prefer my developers to use the cleaner way, but that's up to you. I think your instructor would prefer the cleaner way as well. But some instructors are weird about stuff.

If you want to use this other version, here it is:

But you must use xy was the index key for the first dimension of the array. That how looping occurs.


Honestly, you should receive full marks for either version. If I were you, I would submit the version with the colon. It is more in-line with what would be done in the real world. No need to use verbose for loops when a foreach will do the job.


Anyway. Please do remember to rate my answer.


Thank you again.

Customer: replied 4 years ago.

This is not for homework or anything, Im just trying to learn programming myself.


For this expression:

if(students[xy][1] == students)


getting the following error:

Incompatible operand types String and String[][]


Any thoughts?


You must have not changed something correctly. Try this version:


This version uses the code mentioned and works without any errors.

The colon is the preferred method in the real world. If this were PHP, it would be:

foreach($students as $studentTeacher)

In C#, it would be:

foreach(String[] studentTeacher in students)

And in Java, its:

for(String[] studentTeacher : students)

But the copy I just sent you will work without using this type of for loop.

Let me know if you need anything else. And please remember to rate my answer.

If you have further Java, C#, PHP, ActionScript, HTML/CSS, ColdFusion, Ruby, Ruby on Rails -- programming in general questions, please feel free to ask for me in the future!

You need to spend $3 to view this post. Add Funds to your account and buy credits.
TheDoctor and other Homework Specialists are ready to help you
Customer: replied 4 years ago.

I appreciate it, you are awesome!

Thank you. You are also awesome! It was a pleasure working with you.