• Post Reply Bookmark Topic Watch Topic
  • New Topic

FileNotFound exception  RSS feed

 
Jd Wells
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Im trying to catch a file not found exception so that my program can do something when it occurs, but I keep getting "FileNotFound exception is never thrown in body of corresponding try statement."
I am using a try catch block in the following piece of code to attempt this.

String[] goChar = new String[6];
public BQ() {

initComponents();
try {
Scanner charIn = new Scanner("character.txt");

int count = 0;

while (charIn.hasNext())
{
goChar[count] = charIn.next();
count ++;
}

} catch(FileNotFoundException e) {


JOptionPane.showMessageDialog(null,"Welcome to Bodacious Quest, where questing\n "
+ "is the name of the game. To begin,\n we need to get some information from you,\n "
+ "the bodacious player. Please complete the\n following screens to set up your character." ,null, JOptionPane.INFORMATION_MESSAGE);


setRace();

}

Any assistance would be greatly appreciated. Thanks!
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16057
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This line:

does not do what you think it does.

You most likely think that this is going to try to open a file named "character.txt" from which it is going to read input, but that is not what it does. What it does, is that it is going to read from the string you supplied. So it's going to interpret the string literal "character.txt".

You want to use one of the other constructors of class java.util.Scanner instead. Have a look at the API documentation for java.util.Scanner (<= click that to go to the API documentation).
 
Jd Wells
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ah.. That cleared things up quite a bit! Thank you...
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!