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 1 of 1

Thread: TextArea and Array Problem

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

    Default TextArea and Array Problem

    Hello, everyone. I'm new to Java, and I'm having a headache-inducing hard time trying to analyze this code. Basically, I was tasked to modify the code of the DoBubbleSort() method in the program below to sort the array in descending order if all the numbers entered are odd and ascending order if all the numbers entered are even. Then, I was supposed to check and catch that the data entered is valid. If all the numbers are mixed (odd & even), then I was to have the program print to the textarea box that the data entered is invalid. If strings are entered into textboxes, I had to have the program print to textarea a message that says 'Enter only whole numbers.".

    I thought I was successful in making these several edits, but the program neither sorts the array or displays the error message for the exception. It's as if there is no connection between the DoBubbleSort() and ReadInData() methods and the rest of the program. However, I have no idea what the problem is, and I'm almost certain that there's more than one error.

    Please, I need help! I also have a deadline to complete this program successfully, which is Monday evening.

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import java.text.*;
    import java.util.*;
     
    public class chapter9 extends Frame implements ActionListener{
        JButton button_1;
        JTextField textfield_1;
        JTextField textfield_2;
        JTextField textfield_3;
        JTextField textfield_4;
        JTextField textfield_5;
        JTextArea textarea_1;
        JScrollPane sp_textarea_1;
     
        int[] myArray = new int[5];
     
        public chapter9() {
            chapter9Layout customLayout = new chapter9Layout();
     
            setFont(new Font("Helvetica", Font.PLAIN, 12));
            setLayout(customLayout);
     
            button_1 = new JButton("Run");
            add(button_1);
     
            textfield_1 = new JTextField("");
            add(textfield_1);
     
            textfield_2 = new JTextField("");
            add(textfield_2);
     
            textfield_3 = new JTextField("");
            add(textfield_3);
     
            textfield_4 = new JTextField("");
            add(textfield_4);
     
            textfield_5 = new JTextField("");
            add(textfield_5);
     
            textarea_1 = new JTextArea("");
            sp_textarea_1 = new JScrollPane(textarea_1);
            add(sp_textarea_1);
     
            setSize(getPreferredSize());
     
            button_1.addActionListener(this);
     
            addWindowListener(new WindowAdapter() {
                public void windowClosing(WindowEvent e) {
                    System.exit(0);
                }
            });
        }
     
        public void actionPerformed(ActionEvent e) 
        {
        	readInData();
        	doBubbleSort();
        	displayResult();	
    	}
     
        public void doBubbleSort() 
        {
    	    	int temp = 0, remainder, tempTotal = 0;
     
    		   for(int index2 = 0; index2 < myArray.length-1; index2++)
    		   {
    				remainder = myArray[index2] % 2;
    				tempTotal += remainder;
    		   }
    		   if (tempTotal == 0)
    		   {
    			 	for(int counter=0; counter<myArray.length-1; counter++)
    	      	{ 
    	            for(int index=0; index<myArray.length-1-counter; index++) 
    	            { 
    						 if(myArray[index] > myArray[index+1]) 
    	                { 
    	                    temp = myArray[index]; 
    	                    myArray[index] = myArray[index+1];
    	                    myArray[index+1] = temp;
    	                }
    	            }
    	         }
    		   }else if (tempTotal == myArray.length)
    			{
    				for(int counter=0; counter<myArray.length-1; counter++)
    	      	{ 
    	            for(int index=0; index<myArray.length-1-counter; index++) 
    	            { 
    						 if(myArray[index] < myArray[index+1]) 
    	                { 
    	                    temp = myArray[index]; 
    	                    myArray[index] = myArray[index+1];
    	                    myArray[index+1] = temp;
    	                }
    	            }
    	         }
    			}else
    			{
    				textarea_1.append("The data entered is invalid.");
    			}
     
    	}
     
     
     
        public void readInData() 
        {    	
    		try
    		{
    			myArray[0] = Integer.parseInt(textfield_1.getText());
    			myArray[1] = Integer.parseInt(textfield_2.getText());
    			myArray[2] = Integer.parseInt(textfield_3.getText());
    			myArray[3] = Integer.parseInt(textfield_4.getText());
    			myArray[4] = Integer.parseInt(textfield_5.getText());  
    		}catch (InputMismatchException e)
    		{
    			textarea_1.append("Enter only whole numbers.");
    		} 	
     
    	}
     
        public void displayResult() 
        {
        	textarea_1.setText("\n");
     
    		for (int i = 0; i < myArray.length; i++) 
    		{
        		textarea_1.append(myArray[i] + "\n");
    		}    	  	
    	}
     
     
     
        public static void main(String args[]) {
            chapter9 window = new chapter9();
     
            window.setTitle("chapter9");
            window.pack();
            window.show();
        }
    }
     
    class chapter9Layout implements LayoutManager {
     
        public chapter9Layout() {
        }
     
        public void addLayoutComponent(String name, Component comp) {
        }
     
        public void removeLayoutComponent(Component comp) {
        }
     
        public Dimension preferredLayoutSize(Container parent) {
            Dimension dim = new Dimension(0, 0);
     
            Insets insets = parent.getInsets();
            dim.width = 187 + insets.left + insets.right;
            dim.height = 466 + insets.top + insets.bottom;
     
            return dim;
        }
     
        public Dimension minimumLayoutSize(Container parent) {
            Dimension dim = new Dimension(0, 0);
            return dim;
        }
     
        public void layoutContainer(Container parent) {
            Insets insets = parent.getInsets();
     
            Component c;
            c = parent.getComponent(0);
            if (c.isVisible()) {c.setBounds(insets.left+96,insets.top+136,72,24);}
            c = parent.getComponent(1);
            if (c.isVisible()) {c.setBounds(insets.left+8,insets.top+8,72,24);}
            c = parent.getComponent(2);
            if (c.isVisible()) {c.setBounds(insets.left+8,insets.top+40,72,24);}
            c = parent.getComponent(3);
            if (c.isVisible()) {c.setBounds(insets.left+8,insets.top+72,72,24);}
            c = parent.getComponent(4);
            if (c.isVisible()) {c.setBounds(insets.left+8,insets.top+104,72,24);}
            c = parent.getComponent(5);
            if (c.isVisible()) {c.setBounds(insets.left+8,insets.top+136,72,24);}
            c = parent.getComponent(6);
            if (c.isVisible()) {c.setBounds(insets.left+8,insets.top+176,168,280);}
        }
    }

    EDIT: Thanks to whoever placed my code in the proper format or whatever you call it. I was in such panic that I forgot to read the forum rules before posting. Thanks again. But seriously - and by no means do I intend to rush people but - need HELP!
    Last edited by slippery_one; November 28th, 2010 at 02:57 PM. Reason: Just to say thanks...


Similar Threads

  1. Trouble apending a textArea with the contents of a parameter
    By bluetxxth in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 21st, 2010, 04:28 PM
  2. Problem with array
    By mingming8888 in forum What's Wrong With My Code?
    Replies: 9
    Last Post: October 27th, 2010, 12:17 PM
  3. [SOLVED] Array Problem
    By KevinGreen in forum Object Oriented Programming
    Replies: 2
    Last Post: January 24th, 2010, 09:07 PM
  4. Problem with 2d array
    By Anyone in forum Collections and Generics
    Replies: 2
    Last Post: November 14th, 2009, 09:32 PM
  5. Java program for 2-D Array Maze
    By Peetah05 in forum Collections and Generics
    Replies: 11
    Last Post: May 8th, 2009, 04:30 AM