Results 1 to 5 of 5

Thread: beginner

  1. #1
    Eiolvit is offline Member
    Join Date
    Jul 2011
    Posts
    1
    Rep Power
    0

    Default beginner

    Hi!
    I am very young and very inquisitive!
    I the beginning programmer.
    My program
    (Sorting alphabetically)
    input file:
    ccc
    ddd
    bbb
    ddd
    aaa

    output file:
    aaa
    bbb
    ccc
    ddd
    ddd


    How to make so that in a target file there were no repetitions?
    that was like this:
    aaa
    bbb
    ccc
    ddd


    Help please!)
    Java Code:
     
    class Customer implements Comparable<Customer> {
      
    	private String sequance;
       
       public Customer() {
    	   sequance = "ppp";
    	     }
       
       public Customer(String string) {
       }
     
       public Customer(String N, String S) {
          this();
          sequance = N;
       }
     
       public String getName() {
          return sequance;
       }
     
       public void setName(String letters) {
          this.sequance = letters;
       }
     
      
     
       public void print() {
          System.out.println(sequance + "\t"  );
       }
     
       @Override
       public int compareTo(Customer o) {
          return this.sequance.compareTo(o.sequance);
       }
     
    }
     
    public class Main {
       public static void main(String[] args) {
          Customer[] cust = new Customer[10];
          String name[] = { "ccc", "ddd", "bbb", "ddd", "aaa", "Alex",
                "Andrej", "Oleg", "Ivan", "Egor" };
          int i;
     
          System.out.println("All customers:");
     
          for (i = 0; i < cust.length; i++) {
             cust[i] = new Customer();
             cust[i].setName(name[i]);
             cust[i].print();
          }
     
    System.out.println("Sorted alphabetically: ");
    char alph[]={'a', 'b', 'c', 'd', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'};  
     
        for (int x=0; x<alph.length; x++)
        {for (i=0; i<cust.length; i++)
        {
    if (cust[i].getName().charAt(0)==alph[x])   
    cust[i].print();
        }
      }
     
       }
    }

  2. #2
    Dark's Avatar
    Dark is offline Senior Member
    Join Date
    Apr 2011
    Location
    Camp Lejuene, North Carolina
    Posts
    643
    Rep Power
    4

    Default

    Use the array's sort method.

    Arrays (Java Platform SE 6)
    ArrayList (Java Platform SE 6)

    Its much simpler, and you can look at examples here: sorting strings into alphabetical order - Java

    Ignore the second post, its not really worth reading.
    • Use [code][/code] tags when posting code. That way people don't want to stab their eyes out when trying to help you.
    • +Rep people for helpful posts.

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,785
    Blog Entries
    7
    Rep Power
    21

    Default

    Even better: use a TreeSet because you don't want any duplicates.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  4. #4
    Dark's Avatar
    Dark is offline Senior Member
    Join Date
    Apr 2011
    Location
    Camp Lejuene, North Carolina
    Posts
    643
    Rep Power
    4

    Default

    If this is a class project/homework he might not be able to go and use things that haven't learned about yet. However if its not then I agree with JosAH, look into that.

    If it is a class assignment you could check to see if the entry already exists by looping through your array and use a .equalsIgnoreCase() to compare if a string has already been entered.
    • Use [code][/code] tags when posting code. That way people don't want to stab their eyes out when trying to help you.
    • +Rep people for helpful posts.

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,457
    Rep Power
    20

Similar Threads

  1. JNI beginner
    By M77 in forum Advanced Java
    Replies: 2
    Last Post: 04-04-2011, 01:53 AM
  2. Need Help - Beginner
    By ooooohmaul in forum New To Java
    Replies: 4
    Last Post: 08-08-2010, 05:22 AM
  3. Beginner, need a little help
    By jimmy-lin in forum New To Java
    Replies: 6
    Last Post: 10-10-2009, 02:00 AM
  4. Beginner needs help!
    By Polyy in forum New To Java
    Replies: 1
    Last Post: 11-27-2008, 06:12 AM
  5. almost done...beginner needs help plz..
    By shongo in forum New To Java
    Replies: 15
    Last Post: 11-10-2008, 09:14 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
  •