Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Feb 2011
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    One Vector two Jlist's

    I have this assignment due and having trouble with the list's, the assignment is to take these file names and split them into two list one being artist names and the other song titles. I've managed to split them, but the the artist name either shows on both lists ore both the artist and song title are on the same list or on both list's. Here some of my sample code any suggestions would mean a lot.

    Code:
    ...
        private Vector<String>  items;
    
        private static String   artists = "Artist";
        private static String   title = "Title";
    
        public jTune_Gui(){
            this(new Vector<String>());
        }
    
        public jTune_Gui(Vector<String> listEntries){
            super("Nahid's jTunes Browser");
            items = listEntries;
            intializeComponents();
    
             jOpenButton.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    jOpenButtonEventHandler();
                }
            });
    
            setDefaultCloseOperation(EXIT_ON_CLOSE);
            update();
    
        }
    
         private void intializeComponents(){ some code ..}
         private void jOpenButtonEventHandler(){ some code ...}
         private void update(){
            jArtistList.setListData(items);
           // jSongsList.setListData(items);
        }
    
        private void traverse(File fileOrDir){
            if(fileOrDir.isDirectory()){
                File[] kid = fileOrDir.listFiles();
                for(int i = 0; i < kid.length; i++){
                    traverse(kid[i]);
                }
    
            }else{
                String fileName = fileOrDir.getName();
    
                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();
    
                Song sonG = new Song(newSongTitle, newArtistName);
    
              //  items.add(sonG.getSongTitle());
                items.add(sonG.getArtist());
            }
        }
       
    }

  • #2
    Regular Coder Aradon's Avatar
    Join Date
    Jun 2005
    Location
    USA
    Posts
    734
    Thanks
    0
    Thanked 20 Times in 19 Posts
    What is your input to this? Is it like

    Code:
    -Aradon Rocker - "Sometimes I'm awesome"
    Or is it like

    Code:
      Aradon Rocker - "Sometimes I'm awesome" -
    Or is it like

    Code:
      Aradon Rocker - - "Sometimes it hurts to be so awesome"
    "To iterate is human, to recurse divine." -L. Peter Deutsch

  • #3
    New to the CF scene
    Join Date
    Feb 2011
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    The input's like this

    Code:
    01 - infected mushroom - cities of the future - radio edit.txt

  • #4
    Regular Coder Aradon's Avatar
    Join Date
    Jun 2005
    Location
    USA
    Posts
    734
    Thanks
    0
    Thanked 20 Times in 19 Posts
    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?
    "To iterate is human, to recurse divine." -L. Peter Deutsch


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •