• Post Reply Bookmark Topic Watch Topic
  • New Topic

string tokenizer / string split issue  RSS feed

 
joe schwien
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello first off new to this site so hope I'm posting in right area! okay so I'm working on an assignment where the user will be asked to indicate the number of  favorite composers a certain person has. Then, the user will be asked to enter the names for that many Composers. For example,

NUMBER OF FAVORITE COMPOSERS FOR THIS INSTRUMENTALIST? 3

ENTER THE NAME FOR EACH COMPOSER WHEN PROMPTED:
#1: BEETHOVEN
#2: BRAHMS
#3: BACH

now my issue is that I have to display them seperatley on new lines with semi colons after each name such as
Beethoven;
Brahams;
Bach;
I can use either a string tokenizer or string split to do this but I have been having lots of trouble here is some code ive been working on

but this will only print the last name that I have entered AND it doesn't even show the semi colon. When I used string tokenizer I would get similar results, your guys help and or advice would be awesome thanks in advance!

 
Carey Brown
Saloon Keeper
Posts: 3329
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"favoriteComposers" should be either an array or a List. No need to use split(), you get a new name with each call to readLine().
 
joe schwien
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carey Brown wrote:"favoriteComposers" should be either an array or a List. No need to use split(), you get a new name with each call to readLine().


well the assignment requires me to use a string tokenizer or string split so if i put favorite composers into an array will i be able to tokenize the names from there?
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch. You're seeing that behavior because on line 11 you're clobbering the previously entered value. A variable like that, which you must have declared as a String, can hold only one value at any given time. Each time you assign keyboard.nextLine() to it, the value it had before will be overwritten by whatever the user just entered.

Have you learned about using arrays yet? Because that's the most logical thing to use, given that you first ask the user to enter how many composers he's going to type. With that information, you can create an array that can hold as many Strings, then use a loop to fill in the array with whatever the user enters. After that, you won't even need a tokenizer. You're just overcomplicating things with that. Keep it simple.
 
Carey Brown
Saloon Keeper
Posts: 3329
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
joe schwien wrote:well the assignment requires me to use a string tokenizer or string split so if i put favorite composers into an array will i be able to tokenize the names from there?

If that's the case then "favoriteComposers" should be a StringBuilder which you keep appending new names to delimited by ';'. Then your loop using split() should work.
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sigh.

For the life of me, I can't think of one good reason to ask the user to enter the number of names, prompt him for each one (#1:, #2:, #3, ... etc.), concatenating each value entered to get one String, only to split it up again into an array of Strings. No wonder people find it hard to learn and understand programming, they're basically being taught how to construct Rube Goldberg machines! 
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Joe, just to confirm, can you please post the instructions you were given, verbatim?
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A more reasonable requirement would be to use the new (in Java 8) java.util.StringJoiner class to print out the names entered.
 
Campbell Ritchie
Marshal
Posts: 56598
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome again Before using StringTokenizer, read its documentation:-
the StringTokenizer documentation wrote:StringTokenizer is a legacy class that is retained for compatibility reasons although its use is discouraged in new code. It is recommended that anyone seeking this functionality use the split method of String or the java.util.regex package instead.
Older versions said something about since JDK1.4, so it has been classed as legacy code for about fifteen years. The only way you will get that sort of functionality is if you write a single line of input like this:-
Mozart Brahms Beethoven J S Bach
You will have no end of difficulty getting four composers out of that. Six easily, but not four, so I suggest you instruct your user to enter one wor‍d each.

By the way, it is Beethoven not BEETHOVEN. No websites like all upper‑case writing.
 
Campbell Ritchie
Marshal
Posts: 56598
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you have a predetermined number of composers, you can pass that number to String#split.
 
Campbell Ritchie
Marshal
Posts: 56598
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What type of object is keyboard? If a Scanner, you are using it wrongly.
 
Liutauras Vilda
Sheriff
Posts: 4928
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
joe schwien, could you please upload screenshot of your code, would like to see how the formatting looks in your environment. Thank you.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!