Page 1 of 2 12 LastLast
Results 1 to 20 of 27
  1. #1
    Veronicad1 is offline Member
    Join Date
    May 2011
    Posts
    9
    Rep Power
    0

    Default Got myself in a bind trying to sort names with GUI entry

    I hate to be a bother - I have to write a Java Program by Monday that does the following in a JFrame (GUI) format and I don't know where to start. Here's what I need to do, if it's possible:

    * I need to be able to input first and last name from a GUI window - more than one set of first/last name.

    *Have to have the ability to click on a button to sort by first or last name and/or ascending/descending.

    I can get a bubble sort of an array to do that for me with a first name only but not in a GUI. I can't get past one name in GUI never mind a last name. This is what I have so far:

    import java.util.*;
    import javax.swing.*;
    //import java.awt.event.*;
    public class TestArray1 extends JFrame
    {
    public static void main(String[] args)
    {
    String[] Array;
    Array=new String[3];
    String temp;

    Scanner input=new Scanner(System.in);

    for(int i=0;i<3;i++)
    {
    JOptionPane.showInputDialog(null,
    "Enter a first name ", "Name Box", JOptionPane.INFORMATION_MESSAGE);
    Array[i]=input.next();

    }

    for(int x=1;x<3;x++)
    {
    for(int y=0;y<3-x;y++)
    {
    if(Array[y].compareTo(Array[y+1])>0)
    {
    temp=Array[y];
    Array[y]=Array[y+1];
    Array[y+1]=temp;

    }
    }
    }


    I'm pretty embarrassed by it - it's horrible and a hash of all of it. I have a book and Google to do this with - must have been out of my mind to say I would. HELP!!!

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    First and foremost, get rid of the Scanner object as it has no business here other than to confuse you. Next of all, were you given any code to get you started or sample code of how to make GUI's. Are you supposed to use JOptionPanes to get data or something else? Can you post the entire assignment? Oh and when posting code, please use code tags so your code will be readable (please read the link at the bottom for more).

  3. #3
    Veronicad1 is offline Member
    Join Date
    May 2011
    Posts
    9
    Rep Power
    0

    Default

    Sorry about that - okay it's not so much an assignment as my alligator mouth overloaded my hummingbird butt. But I DO have 3 - 4 hour finals in engineering physics and other fun stuff this weekend. Years ago I did some of this and thought with a book and Google I could just do this. WOW was I wrong! I found the sorting code here and tweaked it just with the book to get this far. And it compiled but of course it takes one name and goes into never-never land.

    What I need to do is extend JFrame and implement an Applet maybe(?) so it creates a Window - a GUI I would say. That way the input would go into a box in the window, save each of the firstName and lastName (right?) then it would put it into an Array to sort it. I've been looking over the book and I was curious would I use the "Add" for the array first - like
    PHP Code:
    public void add()
    or am I just making this too hard? What he wants is to be able to input names (like about 20 but I just was testing 3) and then sort them by their First names and then if he wants to by last names and if he wants in reverse, etc. If it were some old legacy language I could do it - maybe - been a long time. Thanks for your help. Really put my foot in this one.

    PHP Code:
    import java.util.*;
    import javax.swing.*;
    //import java.awt.event.*;
    public class TestArray1 extends JFrame
    {
    //	public static void main(String[] args)
    	{
    		String[] Array;
    		Array=new String[3];
    		String temp;
    		
    		Scanner input=new Scanner(System.in);
    			
    		for(int i=0;i<3;i++)
    		{
    			JOptionPane.showInputDialog(null,
    			"Enter a first name ", "Name Box", JOptionPane.INFORMATION_MESSAGE);
    			Array[i]=input.next();
    		
    			}
    		
    	for(int x=1;x<3;x++)
    		{
    		for(int y=0;y<3-x;y++)
    			{
    			if(Array[y].compareTo(Array[y+1])>0)
    				{
    				temp=Array[y];
    				Array[y]=Array[y+1];
    				Array[y+1]=temp;
    				
    				}
    				}
    				}

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by Veronicad1 View Post
    What I need to do is extend JFrame and implement an Applet maybe(?)
    Makes little sense. Usually you need either you need a JFrame or a JApplet, and you can't implement an applet only extend it.


    so it creates a Window - a GUI I would say. That way the input would go into a box in the window, save each of the firstName and lastName (right?) then it would put it into an Array to sort it. I've been looking over the book and I was curious would I use the "Add" for the array first - like
    PHP Code:
    public void add()
    You're not making much sense. What does the actual assignment say?

    or am I just making this too hard? What he wants is to be able to input names (like about 20 but I just was testing 3) and then sort them by their First names and then if he wants to by last names and if he wants in reverse, etc. If it were some old legacy language I could do it - maybe - been a long time. Thanks for your help. Really put my foot in this one.
    [/quote]

    Break the problem down and solve one problem at a time, not the whole thing. First get the input and print it out to make sure you're getting it right. But again, we need the assignment text.



    PHP Code:
    import java.util.*;
    import javax.swing.*;
    //import java.awt.event.*;
    public class TestArray1 extends JFrame
    {
    //	public static void main(String[] args)
    	{
    		String[] Array;
    		Array=new String[3];
    		String temp;
    		
    		Scanner input=new Scanner(System.in);
    			
    		for(int i=0;i<3;i++)
    		{
    			JOptionPane.showInputDialog(null,
    			"Enter a first name ", "Name Box", JOptionPane.INFORMATION_MESSAGE);
    			Array[i]=input.next();
    		
    			}
    		
    	for(int x=1;x<3;x++)
    		{
    		for(int y=0;y<3-x;y++)
    			{
    			if(Array[y].compareTo(Array[y+1])>0)
    				{
    				temp=Array[y];
    				Array[y]=Array[y+1];
    				Array[y+1]=temp;
    				
    				}
    				}
    				}
    Why do you still have a Scanner object in that code? Please re-read my first post and if you don't understand something, please ask.

  5. #5
    Veronicad1 is offline Member
    Join Date
    May 2011
    Posts
    9
    Rep Power
    0

    Default

    Okay - I got a break from the guy to finish by tomorrow night so I can finish my final exams today.

    I Googled Bubble Sort Arrays using JTextFile and I think this guy's code will work if I change it to a string but the problem is he had an issue with the bubble sort too. I found the example he used and guess what? It wouldn't compile in JGrasp (which is really nice to use to edit I found out too) either. So what's wrong with the Bubble Sort here? If someone can see it please let me know so I can fix it and then make the changes so my friend can use it. Thanks!

    PHP Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.ActionListener;
    import java.awt.event.ActionEvent;
    
    public class NumberSorter extends JFrame implements ActionListener{
    
    private JTextField tf1, tf2, tf3, tf4, tf5;
    private JButton b1;
    private JLabel l1, l2;
    private JPanel panel;
    private JTextArea outputArea;
    int Numbers[]= new int[5];
    
    public NumberSorter() {
    
    super("Number Sorter");
    Container contentPane = getContentPane();
    
    
    //=========================================================================================================
    //	 Create Panel where components are to be stored
    //=========================================================================================================
    
    
    JPanel panel = new JPanel();
    panel.setBackground(Color.pink);//Change background colout to pink
    //panel.setLayout(new GridLayout(5,6 ) );//set layout to GridLayout
    //==========================================================================================================
    //	 Create Label to give instructions to user
    //==========================================================================================================
    
    JLabel label1 = new JLabel("Please enter a number in each field and click on Bubble Sort Button to sort");
    label1.setVerticalAlignment(JLabel.TOP);
    
    
    //============================================================================================================
    //	 Create TextField area for Output
    //============================================================================================================
    
    
    outputArea = new JTextArea(10,25);
    outputArea.setEditable(false);//Do not allow Textarea to be edited.
    outputArea.setLineWrap(true);//allow wrapping of text
    
    
    //=============================================================================================================
    //	 Create five TextFields	& One Button and Add Action Listeners
    //=============================================================================================================
    
    tf1 = new JTextField(5);
    //Align text to center of textfield
    tf1.setHorizontalAlignment(JTextField.CENTER);
    // Initialize the text of the field
    tf1.setText(" ");
    tf1.addActionListener(this );
    
    
    tf2 = new JTextField(5);
    tf2.setHorizontalAlignment(JTextField.CENTER);
    tf2.setText(" ");
    tf2.addActionListener(this );
    
    
    
    
    tf3 = new JTextField(5);
    tf3.setText(" ");
    tf3.setHorizontalAlignment(JTextField.CENTER);
    tf3.addActionListener(this );
    
    
    
    
    tf4 = new JTextField(5);
    tf4.setHorizontalAlignment(JTextField.CENTER);
    tf4.setText(" ");
    tf4.addActionListener(this );
    
    
    
    tf5 = new JTextField(5);
    tf5.setHorizontalAlignment(JTextField.CENTER);
    tf5.setText(" ");
    tf5.addActionListener(this );
    
    
    
    //Create JButton
    
    JButton b1 = new JButton("BUBBLE SORT");
    b1.addActionListener(this );
    
    //=================================================================================================
    // Add Label, TextFields, TextArea and Button to Panel.
    //=================================================================================================
    panel.add(label1);
    panel.add(tf1);
    panel.add(tf2);
    panel.add(tf3);
    panel.add(tf4);
    panel.add(tf5);
    panel.add(b1);
    panel.add( new JScrollPane(outputArea), BorderLayout.CENTER );
    
    //=================================================================================================
    //	 Add the panel to the content pane and set window properties
    //==================================================================================================
    
    contentPane.add(panel);
    
    setSize(450,300);
    setVisible( true );
    
    }//end of Sorter constructor
    
    //===================================================================================================
    //	 Use String Method to send Bubblesort array to TextArea
    //===================================================================================================
    
    
    
    public void actionPerformed( ActionEvent e ){
    
    String s1="", s2="", s3="", s4="", s5="";
    
    
    
    
    s1=tf1.getText();
    Numbers[0]= Integer.parseInt(s1.trim()); //add FIRST number to array here
    s2=tf2.getText();
    Numbers[1]= Integer.parseInt(s2.trim()); //add SECOND number to array here
    s3=tf3.getText();
    Numbers[2]= Integer.parseInt(s3.trim()); //add THIRD number to array here
    s4=tf4.getText();
    Numbers[3]= Integer.parseInt(s4.trim()); //add FOURTH number to array here
    s5=tf5.getText();
    Numbers[4]= Integer.parseInt(s5.trim()); //add FIFTH number to array here
    
    
    
    String output = ("Numbers in original order:\n");
    
    // append array values to String output
    
    for ( int counter = 0; counter < 5; counter++ ){
    
    output += Integer.toString( Numbers[counter])+ " ";
    
    
    outputArea.setText( output );//output numbers to TextArea
    
    
    
    }//end of for loop
    
    
    
    //====================================================================================================
    //	 BubbleSort Numbers in Array and Display to TextArea
    //====================================================================================================
    
    if (e.getActionCommand()=="BUBBLE SORT"){ // test if Bubble Sort button was selected
    
    
    int Numbers[]= new int[5];
    
    String output = "Data items in original order\n";
    
    // append original array values to String output
    
    for ( int counter = 0; counter < array.length; counter++ )
    
    output += " " + array[ counter ];
    
    bubbleSort( array ); // sort array
    
    output += "\n\nData items in ascending order\n";
    
    // append sorted\ array values to String output
    
    for ( int counter = 0; counter < array.length; counter++ )
    
    output += " " + array[ counter ];
    
    outputArea.setText( output );
    
    
    }//end of ActionCommand
    
    
    // sort elements of array with bubble sort
    
    public void bubbleSort( int array2[] ) {
    
    // loop to control number of passes
    
    for ( int pass = 1; pass < array2.length; pass++ ) {
    
    // loop to control number of comparisons
    
    for ( int element = 0; element < array2.length - 1; element++ ) {
    
    // compare side-by-side elements and swap them if first element is greater than second element
    
    if ( array2[ element ] > array2[ element + 1 ] )
    
    swap( array2, element, element + 1 );
    
    
    
    }//end of inner for loop
    
    }//end of for loop
    
    }//end of BubbleSort
    
    // swap two elements of an array
    
    public void swap( int array3[], int first, int second ) {
    
    int hold; // temporary holding area for swap
    
    hold = array3[ first ];
    
    array3[ first ] = array3[ second ];
    
    array3[ second ] = hold;
    
    }//end of swap
    
    
    }
    
    
    }
    
    }//end of ActionEvent
    
    
    
    //===========================================================================================================
    // Instructions to Close Window
    //===========================================================================================================
    
    public static void main(String args [])
    {
    
    NumberSorter myFrame = new NumberSorter();
    myFrame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
    
    }
    Here are the compile errors
    Java Code:
    ----jGRASP exec: javac -g NumberSorter.java
    
    NumberSorter.java:195: illegal start of expression
    public void bubbleSort( int array2[] ) {
    ^
    NumberSorter.java:195: illegal start of expression
    public void bubbleSort( int array2[] ) {
           ^
    NumberSorter.java:195: ';' expected
    public void bubbleSort( int array2[] ) {
                          ^
    NumberSorter.java:195: ';' expected
    public void bubbleSort( int array2[] ) {
                                        ^
    NumberSorter.java:221: illegal start of expression
    public void swap( int array3[], int first, int second ) {
    ^
    NumberSorter.java:221: illegal start of expression
    public void swap( int array3[], int first, int second ) {
           ^
    NumberSorter.java:221: ';' expected
    public void swap( int array3[], int first, int second ) {
                    ^
    NumberSorter.java:221: <identifier> expected
    public void swap( int array3[], int first, int second ) {
                                   ^
    NumberSorter.java:221: not a statement
    public void swap( int array3[], int first, int second ) {
                                        ^
    NumberSorter.java:221: ';' expected
    public void swap( int array3[], int first, int second ) {
                                             ^
    NumberSorter.java:221: ';' expected
    public void swap( int array3[], int first, int second ) {
                                                         ^
    NumberSorter.java:239: class, interface, or enum expected
    }//end of ActionEvent
    ^
    NumberSorter.java:247: class, interface, or enum expected
    public static void main(String args [])
                  ^
    NumberSorter.java:251: class, interface, or enum expected
    myFrame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
    ^
    NumberSorter.java:253: class, interface, or enum expected
    }
    ^
    15 errors
    So why is it hating that bubble sort so much?

    Thanks for any and all input on this!

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    I can't read your code due to very inconsistent indentation. Please fix the indentation so we can understand what you're doing. Also you do not want to use == to compare Strings as you're doing here. Instead use either the .equals(...) or .equalsIgnoreCase(...) method.

  7. #7
    dlorde is offline Senior Member
    Join Date
    Jun 2008
    Posts
    339
    Rep Power
    7

    Default

    You've stuck your bubbleSort method declaration in the middle of your actionPerformed method. That's illegal. Sort out your indentation and you'll probably see where a closing curly brace } is missing.

  8. #8
    Veronicad1 is offline Member
    Join Date
    May 2011
    Posts
    9
    Rep Power
    0

    Default

    Okay I cleaned it up - like I said I found the code so I just wanted to see if it would work- this was the original code, not my creation. Now have new and improved issues and again forgive me for being a newbie. I want to get it to work then change it to work with names, not numbers. Thanks for the help. Sorry, trying to learn this as I go with the website and tutorials I can find.

    PHP Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.ActionListener;
    import java.awt.event.ActionEvent;
    
    public class NumberSorter extends JFrame implements ActionListener
    {
    	private JTextField tf1, tf2, tf3, tf4, tf5;
    	private JButton b1;
    	private JLabel l1, l2;
    	private JPanel panel;
    	private JTextArea outputArea;
    	int Numbers[]= new int[5];
    	public NumberSorter() 
    	{
    		super("Number Sorter");
    		Container contentPane = getContentPane();
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    //=========================================================================================================
    //	 Create Panel where components are to be stored
    //=========================================================================================================
    		JPanel panel = new JPanel();
    		panel.setBackground(Color.YELLOW);
    		//Change background colout to pink
    //panel.setLayout(new GridLayout(5,6 ) );//set layout to GridLayout
    //==========================================================================================================
    //	 Create Label to give instructions to user
    //==========================================================================================================
    		JLabel label1 = new JLabel("Please enter a number in each field and click on Bubble Sort Button to sort");
    		label1.setVerticalAlignment(JLabel.TOP);
    //============================================================================================================
    //	 Create TextField area for Output
    //============================================================================================================
    		outputArea = new JTextArea(10,25);
    		outputArea.setEditable(false);//Do not allow Textarea to be edited.
    		outputArea.setLineWrap(true);//allow wrapping of text
    //=============================================================================================================
    //	 Create five TextFields	& One Button and Add Action Listeners
    //=============================================================================================================
    		tf1 = new JTextField(5);
    		//Align text to center of textfield
    		tf1.setHorizontalAlignment(JTextField.CENTER);
    		// Initialize the text of the field
    		tf1.setText(" ");
    		tf1.addActionListener(this );
    		tf2 = new JTextField(5);
    		tf2.setHorizontalAlignment(JTextField.CENTER);
    		tf2.setText(" ");
    		tf2.addActionListener(this );
    
    		tf3 = new JTextField(5);
    		tf3.setText(" ");
    		tf3.setHorizontalAlignment(JTextField.CENTER);
    		tf3.addActionListener(this );
    
    		tf4 = new JTextField(5);
    		tf4.setHorizontalAlignment(JTextField.CENTER);
    		tf4.setText(" ");
    		tf4.addActionListener(this );
    
    		tf5 = new JTextField(5);
    		tf5.setHorizontalAlignment(JTextField.CENTER);
    		tf5.setText(" ");
    		tf5.addActionListener(this );
    		
    		//Create JButton
    			JButton b1 = new JButton("BUBBLE SORT");
    			b1.addActionListener(this );
    
    //=================================================================================================
    // Add Label, TextFields, TextArea and Button to Panel.
    //=================================================================================================
    			panel.add(label1);
    			panel.add(tf1);
    			panel.add(tf2);
    			panel.add(tf3);
    			panel.add(tf4);
    			panel.add(tf5);
    			panel.add(b1);
    			panel.add( new JScrollPane(outputArea), BorderLayout.CENTER );
    
    //=================================================================================================
    //	 Add the panel to the content pane and set window properties
    //==================================================================================================
    
    			contentPane.add(panel);
    			setSize(450,300);
    			setVisible( true );
    
    			}
    			//end of Sorter constructor
    
    //===================================================================================================
    //	 Use String Method to send Bubblesort array to TextArea
    //===================================================================================================
    
    	public void actionPerformed( ActionEvent e )
    		{
    				String s1="", s2="", s3="", s4="", s5="";
    					s1=tf1.getText();
    				Numbers[0]= Integer.parseInt(s1.trim()); //add FIRST number to array here
    					s2=tf2.getText();
    				Numbers[1]= Integer.parseInt(s2.trim()); //add SECOND number to array here
    					s3=tf3.getText();
    				Numbers[2]= Integer.parseInt(s3.trim()); //add THIRD number to array here
    					s4=tf4.getText();
    				Numbers[3]= Integer.parseInt(s4.trim()); //add FOURTH number to array here
    					s5=tf5.getText();
    				Numbers[4]= Integer.parseInt(s5.trim()); //add FIFTH number to array here
    				String output = ("Numbers in original order:\n");
    				// append array values to String output
    
    					for ( int counter = 0; counter < 5; counter++ )
    					{
    						output += Integer.toString( Numbers[counter])+ " ";
    						outputArea.setText( output );//output numbers to TextArea
    					}
    					//end of for loop
    
    //====================================================================================================
    //	 BubbleSort Numbers in Array and Display to TextArea
    //====================================================================================================
    
    						if (e.getActionCommand()=="BUBBLE SORT")
    							{ 
    							// test if Bubble Sort button was selected
    							int Numbers[]= new int[5];
    							String output = "Data items in original order\n";
    							// append original array values to String output
    							for ( int counter = 0; counter < array.length; counter++ )
    								output += " " + array[ counter ];
    
    								bubbleSort( array ); // sort array
    								output += "\n\nData items in ascending order\n";
    							// append sorted\ array values to String output
    								for ( int counter = 0; counter < array.length; counter++ )
    								output += " " + array[ counter ];
    								outputArea.setText( output );
    							}
    							//end of ActionCommand
    							}// sort elements of array with bubble sort
    	public void bubbleSort( int array2[] ) 
    						{
    							// loop to control number of passes
    							for ( int pass = 1; pass < array2.length; pass++ ) 
    						{
    							// loop to control number of comparisons
    							for ( int element = 0; element < array2.length - 1; element++ ) 
    						{
    // compare side-by-side elements and swap them if first element is greater than second element
    							if ( array2[ element ] > array2[ element + 1 ] )
    								swap( array2, element, element + 1 );
    
    						}//end of inner for loop
    						}//end of for loop
    						}//end of BubbleSort
    						// swap two elements of an array
    
    	public void swap( int array3[], int first, int second ) 
    					{
    					int hold; // temporary holding area for swap
    					hold = array3[ first ];
    					array3[ first ] = array3[ second ];
    					array3[ second ] = hold;
    
    					}//end of swap
    				
    					}//end of ActionEvent
    And here are the error codes:

    Java Code:
    NumberSorter.java:128: output is already defined in actionPerformed(java.awt.event.ActionEvent)
    							String output = "Data items in original order\n";
    							       ^
    NumberSorter.java:130: cannot find symbol
    symbol  : variable array
    location: class NumberSorter
    							for ( int counter = 0; counter < array.length; counter++ )
    							                                 ^
    NumberSorter.java:131: cannot find symbol
    symbol  : variable array
    location: class NumberSorter
    								output += " " + array[ counter ];
    								                ^
    NumberSorter.java:133: cannot find symbol
    symbol  : variable array
    location: class NumberSorter
    								bubbleSort( array ); // sort array
    								            ^
    NumberSorter.java:136: cannot find symbol
    symbol  : variable array
    location: class NumberSorter
    								for ( int counter = 0; counter < array.length; counter++ )
    								                                 ^
    NumberSorter.java:137: cannot find symbol
    symbol  : variable array
    location: class NumberSorter
    								output += " " + array[ counter ];
    								                ^
    6 errors

  9. #9
    pugazhendhi's Avatar
    pugazhendhi is offline Pugazhendhi
    Join Date
    May 2011
    Location
    Bangalore
    Posts
    20
    Rep Power
    0

    Default

    no need of bubble sort ., u can use hashtable for sorting the names ,.
    first name and last name ., '
    u have to keep the first name in key filed and last name in value filed ., and u have to do the sorting using vector .,, or collection .sort method is there .,
    which will sort
    u have any doubt ., i will post my small code with using gui ., its a console app
    :) Small word can mean it more ,.

  10. #10
    pugazhendhi's Avatar
    pugazhendhi is offline Pugazhendhi
    Join Date
    May 2011
    Location
    Bangalore
    Posts
    20
    Rep Power
    0

    Default

    Java Code:
    
    /**
     * author T.Pugazhendhi */
    
    package com.silicomp.training;
    
    import java.util.ArrayList;
    import java.util.Collections;
    import java.util.Hashtable;
    import java.util.Map;
    import javax.swing.JOptionPane;
    import javax.swing.UIManager;
    
    public class LastNameSorting 
    {
    	int count=1;
    	private String strInput=null;
    	
    	Hashtable<String, String> htString=new Hashtable<String, String>();
    	ArrayList<String> alLastNames=new ArrayList<String>();
    	
    	/**
    	 * throws IOException 
    	 * Here it will read the input from the showInputDialog, and it will initialize it to 
    	 * strInput, which it declared as global.,
    	 * and pass the control to getCount function 
    	 * @throws MyException 
    	 */
    
    	private LastNameSorting()
    	{
    		
    		
    		strInput=" ";
    			
    		/**
    		 * in this it will get the name from the user and put into hashatable
    		 * with  lastname as key field and first name as value
    		 * and one more copy of last name is stored  in the arraylsist ,.
    		 * where the sorting is done in arraylist using the collection 
    		 * and the arraylist is iterated and passed the hashtable to get
    		 *  the values \
    		 * 
    		 */
    		while(strInput.length()!=0)
    		{
    			
    			strInput=JOptionPane.showInputDialog(null, "Enter the names"+ "enter empty to quit program ");
    			
    			String[] strNames=strInput.split(" ");
    			
    			if(!htString.containsKey(strInput)&&strInput.length()!=0)
    			{
    				alLastNames.add(strNames[1]);
    				
    				htString.put(strNames[1],strNames[0]);
    			}
    
    			
    		}
    		
    		Collections.sort(alLastNames);
    		
    		fnDisplay();
    		
    	}
    	/**
    	 * in this string is splitted and stored into the 
    	 */
    	public void fnDisplay()
    	{
    		for(int iterator=0;iterator<alLastNames.size();iterator++)
    		{
    			
    			System.out.println(""+htString.get(alLastNames.get(iterator)).toString()+" "+alLastNames.get(iterator));
    		}
    	}
    	
    	public static void main(String[] args) 
    	{
    		
    		try 
    		 {
    	         new LastNameSorting();
    	     } 
    		 catch (Exception ex) 
    		 {
    	         ex.printStackTrace();
    	     }
    	}
    
    }
    :) Small word can mean it more ,.

  11. #11
    Veronicad1 is offline Member
    Join Date
    May 2011
    Posts
    9
    Rep Power
    0

    Default

    That compiled - thank you Pugazhendhi! I plug that into where the bubble sort is? Sorry, I used to program legacy systems thinking I could just do this and this is a whole 'nuther beast!

  12. #12
    Veronicad1 is offline Member
    Join Date
    May 2011
    Posts
    9
    Rep Power
    0

    Default

    I got it work work with numbers (and threw a little PacMan in there - found that too!) just to show that I could get it to work. But I STILL cannot get an String Array to work with this. HELP!

    PHP Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.ActionListener;
    import java.awt.event.ActionEvent;
    import java.util.*;
    
    public class NumberSorter extends JFrame implements ActionListener
    {
    
    private JTextField tf1, tf2, tf3, tf4, tf5;
    private JButton b1;
    private JLabel l1, l2;
    private JPanel panel;
    private JTextArea outputArea;
    int Numbers[]= new int[5];
    
    public NumberSorter() 
       {
          super("Number Sorter");
         Container contentPane = getContentPane();
          JPanel panel = new JPanel();
    
    // Set background color to yellow
    
          panel.setBackground(Color.YELLOW);
          
    // Set Font type and size
    
          Font labelFont = new Font("Arial", Font.BOLD, 14);
    
    //  Create Label to give instructions to user
    
          JLabel label1 = new JLabel
             ("Please enter up to 5 numbers in each field and click Press Me ");
          label1.setFont(labelFont);
          label1.setVerticalAlignment(JLabel.TOP);
    
    //  Create TextField area for Output
    
          outputArea = new JTextArea(10,25);
    
    //  Create five TextFields & One Button and Add Action Listeners
    
             tf1 = new JTextField(5);
    
    //Align text to center of textfield
          
             tf1.setHorizontalAlignment(JTextField.CENTER);
             tf1.setText(" ");
             tf1.addActionListener(this );
    
             tf2 = new JTextField(5);
             tf2.setHorizontalAlignment(JTextField.CENTER);
             tf2.setText(" ");
             tf2.addActionListener(this );
    
             tf3 = new JTextField(5);
             tf3.setText(" ");
             tf3.setHorizontalAlignment(JTextField.CENTER);
             tf3.addActionListener(this );
    
             tf4 = new JTextField(5);
             tf4.setHorizontalAlignment(JTextField.CENTER);
             tf4.setText(" ");
             tf4.addActionListener(this );
    
             tf5 = new JTextField(5);
             tf5.setHorizontalAlignment(JTextField.CENTER);
             tf5.setText(" ");
             tf5.addActionListener(this );
    
    //Create JButton
    
             JButton b1 = new JButton("Press Me");
             b1.addActionListener(this );
    
    // Add Label, TextFields, TextArea and Button to Panel.
    
             panel.add(label1);
             panel.add(tf1);
             panel.add(tf2);
             panel.add(tf3);
             panel.add(tf4);
             panel.add(tf5);
             panel.add(b1);
             panel.add( new JScrollPane(outputArea), BorderLayout.CENTER );
    
    //  Add the panel to the content pane and set window properties
    
             contentPane.add(panel);
    
             setSize(475,450);
             setVisible( true );
    
       }
    
    //  Use String Method to send array to TextArea
    
    
       public void actionPerformed( ActionEvent e )
          {
    
          String s1="", s2="", s3="", s4="", s5="";
    
             s1=tf1.getText();
            Numbers[0]= Integer.parseInt(s1.trim()); //array FIRST number
             s2=tf2.getText();
             Numbers[1]= Integer.parseInt(s2.trim()); //array SECOND number 
             s3=tf3.getText();
             Numbers[2]= Integer.parseInt(s3.trim()); //array THIRD number 
             s4=tf4.getText();
             Numbers[3]= Integer.parseInt(s4.trim()); //array FOURTH number
             s5=tf5.getText();
             Numbers[4]= Integer.parseInt(s5.trim()); //array FIFTH number 
    
    
             String output = ("Numbers in original order:\n");
    
    // append array values to String output
    
             for ( int counter = 0; counter < 5; counter++ )
             {
    
                output += Integer.toString( Numbers[counter])+ "\n ";
    
    
                outputArea.setText(output);//output numbers to TextArea
    
             }
    //To sort full array use sort(int[] ) method
    
                   Arrays.sort(Numbers);
                   
    //print sorted  array
                String output1 = ("Sorted array \n");
                
                for(int index=0; index < Numbers.length ; index++)
    
                output1 += Integer.toString( Numbers[index])+ "\n ";
    
    
                outputArea.setText(output1);
    
          }
    
       		public void paint(Graphics g)
       			{
          		super.paint(g);
    				g.setColor(Color.BLACK);
          		g.fillArc(10, 250, 110, 110, 70, 320);
    				g.setColor(Color.RED);
         			g.fillArc(10, 250, 100, 100, 70, 320);
    				g.setColor(Color.BLACK);
    				g.fillOval(35,275,10,10);
    				}
    
    
    // Instructions to Close Window
    
    
          public static void main(String args [])
             {
    
            NumberSorter myFrame = new NumberSorter();
             myFrame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
    
             }
    
    
    }//end program

  13. #13
    pugazhendhi's Avatar
    pugazhendhi is offline Pugazhendhi
    Join Date
    May 2011
    Location
    Bangalore
    Posts
    20
    Rep Power
    0

    Default

    i am checking u r code ., if i solve that one i will post u
    :) Small word can mean it more ,.

  14. #14
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default

    Quote Originally Posted by Veronicad1 View Post
    HELP!
    The "help" you have gotten so far consists of stealing someone else's code. Since you do not understand it you want us to fix it for you. Do you plan on doing any of the work yourself?

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

    Default

    That's bad code, on several accounts. I suggest you learn how to correctly perform custom painting before trying to advise here. Also, you need to be aware of Swing's single threaded rule, and how to construct and manipulate Swing components only on the EDT.

    More importantly, avoid posting full solutions. As Junky has hinted above, spoonfeeding is not help.

    db

  16. #16
    pugazhendhi's Avatar
    pugazhendhi is offline Pugazhendhi
    Join Date
    May 2011
    Location
    Bangalore
    Posts
    20
    Rep Power
    0

    Default

    hey common ., thats not my code ., i redited to make sure that it work for strings., if u find any faults in my code then i am extremely sorry ., everyone has his own way ., i cant follow ur way ., of code untill u teach it ., if u really want to make something to teach us , then tell us clearly ., what i have to correct here ., i correct it
    :) Small word can mean it more ,.

  17. #17
    dlorde is offline Senior Member
    Join Date
    Jun 2008
    Posts
    339
    Rep Power
    7

    Default

    @Pugazhendhi - if you want help, please start your own thread. Meanwhile, we are trying to help someone write their own code, so please don't post badly coded solutions.

  18. #18
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

  19. #19
    Veronicad1 is offline Member
    Join Date
    May 2011
    Posts
    9
    Rep Power
    0

    Default

    Quote Originally Posted by Junky View Post
    The "help" you have gotten so far consists of stealing someone else's code. Since you do not understand it you want us to fix it for you. Do you plan on doing any of the work yourself?
    Wow I have never seen so many haters; I did what I could for my friend. I'm actually doing this as a favor. I don't know Java, I volunteered thinking it couldn't be that hard. How many mea culpas must I post? I asked for help - that's it. If you can't say anything nice then say nothing at all. I am not EXPECTING ANYONE TO DO ANYTHING - I asked for input as I learn. Please, don't be a teacher, your students will kill themselves with that attitude. Sheesh.

  20. #20
    Veronicad1 is offline Member
    Join Date
    May 2011
    Posts
    9
    Rep Power
    0

    Default

    Quote Originally Posted by DarrylBurke View Post
    That's bad code, on several accounts. I suggest you learn how to correctly perform custom painting before trying to advise here. Also, you need to be aware of Swing's single threaded rule, and how to construct and manipulate Swing components only on the EDT.

    More importantly, avoid posting full solutions. As Junky has hinted above, spoonfeeding is not help.

    db
    I wouldn't know swing code from a fottrot. Like I said and please allow me to apologize for my stupidity yet again:
    1) I AM NEW TO JAVA - THIS IS ALL GREEK TO ME
    2) I DID THIS FOR A FRIEND
    3) I MADE A MISTAKE AND POSTED HERE ASKING FOR HELP

    How many of these mistakes do you think I will repeated based on Einsteins belief that the definition of insanity is repeating an action over and over expecting different results - I'm going to say NONE.

    Thanks you guys for being so rude - as a legacy programmer we were taught manners and how to try to be kind. Not hand hold but at least be decent. Sorry you all missed that in life. Only one person was even remotely kind and to him I say thank you.

    Being on the internet allows people to be mean-spirited. I can only say be careful what kind of karma you create - it will come back to you, good or bad.

Page 1 of 2 12 LastLast

Similar Threads

  1. array to sort names in alphabetical order
    By leoshiner in forum New To Java
    Replies: 6
    Last Post: 05-01-2011, 01:28 PM
  2. Replies: 10
    Last Post: 02-11-2011, 09:15 PM
  3. How to bind soap header using jax-rpc
    By jadeite100 in forum XML
    Replies: 0
    Last Post: 12-05-2010, 07:17 PM
  4. How to bind Spring MVC with my local service
    By Alexey Slepov in forum Web Frameworks
    Replies: 1
    Last Post: 03-25-2010, 12:48 PM
  5. bind slider to column in DB
    By nicromonicon in forum New To Java
    Replies: 0
    Last Post: 01-12-2009, 05:14 PM

Posting Permissions

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