Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Sorting substring?

  1. #1
    Junior Member
    Join Date
    Apr 2010
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Sorting substring?

    Here is my code:

    import java.util.Arrays;
     
    import javax.swing.*;
     
    public class childNameAge {
    	public static void main(String[] args) {
    		int numberOfChildren;
    		String indata;
     
    		indata = JOptionPane.showInputDialog(null, "How many children do you want to register?");
    		numberOfChildren = Integer.parseInt(indata);
     
    		String[] nameOfChild = new String[numberOfChildren + 1];
     
    		for (int i = 1; i <= numberOfChildren; i++) {
    			nameOfChild[i] = JOptionPane.showInputDialog(null, "Personal numer and name " + i + "\nFormat: num-name");
    		}
    		for (int i = 1; i <= numberOfChildren; i++) {
    			System.out.println(nameOfChild[i]);
    		}
    		System.out.println("Picking out year of birth...");
    		for(int i=1; i<=numberOfChildren; i++){
     
    		System.out.println(Integer.parseInt(nameOfChild[i].substring(0, 2)));
    		}
    	}
    }

    Now I want to sort the array, the biggest number last och and the smallest first. Then I want to connect the age to the right name again and print out childrens year of birth and name.


  2. #2
    Super Moderator Sean4u's Avatar
    Join Date
    Jul 2011
    Location
    Tavistock, UK
    Posts
    637
    Thanks
    5
    Thanked 103 Times in 93 Posts

    Default Re: Sorting substring?

    I'm guessing that what you're doing is building a *map* of child-name -> year of birth, right? No, that's not right, you've got id numbers in there - is that because two children may have the same name? So the map is id -> (name, yob) where 'yob' is year-of-birth, right? You can 'connect' the name to the yob by making a class 'ChildYOB' (for example) with two data members: a name and a year.

    You could read in all your values, and *put* each id->ChildYOB in a java.util.TreeMap. TreeMap arranges its keys in 'natural order', so for integers that's small-to-large, but it has methods starting "descending..." which would give you large-to-small. When you retrieve ChildYOB values from the TreeMap, the names would be already 'connected' to year of birth because the pair would be encapsulated by the class you've written.

    That sounds like a lot of work but the classes in the Java API - once you get to know them - make programming something like this very easy, requiring very few lines of code. If you were to try to do this without using the Java API by using arrays of primitive types, you'd end up with a much larger, much harder to understand and debug program.

Similar Threads

  1. replacement substring
    By dcshoecousa in forum What's Wrong With My Code?
    Replies: 7
    Last Post: March 9th, 2011, 04:28 PM
  2. SUBSTRING
    By cutee_eyeh in forum Object Oriented Programming
    Replies: 1
    Last Post: November 12th, 2010, 03:57 AM
  3. Substring help
    By Roach in forum What's Wrong With My Code?
    Replies: 2
    Last Post: September 21st, 2010, 07:57 AM
  4. [SOLVED] replacing subString
    By nasi in forum What's Wrong With My Code?
    Replies: 1
    Last Post: May 21st, 2010, 09:47 PM
  5. First string is a substring of another
    By mgutierrez19 in forum What's Wrong With My Code?
    Replies: 13
    Last Post: October 21st, 2009, 10:35 PM