Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Help with sorting array!
Index -> Programming, Java -> Java Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
an_mui




PostPosted: Thu Jan 06, 2005 12:18 am   Post subject: Help with sorting array!

Hi I am kind of new to programming, and I have to make a program that will keep track of an user's collection. I need help on making a menu that will allow the user to choose what to do next. I know first I have to accept input from user, and then user ifs and else ifs? Any help is appreciated

Java:

System.out.println ("Welcome To My Program");
System.out.println ("Please choose what you want to do (1, 2, 3, 4)");
System.out.println ("1. Input information for your collection");
System.out.println ("2. Edit your collection");
System.out.println ("3. View your collection");
System.out.println ("4. Leave");

InputStreamReader inputRead = new InputStreamReader (System.in);
BufferedReader bufRead = new BufferedReader (inputRead);

String choice = "";

try
{
System.out.print ("Enter Choice: ");
choice = bufRead.readLine ();
}

catch (IOException err)
{
System.out.println ("Error Reading Line");
}

// do what the user chooses to do.


- Annie

EDIT (Martin): Just changed the tags to syntax ones to try out the new system. Carry on. Very Happy
Sponsor
Sponsor
Sponsor
sponsor
wtd




PostPosted: Thu Jan 06, 2005 12:48 am   Post subject: (No subject)

In psuedo-code:


  1. Welcome
  2. Display menu.
  3. Accept input.
  4. If input is not one of the choices, go to step 2.
  5. Test to see what the input is, and do different things depending on that.
an_mui




PostPosted: Mon Jan 10, 2005 9:18 pm   Post subject: (No subject)

Thanks, the help is appreciated =)
an_mui




PostPosted: Tue Jan 11, 2005 5:08 pm   Post subject: (No subject)

Sorry but I've encountered any problem.

For my program, I have 4 arrays to store four types of data that the user entered. The tutorials provided on this web site only deal with sorting one array. However.. for my colleciton, if I sort one array, then I must sort the other array in the same order.. or else the data won't match up.

Sorry if I am not explaining myself clearly.. here's what I meant

Sample Input:

1: 1998, eric, tang
2. 2000, chris, boyd
3. 2003, stephanie, mac

If i sort according to first name alphabetically, then Chris would become associated with 1998, and tang.. which is not true. How would I make my program so that if i sort last name / first name alphabetically.. the other entries would also move accordingly?

Thank you for the help
wtd




PostPosted: Tue Jan 11, 2005 6:09 pm   Post subject: (No subject)

Don't try to sort four arrays in exactly the same way. Simply have one array of objects. You can create a class which holds all of your data. Let's say I have people, and I want to store their names and ages.

Now, I can create a Person class. If I implement the Comparable interface, I'll be ably to sort my array using the Arrays.sort method.

Java:
import java.lang.*;
import java.io.*;
import java.util.*;

class Person implements Comparable
{
   private String name;
   private int age;

   public Person(String name, int age)
   {
      this.name = name;
      this.age = age;
   }

   public String getName()
   {
      return name;
   }

   public int getAge()
   {
      return age;
   }

   public int compareTo(Object o)
   {
      Person p = (Person)o;
      int comparison = name.compareTo(p.getName());

      if (comparison == 0)
      {
         int otherComp = new Integer(age).compareTo(new Integer(p.getAge()));
         return otherComp;
      }
      else
      {
         return comparison;
      }
   }

   public String toString()
   {
      return name + " " + age;
   }
}

public class SortTest
{
   public static void main(String[] args)
   {
      Person[] people = new Person[4];
      people[0] = new Person("Bob", 45);
      people[1] = new Person("John", 23);
      people[2] = new Person("Arnold", 18);
      people[3] = new Person("Bob", 16);

      Arrays.sort(people);

      for (int i = 0; i < 4; i++)
         System.out.println(people[i]);
   }
}
an_mui




PostPosted: Thu Jan 13, 2005 8:09 pm   Post subject: (No subject)

hm.. I've been havnig difficulties sorting string.. can you help me once more?
wtd




PostPosted: Thu Jan 13, 2005 8:26 pm   Post subject: (No subject)

If you're just sorting an array of strings, that's incredibly easy.

Java:
String[] foo = {"hello","world","foo","bar"};

Arrays.sort(foo);
Nezura




PostPosted: Thu Jan 20, 2005 12:48 am   Post subject: (No subject)

wtd wrote:
If you're just sorting an array of strings, that's incredibly easy.

Java:
String[] foo = {"hello","world","foo","bar"};

Arrays.sort(foo);


With Arrays.sort does it sort the foo array ...but doesn't output it does it?...how will we make it output the new sorted array?...loops?
Sponsor
Sponsor
Sponsor
sponsor
wtd




PostPosted: Thu Jan 20, 2005 1:45 am   Post subject: (No subject)

Nezura wrote:
wtd wrote:
If you're just sorting an array of strings, that's incredibly easy.

Java:
String[] foo = {"hello","world","foo","bar"};

Arrays.sort(foo);


With Arrays.sort does it sort the foo array ...but doesn't output it does it?...how will we make it output the new sorted array?...loops?


It just sorts the array. Output the array the same way you would normally. Yes, with a loop.
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 9 Posts ]
Jump to:   


Style:  
Search: