Can you give us an example of when it doesn't work?
Here is the code I wrote and it's outputs:
Code:
import java.util.*;
public class testCodingforums {
public static void main (String args[])
{
String fileName = "01 - infected mushroom - cities of the future - radio edit.txt";
StringTokenizer tokens = new StringTokenizer(fileName);
boolean firstDashFound = false;
boolean secondDashFound = false;
String songTitle = "";
String artist = "";
while(tokens.hasMoreElements()){
String aWord = tokens.nextToken();
if(firstDashFound && !secondDashFound && !aWord.equals("-"))
artist = artist + aWord + " ";
if(secondDashFound) songTitle = songTitle + aWord + " ";
if(aWord.equals("-")){
if(firstDashFound) secondDashFound = true;
firstDashFound = true;
}
}
String newSongTitle = "";
boolean dotFound = false;
for(int i = 0; i < songTitle.length(); i++){
if(songTitle.charAt(i) == '.') dotFound = true;
if(!dotFound) newSongTitle = newSongTitle + songTitle.charAt(i);
}
String newArtistName = artist.trim();
System.out.println(newArtistName);
System.out.println(newSongTitle);
}
}
Code:
Outputs:
infected mushroom
cities of the future - radio edit
So it looks like your looping is pretty sound given the input you've given us. Maybe your outputting is wrong?