Results 1 to 6 of 6
  1. #1
    beandip408 is offline Member
    Join Date
    Sep 2010
    Posts
    10
    Rep Power
    0

    Default Alphebetizing using arrays

    im obviously right at the starting point and need to figure out how to read the following code and:

    sort them in alphabetical order (by first and then last name), and
    print them to an output file (you can hardcode the name or prompt the user for a name) and using Arrays to do this.

    here is what the file contains:
    Java Code:
    Minnie Mouse
    Scrooge McDuck
    Donald Duck
    Mickey Mouse
    Max Goof
    and this is how it needs to come out and be printed into another file:
    Java Code:
    Donald Duck
    Max Goof
    Mickey Mouse
    Minnie Mouse
    Scrooge McDuck
    here is what i have so far:
    Java Code:
    import java.util.Scanner;
    import java.net.MalformedURLException;
    import java.net.URL;
    import java.net.URLConnection;
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.IOException;
    
    public class sorting {
    
    	public static void main(String[] args) throws IOException, MalformedURLException {
    
    		Scanner input = new Scanner(System.in);
    		System.out.println("Please enter the name of the Sorting file: ");
    		String file = input.next();
    		File sort = new File(file);
    		Scanner scan = new Scanner(sort);
    
    
    
    		while (scan.hasNextLine()) {
    			String line = scan.nextLine();
    
    
    		}
    
    	}
    }
    i am familiar with the layout of an array for numbers but not strings
    ( Array name Keyword Type Size semicolon)
    data = new double [10] ;

    any thoughts on how i can implement arrays to make this work?

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

  3. #3
    beandip408 is offline Member
    Join Date
    Sep 2010
    Posts
    10
    Rep Power
    0

    Default

    Quote Originally Posted by Eranga View Post
    Put them in an ArrayList, with generics. Collection provide sort() to handle it.
    that sounds very smart and simple... apparently my brain is a little less smart and more simple. is there an example that you could show me?

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    Of course I don't have a one, but I can give you the guidelines.

    1. Initialize an ArrayList, if you do not use generics compiler will give you a waning on 1.5 and on.

    2. From the source, in you case command line parameters, invoke add() method on the ArrayList you've initialized above. Keep in mind, MUST added as an object, not as primitives. (anyway in your case it's all objects)

    3. Collections.sort() will do the job. Import necessary packages as well.

    Give a try and let me know if you are stuck on anything.

  5. #5
    m00nchile is offline Senior Member
    Join Date
    Feb 2010
    Location
    Ljubljana, Slovenia
    Posts
    470
    Rep Power
    5

    Default

    Also, sorting Strings is no different than sorting numbers. And although Collections.sort() is a legitimate answer, I'd reccomend implementing a sorting algorithm on your own. About comparing Strings, have a look at this:
    Java Code:
    public class StringComparison {
      public static void main(String[] args) {
        String s1 = "a", s2 = "b";
        System.out.println(s1.compareTo(s2)+" "+ s2.compareTo(s1));
      }
    }
    Instead of the regular a > b, your checks will look like this:
    Java Code:
    if(a.compareTo(b) < 0) //sort accordingly
    Ever seen a dog chase its tail? Now that's an infinite loop.

  6. #6
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    True, however I don't think customize solutions for this kind of issues are not really necessary. But if you can have your own solutions, it's really nice. So that the complete control is with you. Which I really like to move with. :)

Similar Threads

  1. store array of arrays in array of arrays
    By joost_m in forum New To Java
    Replies: 4
    Last Post: 04-19-2010, 10:32 AM
  2. Arrays.sort... why sorting all arrays in class?
    By innspiron in forum New To Java
    Replies: 6
    Last Post: 03-23-2010, 01:40 AM
  3. Arrays
    By karabo101 in forum New To Java
    Replies: 12
    Last Post: 10-11-2009, 05:02 PM
  4. Need help with Arrays
    By dietgal in forum New To Java
    Replies: 21
    Last Post: 10-08-2008, 01:59 PM
  5. Help with Arrays
    By bri1547 in forum New To Java
    Replies: 4
    Last Post: 08-01-2008, 05:12 AM

Posting Permissions

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