Results 1 to 4 of 4
  1. #1
    javakaz is offline Member
    Join Date
    Mar 2016
    Posts
    1
    Rep Power
    0

    Default Sort an array list of pathnames in java.

    My user input is

    \home\me\cs1

    \usr\share

    \var\log

    \usr\local\jdk1.6.0\jre\lib
    and I need to sort these pathnames so that the output is in the correct lexographic order. However they are first sorted by length which is the number of slashes in each string. The path names are stored in an arraylist of strings. I can not use collections,comparator or arrays. Must be arraylist. Can anyone tell me what is wrong with my code?

    the output should be:

    \usr\share

    \var\log

    \home\me\cs1

    \usr\local\jdk1.6.0\jre\lib
    This is my code so far:
    Java Code:
    import java.util.ArrayList;
    import java.util.Scanner;
    public class FileName 
    {
    private ArrayList<String> pathNames;
    
    public FileName()
    {
        pathNames = new ArrayList<String>();
    }
    
    public void printPaths()
    {
        for(int i = 0; i < pathNames.size(); i++)
        {
            System.out.println(pathNames.get(i));
        }
    }
    public int pathLength(String path)
    {
        int count = 0;
        String slash = "\\";
        for(int i = 0; i < path.length(); i++)
        {
            if(path.substring(i,i + 1).equals(slash))
            {
                count++;
            }   
        }
        return count;
    }
    
    public void sort()
    {
        pathNames = mergeSort(pathNames);
    }
    public ArrayList<String> mergeSort(ArrayList<String> paths)
    {
        if(paths.size() == 1)
        {
            return paths;
        }
        else
        {
            ArrayList<String> left = new ArrayList<String>();
            ArrayList<String> right = new ArrayList<String>();
            int middle = paths.size() / 2;
            for(int i = 0; i < middle; i++)
            {
                left.add(paths.get(i));
            }
    
            for(int i = middle; i < paths.size(); i++)
            {
                right.add(paths.get(i));
            }
    
            left = mergeSort(left);
            right = mergeSort(right);
    
            merge(left, right, paths);
        }
        return paths;
    }
    
    public void merge(ArrayList<String> left, ArrayList<String> right, ArrayList<String> paths)
    {
        int leftNum = 0;
        int rightNum = 0;
        int pathsNum = 0;
        while (leftNum < left.size() && rightNum < right.size())
        {
            if ((left.get(leftNum).compareTo(right.get(rightNum)))<0) 
            {
                paths.set(pathsNum, left.get(leftNum));
                leftNum++;
            }
            else
            {
                paths.set(pathsNum, right.get(rightNum));
                rightNum++;
            }
            pathsNum++;
        }
    
        ArrayList<String>rest;
        int restNum;
        if (leftNum >= left.size()) 
        {
            rest = right;
            restNum = rightNum;
        }
        else 
        {
            rest = left;
            restNum = leftNum;
        }
    
        for (int i = restNum; i < rest.size(); i++) 
        {
            paths.set(pathsNum, rest.get(i));
            pathsNum++;
        }
    
    }
    public void readInput()
    {
        Scanner input = new Scanner(System.in);
        System.out.println("Please enter a list of path names.(press enter after each path name, and type \"stop\" once you are finished.");
        String termination = "stop";
        boolean reading = true;
        while(!(input.nextLine().equals(termination)))
        {
            String in = input.nextLine();
           pathNames.add(in);
        
        }
    }
    }
    This is my main method.
    Java Code:
    public class FileNamePrgm 
    {
        public static void main(String [] args)
        {
            FileName paths = new FileName();
            paths.readInput();  
            paths.sort();
        }   
    }
    Last edited by JosAH; 03-11-2016 at 09:21 AM. Reason: added [code] ... [/code] tags

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,012
    Rep Power
    35

    Default Re: Sort an array list of pathnames in java.

    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,995
    Rep Power
    10

    Default Re: Sort an array list of pathnames in java.

    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    15

    Default Re: Sort an array list of pathnames in java.

    Quote Originally Posted by javakaz View Post
    Can anyone tell me what is wrong with my code?
    Yes, eventually. But what attempts have you made to figure it out yourself? I don't see any debugging statements in your code.
    When you create an algorithm you expect the code to be doing certain things at certain times. So you should sprinkle some print
    statements thru out your code to see if it is performing as expected.

    Check input, output and stuff in between.

    And to make your testing easier, I recommend you create a readInputTest() method which reads in a file of pathnames. Then you won't have to enter them by hand each time you want to test the program.

    After trying my suggestions if you are still having problems, we will be here.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. How to use mergesort to sort a list of names?
    By cheezncrackers in forum New To Java
    Replies: 1
    Last Post: 03-27-2012, 02:22 PM
  2. need help to sort a list
    By gretaks in forum New To Java
    Replies: 13
    Last Post: 11-02-2011, 10:07 PM
  3. bucket sort, list in java
    By someone in forum New To Java
    Replies: 10
    Last Post: 04-11-2011, 11:13 PM
  4. Insertion Sort for linked list help?
    By bubtub24 in forum New To Java
    Replies: 3
    Last Post: 11-28-2010, 06:21 AM
  5. How to sort a list using Bubble sort algorithm
    By Java Tip in forum Algorithms
    Replies: 3
    Last Post: 04-29-2008, 08:04 PM

Tags for this Thread

Posting Permissions

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