Results 1 to 11 of 11
  1. #1
    Join Date
    Mar 2012
    Location
    Dublin
    Posts
    21
    Rep Power
    0

    Default Sorting text in imported text file

    Hi,

    I'm new to this site so forgive any errors in posting :)

    I have to sort a text file in alphabetical order. The text file is in "first name, surname, phone number" order and I have to rearrange it to "surname, first name phone number"

    here's what I have all ready
    Java Code:
    class Directory{
    	
    		@SuppressWarnings("deprecation")
    		public static void main(String[] args){
    			File file = new File("c:\\Names.txt");
    			FileInputStream files = null;
    			DataInputStream data = null;
    			try{
    				files = new FileInputStream(file);
    				data = new DataInputStream(files);
    		
    				while (data.available() != 0) {
    					System.out.println(data.readLine());
    				}
    		 
    				files.close();
    				data.close();
    			}
    			catch (IOException k) {
    				k.printStackTrace();
    			}
    	
    		}
    }
    Any help would be greatly appreciated
    Last edited by JosAH; 03-23-2012 at 06:22 PM. Reason: added [code] ... [/code] tags

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,458
    Rep Power
    25

    Default Re: Sorting text in imported text file

    Look at the String class for methods to break the line up into separate parts. The split() might be useful, or indexOf and substring
    Once you have the parts, you can put them back together in the order that you want them.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    Join Date
    Mar 2012
    Location
    Dublin
    Posts
    21
    Rep Power
    0

    Default Re: Sorting text in imported text file

    would i be better off making a separate class for that? I was thinking of using Token and split to separate and read the individual parts

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,458
    Rep Power
    25

    Default Re: Sorting text in imported text file

    You could make a method (not a class) to do the splitting, but it's not needed to solve the problem.
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    Join Date
    Mar 2012
    Location
    Dublin
    Posts
    21
    Rep Power
    0

    Default Re: Sorting text in imported text file

    I'v to use classes for this Assignment even though they aren't needed

    here's the question
    The program must use a class Person to represent the idea of a person, with methods to read a person’s details, print a person’s details, etc. Inputs of up to 1000 lines should be catered for, but the number of lines is not known in advance.

    this has to be brought into a class directory
    Last edited by ManInTheMiddle; 03-23-2012 at 07:08 PM.

  6. #6
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,458
    Rep Power
    25

    Default Re: Sorting text in imported text file

    Use an arraylist to hold the Person class objects. It has no limit to the number of elements it can contain.
    If you don't understand my response, don't ignore it, ask a question.

  7. #7
    Join Date
    Mar 2012
    Location
    Dublin
    Posts
    21
    Rep Power
    0

    Default Re: Sorting text in imported text file

    and how would I import the Names.txt data into an Array list?

  8. #8
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,458
    Rep Power
    25

    Default Re: Sorting text in imported text file

    Read the file line by line, parse each line into its parts, create a new instance of the class using those parts, add that instance to the arraylist.
    If you don't understand my response, don't ignore it, ask a question.

  9. #9
    Join Date
    Mar 2012
    Location
    Dublin
    Posts
    21
    Rep Power
    0

    Default Re: Sorting text in imported text file

    I have it reading line by line in the main code so I'll just take that out of Directory main and put it into class Person then parse each part, yeah?

  10. #10
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,458
    Rep Power
    25

    Default Re: Sorting text in imported text file

    You could call the Person class's constructor with the line that is read from the file. The constructor could parse the line into its parts.
    The main() method reads the file, the Person class parses the line.
    If you don't understand my response, don't ignore it, ask a question.

  11. #11
    Join Date
    Mar 2012
    Location
    Dublin
    Posts
    21
    Rep Power
    0

    Default Re: Sorting text in imported text file

    Cheers dude, I'll get cracking on that.. Thanks for the time n help...

Similar Threads

  1. Replies: 8
    Last Post: 07-17-2011, 02:38 PM
  2. Replies: 2
    Last Post: 04-27-2011, 04:09 PM
  3. Replies: 7
    Last Post: 03-05-2011, 09:25 AM
  4. Replies: 0
    Last Post: 12-09-2010, 05:56 PM
  5. find and replace text from a text file
    By gezzel in forum New To Java
    Replies: 2
    Last Post: 09-19-2008, 04: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
  •