473,405 Members | 2,349 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

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 fileNotFoundException, 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


Expand|Select|Wrap|Line Numbers
  1.       public void actionPerformed(ActionEvent e)
  2.       {
  3.           String find = findTextField.getText();
  4.  
  5.           String replace = replaceTextField.getText();
  6.  
  7.  
  8.           try
  9.           {         
  10.               FileReader fReader = new FileReader("Readme.txt");
  11.               BufferedReader inputFile = new BufferedReader(fReader);
  12.           }
  13.           catch(FileNotFoundException a)
  14.           {
  15.               JOptionPane.showMessageDialog(null, "File not found.");
  16.           }
  17.  
  18.  
  19.           String line = inputFile.readLine();
  20.  
  21.           System.out.println("New text: \n");
  22.  
  23.           while (!(line == null))
  24.           {
  25.               while(line.indexOf(find) != -1)
  26.               {
  27.                   String [] words = line.split(find);
  28.                   line = (words[0].concat(replace).concat(words[1]));
  29.  
  30.               }
  31.  
  32.               System.out.println(line);
  33.  
  34.               line = inputFile.readLine();
  35.  
  36.           }
  37.  
  38.  
  39.       }
Mar 5 '09 #1
9 1121
r035198x
13,262 8TB
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.
Mar 5 '09 #2
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?
Mar 5 '09 #3
r035198x
13,262 8TB
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).
Mar 5 '09 #4
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.IOException; must be caught or declared to be thrown

thanks again for your help, i apologize if i am a noob here haha
Mar 5 '09 #5
r035198x
13,262 8TB
Add
Expand|Select|Wrap|Line Numbers
  1. catch(Exception e)  {
  2.           e.printStackTrace();
  3. }
  4.  
after your
Expand|Select|Wrap|Line Numbers
  1. catch(FileNotFoundException a)  {
  2.           JOptionPane.showMessageDialog(null, "File not found.");
  3. }
  4.  
And check your console to see what gets printed there.

P.S Writing code first and reading the tutorial later is hardly standard practice
Mar 5 '09 #6
Unfortunatly still getting the same error, not having much luck here blah
Mar 5 '09 #7
JosAH
11,448 Expert 8TB
@adamrehill
Read what r035198x wrote:

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
Mar 5 '09 #8
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
Mar 5 '09 #9
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!
Mar 5 '09 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

15
by: Cheryl Langdon | last post by:
Hello everyone, This is my first attempt at getting help in this manner. Please forgive me if this is an inappropriate request. I suddenly find myself in urgent need of instruction on how to...
3
by: Noremac | last post by:
My google skills must be dwindling. I am trying to determine how in ASP.NET 2.0 I can get the ReturnUrl querystring variable in Forms Authentication to contain the absolute url. Just like others...
1
by: psantosh12 | last post by:
Hello Frnds Please need help to resolve error.......... it is very very urgent........ The error is Runtime Error Description: An application error occurred on the server. The current custom...
4
by: Linda Liu[MSFT] | last post by:
Hi Moondaddy, I downloaded your sample project and run it and did see the problem on my side. There're three problems in the source code of your project. 1. You should move the following...
5
by: =?Utf-8?B?SGVyYg==?= | last post by:
I need a Table object in my RDLC with 6 columns on the top several rows, but 1 column on the bottom two rows: COLUMN1 COLUMN2 COLUMN3 COLUMN4 COLUMN5 COLUMN6 COLUMN1 COLUMN2 COLUMN3 ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.