• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

FileNotFound exception

 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ah.. That cleared things up quite a bit! Thank you...
reply
    Bookmark Topic Watch Topic
  • New Topic