Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

java exception errors

Options
  • 12-11-2003 8:15pm
    #1
    Registered Users Posts: 1,361 ✭✭✭


    Hi everyone, i cant figure out these exception errors im getting, the errors are :

    C:\Everything\projects\voting\screenprintfile.java:41: cannot resolve symbol
    symbol : variable in
    location: class screenprintfile
    while((s = in.readLine())!= null)
    ^
    C:\Everything\projects\voting\screenprintfile.java:62: cannot resolve symbol
    symbol : variable in
    location: class screenprintfile
    in.close();
    ^
    2 errors

    Process completed.
    *************************************************

    the code im using is :

    import java.io.*;

    public class screenprintfile {


    static String [][] cinfo = new String[1000][1000];

    public static void main(String[] args)
    throws IOException {

    /////////////////PROGRAM ORDER////////////////////////
    putCandidArray();
    printStuff();
    ////////////////////////////////////////////////////////



    }

    ///////////////////GET FILE AND PUT IN ARRAY///////////////////////
    static void putCandidArray() {
    try{
    BufferedReader in =
    new BufferedReader(
    new FileReader("c:\\everything\\projects\\voting\\vote1.dat"));
    }
    catch(Exception e) {
    System.err.println("Caught an exception");
    }

    String s, s2 = new String();

    String cand = new String();
    String temp = new String();
    boolean cfound;
    cfound=false;

    int x=0;
    //start of code
    //read everything from file
    while((s = in.readLine())!= null)
    {

    s2 += s + "\n";
    //end of read everything- s2 contains all of file

    //locate candidates
    if (s.equals("<CAND>"))
    {
    cfound=true;
    continue;
    }
    if( s.equals("</CAND>"))
    cfound=false;

    if(cfound == true)
    {
    cand+=s + "\n";
    cinfo[1][x]=s;
    x++;
    }
    in.close();
    }
    }
    /////////////////////////////////////////////////////////////


    ///////////////PRINT STUFF//////////////////////////////
    static void printStuff(){
    //System.out.println(x);
    System.out.println(cinfo[1][3]);
    System.out.println("\n");
    }


    //////////////////////////////////////////////////////


    }


Comments

  • Closed Accounts Posts: 1,567 ✭✭✭Martyr


    Try: while((s = System.in.readLine())!= null)
    Same for other line.
    Try: System.in.close();


  • Registered Users Posts: 1,931 ✭✭✭Zab


    You declared in inside a try block, so it goes out of scope at the end of the try block.

    You could put the declaration outside of the try block, like
    BufferedReader in = null;
    try
    {
      in = blah;
    }
    catch(blah)
    {
    }
    

    On the other hand, you seem to be missing the point of the exception being thrown. If an exception is thrown when you are opening the file, you CANNOT continue, as... well, the file isn't open, is it? So, the whole method should probably be in the try block, and you should be throwing an exception so that users of putCandidArray know that something has gone wrong.

    Zab.


  • Registered Users Posts: 19,396 ✭✭✭✭Karoma


    far too early for this ****.

    public class screenprintfile {
    static BufferedReader in; // so it can be seen elsewhere throughout!

    ___________________________________________

    Also,you must use try catch whenever using the file:

    try
    {
    while((s = in.readLine())!= null)

    ......

    }
    catch(IOException dumb)
    {
    System.err.println("Caught an IO exception");
    }

    and so on...


  • Registered Users Posts: 19,396 ✭✭✭✭Karoma


    /*
    * should look something like this is the end.
    */


    import java.io.*;

    public class screenprintfile {

    static BufferedReader in;

    static String [][] cinfo = new String[1000][1000];

    public static void main(String[] args) throws IOException
    {

    /////////////////PROGRAM ORDER////////////////////////
    putCandidArray();
    printStuff();
    ////////////////////////////////////////////////////////


    }

    ///////////////////GET FILE AND PUT IN ARRAY///////////////////////
    static void putCandidArray() {
    try{
    BufferedReader in =
    new BufferedReader(
    new FileReader("c:\\everything\\projects\\voting\\vote1.dat"));
    }
    catch(IOException e) {
    System.err.println("Caught an exception");
    }

    String s, s2 = new String();

    String cand = new String();
    String temp = new String();
    boolean cfound;
    cfound=false;

    int x=0;
    //start of code
    //read everything from file
    try
    {

    while((s = in.readLine())!= null)
    {

    s2 += s + "\n";
    //end of read everything- s2 contains all of file

    //locate candidates
    if (s.equals("<CAND>"))
    {
    cfound=true;
    continue;
    }
    if( s.equals("</CAND>"))
    cfound=false;

    if(cfound == true)
    {
    cand+=s + "\n";
    cinfo[1][x]=s;
    x++;
    }
    in.close();
    }
    }
    catch(IOException e)
    {
    System.err.println("Caught an exception");
    }

    }
    /////////////////////////////////////////////////////////////


    ///////////////PRINT STUFF//////////////////////////////
    static void printStuff(){
    //System.out.println(x);
    System.out.println(cinfo[1][3]);
    System.out.println("\n");
    }


    //////////////////////////////////////////////////////


    }


  • Registered Users Posts: 1,361 ✭✭✭tw0nk


    thanks folks, everything working smoothly now! phewww


  • Advertisement
Advertisement