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: Simple sorting program, can't figure out whats wrong with my code

  1. #1
    Junior Member
    Join Date
    Nov 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Simple sorting program, can't figure out whats wrong with my code

    Program description:

    The purpose of this assignment is to give you experience in both Java, as well as in the design of algorithms for sorting different kinds of data. The system you will construct will consist of a window that contains a title, a table with two columns, and two buttons, as shown here. The first column of the table will contain the names of people and the second column will contain their corresponding ages. The table will initially be empty and a user will be able to type values into it. One button will cause the entries in the table to be sorted based on the name in column 1 and the second button will cause the entries to be sorted based on the ages in column 2.

    my code:
    //Class that only contains name and age variables
    public class Person {

    String Name;
    int Age;


    public Person(String name, int age) {
    this.Name = name;
    this.Age = age;
    }
    public static void main(String[] args) {
    // TODO Auto-generated method stub

    }

    }



    /////////////////////////////////////////////////////////////
    import java.awt.FlowLayout;
    import java.awt.GridLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;

    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JTextField;


    public class List extends JFrame implements ActionListener {

    private JButton btnName;
    private JButton btnAge;
    private JPanel roster, list, button;
    private JTextField[] name,age;
    private Person[] people;

    public List(){
    super("Roster");
    setupComps();
    setupPanels();
    setupFrame();

    }

    private void setupComps(){
    btnName = new JButton("Sort by Name");
    btnName.addActionListener(this);
    btnAge = new JButton("Sort by Age");
    btnAge.addActionListener(this);

    name = new JTextField[6];
    age = new JTextField[6];

    for(int i = 0; i<6; i++){
    name[i] = new JTextField(12);
    age[i] = new JTextField(5);
    }
    }

    private void setupPanels(){
    roster = new JPanel(new FlowLayout());
    roster.add(new JLabel("ROSTER"));

    list = new JPanel(new GridLayout(7,2));
    list.add(new JLabel("Name"));
    list.add(new JLabel("Age"));

    for(int i = 0; i < 6; i++){
    list.add(name[i]);
    list.add(age[i]);
    }

    button = new JPanel(new GridLayout(1,2));
    button.add(btnName);
    button.add(btnAge);
    }

    private void setupFrame(){
    setSize(800,600);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setLayout(new FlowLayout());

    setVisible(true);

    add(roster);
    add(list);
    add(button);

    }

    private void getData(){
    for(int i = 0; i < 6; i++){
    people[i] = new Person(name[i].getText(), Integer.parseInt(age[i].getText()));
    }
    }

    private void setData(){
    for(int i = 0; i < 6; i++){
    name[i].setText(people[i].Name);
    age[i].setText(Integer.toString(people[i].Age));
    }
    }

    private void sortAge(){
    boolean swapped = true;
    int j = 0;
    Person tmp;
    while(swapped){
    swapped = false;
    j++;
    for(int i = 0; i < people.length - j; i++){
    if(people[i].Age > people[i+1].Age){
    tmp = people[i];
    people[i+1] = tmp;
    swapped = true;
    }
    }
    }


    }
    private void sortName(){
    boolean swapped = true;
    int j = 0;
    Person tmp;
    while(swapped){
    swapped = false;
    j++;
    for(int i = 0; i < people.length - j; i++){
    if(people[i].Name.compareTo(people[i+1].Name) > 0){
    tmp = people[i];
    people[i+1] = tmp;
    swapped = true;
    }
    }
    }


    }
    public static void main(String[] args) {
    // TODO Auto-generated method stub

    List names = new List();

    }

    //clicked button Event
    @Override
    public void actionPerformed(ActionEvent e) {
    getData();
    String Command = e.getActionCommand();
    if(Command.contains("Name")){
    sortName();
    } else if(Command.contains("Age")){
    sortAge();
    }

    setData();

    }

    }


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Simple sorting program, can't figure out whats wrong with my code

    Please use the highlight tags when posting code.

    Also, you've posted a bunch of code and your assignment, but no explanation of what's going on or what your question is. What do you expect this code to do? What does it do instead? What line exhibits the incorrect behavior? Have you stepped through this with a debugger to figure out what's going on?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. can't figure whats wrong with my add method?? help!
    By b094mph in forum What's Wrong With My Code?
    Replies: 4
    Last Post: October 8th, 2011, 05:00 PM
  2. whats wrong with my code.
    By jove in forum Object Oriented Programming
    Replies: 3
    Last Post: July 30th, 2011, 11:45 PM
  3. Whats wrong with my code!!
    By nitwit3 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: July 22nd, 2011, 11:45 AM
  4. Whats wrong with my code?
    By whattheeff in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 4th, 2011, 05:34 PM
  5. whats wrong with my program??
    By jrodriguo in forum What's Wrong With My Code?
    Replies: 4
    Last Post: May 11th, 2010, 06:16 AM