Win a copy of Java 9 Revealed this week in the Features new in Java 9 forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Array Splitting  RSS feed

 
adebari olalekan
Ranch Hand
Posts: 48
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hey! can someone please show me what is wrong with this block of code.

what i am trying to do is to create a phonebook that will receive the ; first input as the amount of entries the user want to enter ; and the subsequent inputs will be the input of the phonebook details in
the form of a long string . it will include the name of the person and the phone number , on a single line and then after the input ; i will split the inputed values into two and store each as a key and value in a map. but i think my code should work but ; actually it has not been , an insight will be helpful.......


the outputs includes cutting off some part of the values and keys ; and seemingly unstable results.

 
Carey Brown
Bartender
Posts: 2606
40
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
adebari olalekan
Ranch Hand
Posts: 48
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.


yeah! i had thought so ; but , it didn't work.the result get more messier; and i hope you can just run the code to make a clear cut of the issue. thanks

i also try handling one of the loops with a method , by receiving an input of type array into the method from the main method; but , same problem persists.
 
Carey Brown
Bartender
Posts: 2606
40
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
adebari olalekan
Ranch Hand
Posts: 48
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.


are you suggesting i created a seperate list for the two values, i.e after splitting them i should store them in two seperate Lists?
 
Carey Brown
Bartender
Posts: 2606
40
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Ranch Hand
Posts: 48
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.


ofcourse i understand how to manipulate the Map , but the problems lie in populating it with the way the input is being generated......
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
Bartender
Posts: 2606
40
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Carey Brown
Bartender
Posts: 2606
40
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is very easy to be caught up in the "how" part of the design without taking the time to review the scenarios (a.k.a. "use cases") that the program is expected to support.

A use case might look like:
  • User enters a quantity for the number of data records that will be entered.
  • User then is prompted for that quantity of name/phone pairs.
  • User is prompted for a name to look up
  • The user is presented with the phone number corresponding to the name.


  • I'm not clear on your scenario so it's difficult to advise you on how to proceed.
     
    adebari olalekan
    Ranch Hand
    Posts: 48
    Chrome Eclipse IDE Java
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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.



    Yes I will definitely need a Map; the statement " IF " was a typographical error and I can't edit here.

    So the List is not empty , but it only holds one value instead of two. The ArrayList is supposed to help to hold the splitted string, so that it can be used to populate the Map, as the splitted string of array that is received from the stdin cannot be used to populate the Map directly .
     
    adebari olalekan
    Ranch Hand
    Posts: 48
    Chrome Eclipse IDE Java
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    The scenario is just the way you had described it in the latter comment ; its as simple as that. Nothing more, and I have only been having problems with populating the Map.
     
    Knute Snortum
    Sheriff
    Posts: 3612
    86
    Chrome Eclipse IDE Java Postgres Database VI Editor
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Ranch Hand
    Posts: 48
    Chrome Eclipse IDE Java
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Bartender
    Posts: 2606
    40
    Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Ranch Hand
    Posts: 48
    Chrome Eclipse IDE Java
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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.


    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
    Bartender
    Posts: 2606
    40
    Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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.

    Ah, now we're getting somewhere.
    Put in some prints like


    That should give you a clue as to where the problem is.

    I believe I see your problem, you are using sc.nextInt() to get the number of testcases, but this call leaves a new-line in the scanner and the very next call to sc.nextLine() will be an empty string causing your array out of bounds problem. After you call nextInt() then call nextLine() NOT INSIDE THE LOOP.
     
    Carey Brown
    Bartender
    Posts: 2606
    40
    Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
     
    adebari olalekan
    Ranch Hand
    Posts: 48
    Chrome Eclipse IDE Java
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Carey Brown 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
    Bartender
    Posts: 2606
    40
    Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    adebari olalekan wrote:
    Carey Brown wrote:
    well, well, i think it worked this way; but can you please explain the reason why the sc.nextLine(); caused this problem?

    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.
     
    adebari olalekan
    Ranch Hand
    Posts: 48
    Chrome Eclipse IDE Java
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Carey Brown wrote:
    adebari olalekan wrote:
    Carey Brown wrote:
    well, well, i think it worked this way; but can you please explain the reason why the sc.nextLine(); caused this problem?

    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.


    ok, so its like rebooting the scanner ;  a way of clearing the table so that the next input will not queue. thanks you very much.
     
    Carey Brown
    Bartender
    Posts: 2606
    40
    Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Glad to be of help.
     
    Joe Valdes
    Greenhorn
    Posts: 1
    Java MySQL Database Netbeans IDE
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Try this:


    Results:
    Records: 3
    Add the 3 records
    Use notation 'Name_Phone': Joe_6234634567
    Use notation 'Name_Phone': Leo_6234574568
    Use notation 'Name_Phone': Red_1234645788
    {Joe=6234634567, Leo=6234574568, Red=1234645788}

     
    It is sorta covered in the JavaRanch Style Guide.
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!