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 -
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();
-
-
}
-
-
-
}
9 1121
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.
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?
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).
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
Add -
catch(Exception e) {
-
e.printStackTrace();
-
}
-
after your -
catch(FileNotFoundException a) {
-
JOptionPane.showMessageDialog(null, "File not found.");
-
}
-
And check your console to see what gets printed there.
P.S Writing code first and reading the tutorial later is hardly standard practice
Unfortunatly still getting the same error, not having much luck here blah
@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
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
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!
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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...
|
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 ...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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
|
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...
|
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,...
|
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...
|
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,...
|
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...
|
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,...
|
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...
| |