Need help please *urgent*

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • adamrehill
    New Member
    • Mar 2009
    • 7

    Need help please *urgent*

    Hi there, this is my first post on the forum, and i'm relatively new to java. Anyways, i've just spent the last few hours looking for answers in my university textbook and the sun website, and im at a loss, so any help provided would be lovely, and thanks in advance.

    Anyways, my project is to create a "find and replace" program simliar to those in microsoft word, etc. The only problem i've ran into so far is that i get an error with the code that i have in public void actionPerformed (ActionEvent e)
    its telling me that i haven't set anything up to handle a fileNotFoundExc eption, which makes sense because from my understanding, you can't throw an exception with actionPerformed ? So i saw on previous forums to use a try, catch, but im still having problems. any suggestions? My code is below


    Code:
          public void actionPerformed(ActionEvent e)
          {
              String find = findTextField.getText();
              
              String replace = replaceTextField.getText();
              
                       
              try
              {         
                  FileReader fReader = new FileReader("Readme.txt");
                  BufferedReader inputFile = new BufferedReader(fReader);
              }
              catch(FileNotFoundException a)
              {
                  JOptionPane.showMessageDialog(null, "File not found.");
              }
              
                       
              String line = inputFile.readLine();
              
              System.out.println("New text: \n");
              
              while (!(line == null))
              {
                  while(line.indexOf(find) != -1)
                  {
                      String [] words = line.split(find);
                      line = (words[0].concat(replace).concat(words[1]));
                      
                  }
                  
                  System.out.println(line);
                  
                  line = inputFile.readLine();
                  
              }
              
              
          }
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    1.) You can throw any exception in the actionPerformed method.
    2.) You have declared the variable inputFile inside the try, that means you cannot access it after that try block. It is only available inside the block where it was declared. Better declare it before the try so that it becomes available in the rest of the actionPerformed method.

    Comment

    • adamrehill
      New Member
      • Mar 2009
      • 7

      #3
      hey, thanks for the reply. Yah at first i thought that was the problem, but once i change it so that it is declared outside the try, i get the error variable inputFile may not have been initialized so it appears as though it doesn't even enter the try :S

      any suggestions?

      Comment

      • r035198x
        MVP
        • Sep 2006
        • 13225

        #4
        Then initialize it to null when you declare it.

        P.S Going through Sun's Java tutorial will also help a lot(See the thread titled "Read this first" at the top of the java forums).

        Comment

        • adamrehill
          New Member
          • Mar 2009
          • 7

          #5
          Yea, reading the tutorial was on my next list of things to do, it's just ive been in bed sick with the flu and strep throat for four days, so reading tutorials when im all drugged up has been hard haha, and this assignment is due on friday so I was trying to find some quick help lol.

          Anyways i appreciate the help, but now that i've declared them as null im back to square one, and it's giving me the same error unreported exception java.io.IOExcep tion; must be caught or declared to be thrown

          thanks again for your help, i apologize if i am a noob here haha

          Comment

          • r035198x
            MVP
            • Sep 2006
            • 13225

            #6
            Add
            [code=java]
            catch(Exception e) {
            e.printStackTra ce();
            }
            [/code]
            after your
            [code=java]
            catch(FileNotFo undException a) {
            JOptionPane.sho wMessageDialog( null, "File not found.");
            }
            [/code]

            And check your console to see what gets printed there.

            P.S Writing code first and reading the tutorial later is hardly standard practice

            Comment

            • adamrehill
              New Member
              • Mar 2009
              • 7

              #7
              Unfortunatly still getting the same error, not having much luck here blah

              Comment

              • JosAH
                Recognized Expert MVP
                • Mar 2007
                • 11453

                #8
                Originally posted by adamrehill
                Unfortunatly still getting the same error, not having much luck here blah
                Read what r035198x wrote:

                Originally posted by r035198x
                P.S Writing code first and reading the tutorial later is hardly standard practice
                Per the contract of the interface the actionPerformed method is not allowed to throw a checked exception. You can either throw an unchecked exception (which I consider hacking) or catch the checked exception and handle it. If you don't know what I'm talking about it's time to stop typing and start reading the tutorial. If you do know what I'm talking about you're able to fix your own error(s).

                kind regards,

                Jos

                Comment

                • adamrehill
                  New Member
                  • Mar 2009
                  • 7

                  #9
                  Alright, well thanks for you help. Sorry if i've wasted anyone's time. Like i said, i had read many other forum's tutorials, as well as several chapters in my text book, so it wasn't like i have been blind coding haha. however, i'll take a look in the tutorial.

                  thanks

                  Comment

                  • adamrehill
                    New Member
                    • Mar 2009
                    • 7

                    #10
                    Just thought i'd let you know that i read the tutorial on exceptions (turns out it was one of the one's i had stumbled upon earlier today, and didn't realize it until i took a second glance at it) and the tutorial plus the last few things you guys added fixed my problems, so thanks for the help guys. sorry if i was a pain and didnt go to the tutorials right away, just got frustrating after trying to fix/find solutions online all day haha.

                    thanks again!

                    Comment

                    Working...