Carey Brown wrote:Without seeing a sample of your input data I'm going to guess that your line 17 should be moved to line 15. You are currently reading two lines for each record, splitting them on " ", and only copying [0] from the first line and [1] from the second line.
Carey Brown wrote:You could do away with an inner loop seeing as how you only have two strings you are dealing with, a key, and a value. The map.put() call makes perfect sense but I'm not so sure about the array.add() calls. Why add both the key and value to the same list? Please rename "array" to "list" because it is not an array.
Carey Brown wrote:After populating the map you can look up phone numbers using a key. Makes perfect sense.
What are you ultimately trying to do with the lists? You could have two lists, one for keys and one for values but whether that is what you need I can't tell without knowing more about how you are using the lists.
With map.keySet() you can get a set of all keys in the map. With map.values() you can get a collection of all the values. Possibly you could use these two methods instead of creating independent lists, though you might find the lists easier to deal with.
adebari olalekan wrote:what i am doing with the Lists is this: the input from stdin will be a single string of the name and the phone number on a single line of input; so what i am trying to do is to split the long string of name and number into two(name and number), and store them both in a list , then if i want to populate the Map i will then call the index of the list where the number is situated and use it as the value , and also call the index of the list where the name is situated and use it as key in the Map. i have tried using array but there are problems when splitting the string. and with this list now the splitted strings seems not to be saving in the list , probably because of iterations of the loops. that is the issue at hand.
Carey Brown wrote:
adebari olalekan wrote:what i am doing with the Lists is this: the input from stdin will be a single string of the name and the phone number on a single line of input; so what i am trying to do is to split the long string of name and number into two(name and number), and store them both in a list , then if i want to populate the Map i will then call the index of the list where the number is situated and use it as the value , and also call the index of the list where the name is situated and use it as key in the Map. i have tried using array but there are problems when splitting the string. and with this list now the splitted strings seems not to be saving in the list , probably because of iterations of the loops. that is the issue at hand.
What I think you are saying is that your data looks like:
Where "Joe" is the name and also the key in your map, and that 555-1212 is the phone number and the value in your map.
You are already reading in the data line by line and taking each line and splitting it in to two parts: name, and phone. So far, so good.
Then later the user will be prompted for a name, the name will be used to find the phone in the map.
Where I'm confused is that I don't see a need for arrays (lists) at all in this scenario, so perhaps I'm getting this wrong.
My interpretation of your above paragraph is that you first want to read in all the name/phone pairs and add them correspondingly to a list of names and a list of phones. Then as a secondary step, go through the list of names and for each name populate a map with the name as the key and then using the index of the name in the name list, look up the corresponding phone in the phone list at the same index and use that phone to populate the value in your map.
If that is what you are saying then that is doable but involves extra steps to achieve the same ends.
I'm confused in this scenario when you say "IF I want to populate the Map...", do you need a map or don't you?
And thirdly, you say "splitted strings seems not to be saving in the list". Is your list empty? Have you tried to print it out? I'd need to see the latest copy of your code to possibly answer this.
All things are lawful, but not all things are profitable.
Knute Snortum wrote:Why can't the key of the Map be the name and the value of the key be the phone number? What is the List for?
adebari olalekan wrote:
Knute Snortum wrote:Why can't the key of the Map be the name and the value of the key be the phone number? What is the List for?
Yeah! That's why I had to bring the problem here; I did it that way and it did not work; simply because of what I did not know; then I got an idea of putting it in a list first and it got complicated . if you run it this way and it worked , that means you did not include the inner loop that is meant to process the input and insert it in the Map. And it has to be that way because; each input need to be processed before being inserted into the Map.
Carey Brown wrote:
adebari olalekan wrote:
Knute Snortum wrote:Why can't the key of the Map be the name and the value of the key be the phone number? What is the List for?
Yeah! That's why I had to bring the problem here; I did it that way and it did not work; simply because of what I did not know; then I got an idea of putting it in a list first and it got complicated . if you run it this way and it worked , that means you did not include the inner loop that is meant to process the input and insert it in the Map. And it has to be that way because; each input need to be processed before being inserted into the Map.
split() IS the process.
adebari olalekan wrote:yeah i know that split() is the process, but it just doesnt work like that ; because when i tried referencing the index of the split() array and using it to populate the map, all i get is outOfBound Error pointed at the array.
Carey Brown wrote:
adebari olalekan wrote:
well, well, i think it worked this way; but can you please explain the reason why the sc.nextLine(); caused this problem?Carey Brown wrote:
Carey Brown wrote:
adebari olalekan wrote:
well, well, i think it worked this way; but can you please explain the reason why the sc.nextLine(); caused this problem?Carey Brown wrote:
It's a "feature", not a bug, of the Scanner class. When you call nextLine() you get everything up to the new-line and it throws away the next pending new-line. ALL other nextXXX() calls will leave any pending new-lines in the queue, so you have to flush them out by calling an additional nextLine() and throwing away the results (see line 3). This is a Scanner gotcha.