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: Button help, confusing error.

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

    Default Button help, confusing error.

    So in my CS class we made a simple program to sort a list of names then re-write the names into a new file. It was really easy and I finished pretty fast. But then I decided to make it GUI with awt so I added 1 button that does everything it did. But I'm getting this error and I don't know how to fix it

    actionPerformed(java.awt.event.ActionEvent) in SortMe cannot implement actionPerformed(java.awt.event.ActionEvent) in java.awt.event.ActionListener; overridden method does not throw java.io.IOException
    public void actionPerformed(ActionEvent e) throws IOException

    Will someone review my source file and tell me what exactly I'm doing wrong?

    import java.io.*;
    import java.util.Arrays;
    import java.awt.*;
    import java.awt.event.*;
     
     
    public class SortMe extends Frame implements ActionListener
    {
        private static String names[];
        private static int fileSize;
        private static int tick = 0;
        private Button b;
        private BufferedReader input;
        private BufferedWriter output;
     
        public SortMe()
        {
            super("Sorting Names");
            Frame f = new Frame();
            b = new Button("Sort!");
    	f.add(b);
            b.addActionListener(this);
        }
     
    //----------------------------------------------------------------------------
     
        public void actionPerformed(ActionEvent e) 
        {
            if (e.getSource() == b)
            {
                 perform();
            }
        }
     
    //-----------------------------------------------------------------------------
     
        public void perform() 
        {
                names = new String[100];
                readFile("Names1.txt");
                displayArray();
                sortArray();
                displayArray();
                writeFile("Names2.txt");
        }
     
    //-------------------------------------------------------------------------------
     
        public static void main (String args[])
        {
     
            SortMe app = new SortMe();
            app.addWindowListener(
            new WindowAdapter() { public void windowClosing(          WindowEvent e )
            {
            System.exit( 0 );
            }
            }
            );
        }
     
    //-------------------------------------------------------------------------------
     
        public void readFile(String fileName) 
        {
            input = new BufferedReader(new FileReader(fileName));
            int i = 0;
            String in = input.readLine();
            while (in !=null)
            {
                names[i] = in;
                i++;
                tick++;
                in = input.readLine();
            }
     
                input.close();
        }
     
    //-------------------------------------------------------------------------------
     
        public static void displayArray()
        {
     
            for (int x =0; x <= names.length-1; x++)
            {
                if (names[x] != null)
                System.out.println(names[x]);
            }
     
        }
     
    //-------------------------------------------------------------------------------
     
        public static void sortArray()
        {
            int tempCount = 0;
            String temp = "";
            for (int x = 1; x < tick; x++)
                for (int y = 0; y < tick-x; y++)
                {
                    String s = names[y];
                    String s1 = names[y+1];
                    tempCount = 1;
                    while (tempCount != 0)
                    {
                        if (s.equals(s1))
                            tempCount = 0;
                        else
                            if (s.charAt(tempCount-1) ==  s1.charAt(tempCount-1))
                            {
                                tempCount++;
                            }
                            else
                            {
                                if (s.charAt(tempCount-1) > s1.charAt(tempCount-1))
                                {
                                    temp = names[y];
                                    names[y] = names[y+1];
                                    names[y+1]= temp;
                                }
                                tempCount = 0;
     
     
                        }
                    }
                }
     
     
        }
     
    //-------------------------------------------------------------------------------
     
        public void writeFile(String fileName) throws IOException
        {
                output = new BufferedWriter(new FileWriter(fileName));
                for (int x = 0; x < tick; x++)
                if (names[x] != null)
                {
                    output.write(names[x]);
                    output.newLine();
                }
     
                output.close();
        }
     
    }
    Last edited by camboy8; December 17th, 2010 at 09:21 PM.


  2. #2
    Super Moderator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,230
    Thanks
    176
    Thanked 817 Times in 760 Posts
    Blog Entries
    5

    Default Re: Button help, confusing error.

    The writeFile method throws an IOException, which must be caught. You can have a piece of code not catch the exception, but any method calling that code must either catch or throw that exception. As a solution, since the io seems specific for that function, you might want to deal with the exceptions within that function, catching any IOExceptions and closing resources in a finally, perhaps returning a status
    	public int writeFile(String fileName) 
    	{
    		BufferedWriter output = null;
    		try{
    			output = new BufferedWriter(new FileWriter(fileName));//local variable
    			for (int x = 0; x < tick; x++)
    				if (names[x] != null)
    				{
    					output.write(names[x]);
    					output.newLine();
    				}
    		}catch(Exception e){
    			e.printStackTrace();
    			return -1;
    		}finally{
    			try{ output.close(); }catch(Exception e){}
    		}
    		return 0;
    	}

    Further, you might want to consider using Swing rather than AWT.

Similar Threads

  1. Action button HELP?
    By utoptas in forum Java Theory & Questions
    Replies: 8
    Last Post: August 27th, 2010, 03:32 PM
  2. result set array button
    By dread_arisawa in forum What's Wrong With My Code?
    Replies: 2
    Last Post: August 21st, 2010, 10:05 AM
  3. Please help with Actionlistener-Button
    By ashleykathy in forum AWT / Java Swing
    Replies: 1
    Last Post: March 4th, 2010, 07:21 PM
  4. While (logical confusing output)
    By chronoz13 in forum Loops & Control Statements
    Replies: 4
    Last Post: December 20th, 2009, 12:17 AM
  5. how to using button to change linechart
    By NARs in forum AWT / Java Swing
    Replies: 3
    Last Post: October 30th, 2009, 12:53 PM