FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


[Java] Wanna help me figure out this error?





munkey_boy
Ok so my code is:

Code:
import java.util.*;
import java.io.*;

public class PassGen
{
   public static void main(String[] args)
   {
      Scanner input = new Scanner(System.in);


      String strInput1;
      int intInput1;

      System.out.print("******************************************* \n");
      System.out.print("|         Password Generator v1.1         | \n");
      System.out.print("|        Copyright 2007 munkey_boy        | \n");
      System.out.print("******************************************* \n \n");

         while (true)
         {

         System.out.print("First & Last Name: ");
         strInput1 = input.nextLine();

         intInput1 = 10 + (int)(Math.random() * 1100000);

         System.out.printf(strInput1 + "'s Password: " + intInput1 + "\n \n \n");

         FileWriter FileWriter1 = new FileWriter(strInput1 + ".txt", false);
         PrintWriter PrintWriter1 = new PrintWriter(FileWriter1);

         PrintWriter1.println(strInput1 + ": " + intInput1);
         PrintWriter1.close();

      }
   }
}


But when I compile it I get:
Code:
C:\Documents and Settings\Zac\Desktop\Java\Finished\Password Generator\PassGen.java:29: unreported exception java.io.IOException; must be caught or declared to be thrown
         FileWriter FileWriter1 = new FileWriter(strInput1 + ".txt", false);


I know it's only small but I want to finish what I started, and this is really getting on my nerves. Smile

Any help appreciated. Wink
AftershockVibe
Hey there!

Your password generator calls methods in a Java.IO object which can throw an exception. I assume that this is the "FileWriter" object given the error.

Either way, the Java compiler requires that if your "PassGen" method can throw then you declare that it may throw. Note that it can throw because it calls a method which can throw.

Alternatively, you can change your method so that it can't throw because it catches and handles the exceptions which might be thrown. I assume this is the easiest method because I'm pretty sure you can't redefine main() to says it can throw.

This method is really simple, just add some try/catch pairs to the methods that might throw:

Code:

try
{
    FileWriter FileWriter1 = new FileWriter(strInput1 + ".txt", false);
    PrintWriter PrintWriter1 = new PrintWriter(FileWriter1);
}
catch (java.io.IOException ex)
{
    // Do some handling
    // Output some sort of error and close the file if it is open.
}


What exactly needs enclosed in the try() you can refine. Some of the declarations will probably need to be moved outside so they are visible to the catch().

You might also want to do a few separate try/catch pairs so you can report different errors dependent on the error. FileWriter or PrintWriter?

Hope this helps.
munkey_boy
Thanks man. I will give it a try.
Related topics
Help: SQL error after uploading new files(thread updated)
Java experts help us who know nothing
help explaining error messsage from CSS validator
Does anypne help me about JAVA on UNIX?
Wanna help IFA with their website?
help desktop error.
[help]fatal error eso dice mi web
Need help with error
Java Game HELP!!
Realy need Help.. Critical Error!! Permission denied
Java Hide/Show Link Error
reg JAVA
java script help please!!!
Java Servlet Help needed
Reply to topic    Frihost Forum Index -> Scripting -> Others

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.