Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Problem with IOException

PerforatedPerforated Member Posts: 19
I'm writing a program that is meant to do a number of things. In order to make this as easy as possible I've written a rudimentary command-line parser.

And this is where my problem starts. Tha parser takes a command with it's parameters, separates them and after this is done my intention was to forward this command to specific private methods within the parser. The code looks like this.

------------------------------------------------------------------------
public class Parser_Cards
{
enum Command {shuffle, reset, deal, play, exit, help}

private String[] comName = new String[6];
private char[] argument = new char[2];
File("Commands.cdd"));

private boolean test = false;
private boolean fail = false;
private int i;

private Command coms;
private String realCom, args;

//Constructor
public Parser_Cards()
{
}


//The "main" method of the class, takes a command and verifies
//if it is corrent or not.

public boolean commander(String com) throws IOException
{
//Reads the available commands from a file.
Scanner comScan = new Scanner(new File("Commands.cdd"));

//I would very much like to add the below initialization
//to all the other initializations before the
//constructor, but all I get when I do is an error
//error message telling me the following (keep
//reading after the code)
DeckOfCards doc = new DeckOfCards();
------------------------------------------------------------------------

Unhandled exception type IOException Parser_Cards.java line 37

I've tried to add a "throws IOException to the class declaration but that produces even more error messages. What should I do to fix this?


Thank you in advance.

Comments

  • VilanyeVilanye Member Posts: 684
    : I'm writing a program that is meant to do a number of things. In order to make this as easy as possible I've written a rudimentary command-line parser.
    :
    : And this is where my problem starts. Tha parser takes a command with it's parameters, separates them and after this is done my intention was to forward this command to specific private methods within the parser. The code looks like this.
    :
    : ------------------------------------------------------------------------
    : public class Parser_Cards
    : {
    : enum Command {shuffle, reset, deal, play, exit, help}
    :
    : private String[] comName = new String[6];
    : private char[] argument = new char[2];
    : File("Commands.cdd"));
    :
    : private boolean test = false;
    : private boolean fail = false;
    : private int i;
    :
    : private Command coms;
    : private String realCom, args;
    :
    : //Constructor
    : public Parser_Cards()
    : {
    : }
    :
    :
    : //The "main" method of the class, takes a command and verifies
    : //if it is corrent or not.
    :
    : public boolean commander(String com) throws IOException
    : {
    : //Reads the available commands from a file.
    : Scanner comScan = new Scanner(new File("Commands.cdd"));
    :
    : //I would very much like to add the below initialization
    : //to all the other initializations before the
    : //constructor, but all I get when I do is an error
    : //error message telling me the following (keep
    : //reading after the code)
    : DeckOfCards doc = new DeckOfCards();
    : ------------------------------------------------------------------------
    :
    : Unhandled exception type IOException Parser_Cards.java line 37
    :
    : I've tried to add a "throws IOException to the class declaration but that produces even more error messages. What should I do to fix this?
    :
    :
    : Thank you in advance.
    :


    post DeckOfCards contructor
    [italic][blue]Just my 2 bits[/blue][italic]

  • PerforatedPerforated Member Posts: 19
    [b][red]This message was edited by Perforated at 2005-9-30 15:57:58[/red][/b][hr]
    : : I'm writing a program that is meant to do a number of things. In order to make this as easy as possible I've written a rudimentary command-line parser.
    : :
    : : And this is where my problem starts. Tha parser takes a command with it's parameters, separates them and after this is done my intention was to forward this command to specific private methods within the parser. The code looks like this.
    : :
    : : ------------------------------------------------------------------------
    : : public class Parser_Cards
    : : {
    : : enum Command {shuffle, reset, deal, play, exit, help}
    : :
    : : private String[] comName = new String[6];
    : : private char[] argument = new char[2];
    : : File("Commands.cdd"));
    : :
    : : private boolean test = false;
    : : private boolean fail = false;
    : : private int i;
    : :
    : : private Command coms;
    : : private String realCom, args;
    : :
    : : //Constructor
    : : public Parser_Cards()
    : : {
    : : }
    : :
    : :
    : : //The "main" method of the class, takes a command and verifies
    : : //if it is corrent or not.
    : :
    : : public boolean commander(String com) throws IOException
    : : {
    : : //Reads the available commands from a file.
    : : Scanner comScan = new Scanner(new File("Commands.cdd"));
    : :
    : : //I would very much like to add the below initialization
    : : //to all the other initializations before the
    : : //constructor, but all I get when I do is an error
    : : //error message telling me the following (keep
    : : //reading after the code)
    : : DeckOfCards doc = new DeckOfCards();
    : : ------------------------------------------------------------------------
    : :
    : : Unhandled exception type IOException Parser_Cards.java line 37
    : :
    : : I've tried to add a "throws IOException to the class declaration but that produces even more error messages. What should I do to fix this?
    : :
    : :
    : : Thank you in advance.
    : :
    :
    :
    : post DeckOfCards contructor
    : [italic][blue]Just my 2 bits[/blue][italic]
    :
    :

    //Deck of cards constructor
    public DeckOfCards() throws IOException
    {
    Scanner cardScan = new Scanner(new File("deckSetUp.dkp"));

    for (int i = 0; i <=12; i++)
    {
    String tempType = cardScan.next();
    int face = cardScan.nextInt();

    cards[i] = new Card(face, "Hearts", tempType);
    cards[i+13] = new Card(face, "Diamonds", tempType);
    cards[i+26] = new Card(face, "Spades", tempType);
    cards[i+39] = new Card(face, "Clubs", tempType);
    }
    }
    As requested :)
  • VilanyeVilanye Member Posts: 684
    : [b][red]This message was edited by Perforated at 2005-9-30 15:57:58[/red][/b][hr]
    : : : I'm writing a program that is meant to do a number of things. In order to make this as easy as possible I've written a rudimentary command-line parser.
    : : :
    : : : And this is where my problem starts. Tha parser takes a command with it's parameters, separates them and after this is done my intention was to forward this command to specific private methods within the parser. The code looks like this.
    : : :
    : : : ------------------------------------------------------------------------
    : : : public class Parser_Cards
    : : : {
    : : : enum Command {shuffle, reset, deal, play, exit, help}
    : : :
    : : : private String[] comName = new String[6];
    : : : private char[] argument = new char[2];
    : : : File("Commands.cdd"));
    : : :
    : : : private boolean test = false;
    : : : private boolean fail = false;
    : : : private int i;
    : : :
    : : : private Command coms;
    : : : private String realCom, args;
    : : :
    : : : //Constructor
    : : : public Parser_Cards()
    : : : {
    : : : }
    : : :
    : : :
    : : : //The "main" method of the class, takes a command and verifies
    : : : //if it is corrent or not.
    : : :
    : : : public boolean commander(String com) throws IOException
    : : : {
    : : : //Reads the available commands from a file.
    : : : Scanner comScan = new Scanner(new File("Commands.cdd"));
    : : :
    : : : //I would very much like to add the below initialization
    : : : //to all the other initializations before the
    : : : //constructor, but all I get when I do is an error
    : : : //error message telling me the following (keep
    : : : //reading after the code)
    : : : DeckOfCards doc = new DeckOfCards();
    : : : ------------------------------------------------------------------------
    : : :
    : : : Unhandled exception type IOException Parser_Cards.java line 37
    : : :
    : : : I've tried to add a "throws IOException to the class declaration but that produces even more error messages. What should I do to fix this?
    : : :
    : : :
    : : : Thank you in advance.
    : : :
    : :
    : :
    : : post DeckOfCards contructor
    : : [italic][blue]Just my 2 bits[/blue][italic]
    : :
    : :
    :
    : //Deck of cards constructor
    : public DeckOfCards() throws IOException
    : {
    : Scanner cardScan = new Scanner(new File("deckSetUp.dkp"));
    :
    : for (int i = 0; i <=12; i++)
    : {
    : String tempType = cardScan.next();
    : int face = cardScan.nextInt();
    :
    : cards[i] = new Card(face, "Hearts", tempType);
    : cards[i+13] = new Card(face, "Diamonds", tempType);
    : cards[i+26] = new Card(face, "Spades", tempType);
    : cards[i+39] = new Card(face, "Clubs", tempType);
    : }
    : }
    : As requested :)
    :


    hmm is there anything in Card that could throw an exception? What is the entire error message, including a printStackTrace()?
    [italic][blue]Just my 2 bits[/blue][italic]

  • PerforatedPerforated Member Posts: 19
    : hmm is there anything in Card that could throw an exception? What is the : entire error message, including a printStackTrace()?
    : [italic][blue]Just my 2 bits[/blue][italic]

    Nothing in the Card class should throw an exception, not that I'm aware of anyways. This is the code for the Card class (The comments are in swedish but you shouldn't need them anyways):

    public class Card
    {
    private String suit, type, stringRep;
    private int faceValue = 0;


    //Konstruktr, initierar samtliga variabler som bestmmer
    //kortets utformning
    public Card(int faceValue, String suit, String type)
    {
    this.faceValue = faceValue;
    this.suit = suit;
    this.type = type;
    }


    //En metod fr att ta reda p vilken frg kortet tillhr
    public String getSuit()
    {
    return suit;
    }


    //En metod fr att ta reda p vad kortets vrde r
    public int getFaceValue()
    {
    return faceValue;
    }


    //En metod fr att ta reda p vilken typ av kort det r
    public String getType()
    {
    return type;
    }


    //En mutator fr att stta kortets frg
    public void setSuit(String suit)
    {
    this.suit = suit;
    }


    //En mutator fr att stta kortets vrde
    public void setFaceValue(int faceValue)
    {
    this.faceValue = faceValue;
    }


    //En mutator fr att stta kortets typ
    public void setType(String type)
    {
    this.type = type;
    }


    //Modifierad toString-metod
    public String toString()
    {
    stringRep = (type + " of " + suit + " " + faceValue);

    return stringRep;
    }
    }

    The complete error message is:
    ------------------------------------------------------------------------
    Exception in thread "main" java.lang.Error: Unresolved compilation problem:
    Unhandled exception type IOException

    at Parser_Cards.(Parser_Cards.java:37)
    ------------------------------------------------------------------------

    line 37 is the following line:

    DeckOfCards doc = new DeckOfCards();

    This instantiation is placed before the constructor (right after the class declaration). I have no idea what a printStackTrace() is.

    Thanks again
  • VilanyeVilanye Member Posts: 684
    : : hmm is there anything in Card that could throw an exception? What is the : entire error message, including a printStackTrace()?
    : : [italic][blue]Just my 2 bits[/blue][italic]
    :
    : Nothing in the Card class should throw an exception, not that I'm aware of anyways. This is the code for the Card class (The comments are in swedish but you shouldn't need them anyways):
    :
    : public class Card
    : {
    : private String suit, type, stringRep;
    : private int faceValue = 0;
    :
    :
    : //Konstruktr, initierar samtliga variabler som bestmmer
    : //kortets utformning
    : public Card(int faceValue, String suit, String type)
    : {
    : this.faceValue = faceValue;
    : this.suit = suit;
    : this.type = type;
    : }
    :
    :
    : //En metod fr att ta reda p vilken frg kortet tillhr
    : public String getSuit()
    : {
    : return suit;
    : }
    :
    :
    : //En metod fr att ta reda p vad kortets vrde r
    : public int getFaceValue()
    : {
    : return faceValue;
    : }
    :
    :
    : //En metod fr att ta reda p vilken typ av kort det r
    : public String getType()
    : {
    : return type;
    : }
    :
    :
    : //En mutator fr att stta kortets frg
    : public void setSuit(String suit)
    : {
    : this.suit = suit;
    : }
    :
    :
    : //En mutator fr att stta kortets vrde
    : public void setFaceValue(int faceValue)
    : {
    : this.faceValue = faceValue;
    : }
    :
    :
    : //En mutator fr att stta kortets typ
    : public void setType(String type)
    : {
    : this.type = type;
    : }
    :
    :
    : //Modifierad toString-metod
    : public String toString()
    : {
    : stringRep = (type + " of " + suit + " " + faceValue);
    :
    : return stringRep;
    : }
    : }
    :
    : The complete error message is:
    : ------------------------------------------------------------------------
    : Exception in thread "main" java.lang.Error: Unresolved compilation problem:
    : Unhandled exception type IOException
    :
    : at Parser_Cards.(Parser_Cards.java:37)
    : ------------------------------------------------------------------------
    :
    : line 37 is the following line:
    :
    : DeckOfCards doc = new DeckOfCards();
    :
    : This instantiation is placed before the constructor (right after the class declaration). I have no idea what a printStackTrace() is.
    :
    : Thanks again
    :


    printStackTrace() is a method in the Exception classes,it traces through the stack after an exception is thrown. Try putting a try/catch around line 37 and call printStackTrace(). It will give more details, and make it easier to figure out what is going on.
    [italic][blue]Just my 2 bits[/blue][italic]

  • PerforatedPerforated Member Posts: 19
    : : : hmm is there anything in Card that could throw an exception? What is the : entire error message, including a printStackTrace()?
    : : : [italic][blue]Just my 2 bits[/blue][italic]
    : :
    : : Nothing in the Card class should throw an exception, not that I'm aware of anyways. This is the code for the Card class (The comments are in swedish but you shouldn't need them anyways):
    : :
    : : public class Card
    : : {
    : : private String suit, type, stringRep;
    : : private int faceValue = 0;
    : :
    : :
    : : //Konstruktr, initierar samtliga variabler som bestmmer
    : : //kortets utformning
    : : public Card(int faceValue, String suit, String type)
    : : {
    : : this.faceValue = faceValue;
    : : this.suit = suit;
    : : this.type = type;
    : : }
    : :
    : :
    : : //En metod fr att ta reda p vilken frg kortet tillhr
    : : public String getSuit()
    : : {
    : : return suit;
    : : }
    : :
    : :
    : : //En metod fr att ta reda p vad kortets vrde r
    : : public int getFaceValue()
    : : {
    : : return faceValue;
    : : }
    : :
    : :
    : : //En metod fr att ta reda p vilken typ av kort det r
    : : public String getType()
    : : {
    : : return type;
    : : }
    : :
    : :
    : : //En mutator fr att stta kortets frg
    : : public void setSuit(String suit)
    : : {
    : : this.suit = suit;
    : : }
    : :
    : :
    : : //En mutator fr att stta kortets vrde
    : : public void setFaceValue(int faceValue)
    : : {
    : : this.faceValue = faceValue;
    : : }
    : :
    : :
    : : //En mutator fr att stta kortets typ
    : : public void setType(String type)
    : : {
    : : this.type = type;
    : : }
    : :
    : :
    : : //Modifierad toString-metod
    : : public String toString()
    : : {
    : : stringRep = (type + " of " + suit + " " + faceValue);
    : :
    : : return stringRep;
    : : }
    : : }
    : :
    : : The complete error message is:
    : : ------------------------------------------------------------------------
    : : Exception in thread "main" java.lang.Error: Unresolved compilation problem:
    : : Unhandled exception type IOException
    : :
    : : at Parser_Cards.(Parser_Cards.java:37)
    : : ------------------------------------------------------------------------
    : :
    : : line 37 is the following line:
    : :
    : : DeckOfCards doc = new DeckOfCards();
    : :
    : : This instantiation is placed before the constructor (right after the class declaration). I have no idea what a printStackTrace() is.
    : :
    : : Thanks again
    : :
    :
    :
    : printStackTrace() is a method in the Exception classes,it traces through the stack after an exception is thrown. Try putting a try/catch around line 37 and call printStackTrace(). It will give more details, and make it easier to figure out what is going on.
    : [italic][blue]Just my 2 bits[/blue][italic]
    :
    :


    I've tried but I get even more errors when doing this, is it even possible to have a block statement in a class that is not part of a method?
  • VilanyeVilanye Member Posts: 684
    [b][red]This message was edited by Vilanye at 2005-10-1 15:41:28[/red][/b][hr]
    Doing this causes more errors?

    try
    {
    DeckOfCards doc = new DeckOfCards();
    }
    catch(IOException e)
    {
    e.printStackTrace();
    }

    What are the new errors?

    Sorry I haven't been much help. Often the solution for these sorts of problems are not where the compiler say they are but are caused in places you wouldn't think they are. I did see this in your original post, what is this supposed to do: File("Commands.cdd")); ?
    [italic][blue]Just my 2 bits[/blue][italic]



  • PerforatedPerforated Member Posts: 19
    : [b][red]This message was edited by Vilanye at 2005-10-1 15:41:28[/red][/b][hr]
    : Doing this causes more errors?
    :
    : try
    : {
    : DeckOfCards doc = new DeckOfCards();
    : }
    : catch(IOException e)
    : {
    : e.printStackTrace();
    : }
    :
    : What are the new errors?
    :
    : Sorry I haven't been much help. Often the solution for these sorts of problems are not where the compiler say they are but are caused in places you wouldn't think they are. I did see this in your original post, what is this supposed to do: File("Commands.cdd")); ?
    : [italic][blue]Just my 2 bits[/blue][italic]
    :
    :
    :
    :


    The Scanner initialization including File("Commands.cdd")) is rather important as it's where all the possible commands are drawn from. In order to make this parser work for as many programs as possible with little changes I decided that it would be easier editing a file than it would be to edit the source code every time a new command was to be included.

    The new errors are:


    Syntax error on token "{", { expected after this token Parser_Cards.java Cards line 13

    Return type for the method is missing Parser_Cards.java Cards line 55

    I really have no idea what might be wrong...

  • VilanyeVilanye Member Posts: 684
    : : [b][red]This message was edited by Vilanye at 2005-10-1 15:41:28[/red][/b][hr]
    : : Doing this causes more errors?
    : :
    : : try
    : : {
    : : DeckOfCards doc = new DeckOfCards();
    : : }
    : : catch(IOException e)
    : : {
    : : e.printStackTrace();
    : : }
    : :
    : : What are the new errors?
    : :
    : : Sorry I haven't been much help. Often the solution for these sorts of problems are not where the compiler say they are but are caused in places you wouldn't think they are. I did see this in your original post, what is this supposed to do: File("Commands.cdd")); ?
    : : [italic][blue]Just my 2 bits[/blue][italic]
    : :
    : :
    : :
    : :
    :
    :
    : The Scanner initialization including File("Commands.cdd")) is rather important as it's where all the possible commands are drawn from. In order to make this parser work for as many programs as possible with little changes I decided that it would be easier editing a file than it would be to edit the source code every time a new command was to be included.

    [blue]So you are trying to create a file object for that file so you can pass that into the scanner constructor? That is not how you do it. File file = new File("Commands.cdd"), but you created a file object when you instantiated Scanner. It also has an extra ), but File("Commands.cdd")) does nothing, and should generate several compiler errors, unless that is something bizzare with Scanner, I have never had a need to use Scanner and I use 1.4.2 not 1.5, so I can't test it, but there is nothing in the java docs. [/blue]
    :
    : The new errors are:
    :
    :
    : Syntax error on token "{", { expected after this token Parser_Cards.java Cards line 13
    :
    : Return type for the method is missing Parser_Cards.java Cards line 55
    :
    : I really have no idea what might be wrong...
    :
    :


    Post all the code, but post it each class in seperate code tags, that makes it easier to read.
    [italic][blue]Just my 2 bits[/blue][italic]

  • PerforatedPerforated Member Posts: 19
    : : : [b][red]This message was edited by Vilanye at 2005-10-1 15:41:28[/red][/b][hr]
    : : : Doing this causes more errors?
    : : :
    : : : try
    : : : {
    : : : DeckOfCards doc = new DeckOfCards();
    : : : }
    : : : catch(IOException e)
    : : : {
    : : : e.printStackTrace();
    : : : }
    : : :
    : : : What are the new errors?
    : : :
    : : : Sorry I haven't been much help. Often the solution for these sorts of problems are not where the compiler say they are but are caused in places you wouldn't think they are. I did see this in your original post, what is this supposed to do: File("Commands.cdd")); ?
    : : : [italic][blue]Just my 2 bits[/blue][italic]
    : : :
    : : :
    : : :
    : : :
    : :
    : :
    : : The Scanner initialization including File("Commands.cdd")) is rather important as it's where all the possible commands are drawn from. In order to make this parser work for as many programs as possible with little changes I decided that it would be easier editing a file than it would be to edit the source code every time a new command was to be included.
    :
    : [blue]So you are trying to create a file object for that file so you can pass that into the scanner constructor? That is not how you do it. File file = new File("Commands.cdd"), but you created a file object when you instantiated Scanner. It also has an extra ), but File("Commands.cdd")) does nothing, and should generate several compiler errors, unless that is something bizzare with Scanner, I have never had a need to use Scanner and I use 1.4.2 not 1.5, so I can't test it, but there is nothing in the java docs. [/blue]
    : :
    : : The new errors are:
    : :
    : :
    : : Syntax error on token "{", { expected after this token Parser_Cards.java Cards line 13
    : :
    : : Return type for the method is missing Parser_Cards.java Cards line 55
    : :
    : : I really have no idea what might be wrong...
    : :
    : :
    :
    :
    : Post all the code, but post it each class in seperate code tags, that makes it easier to read.
    : [italic][blue]Just my 2 bits[/blue][italic]
    :
    :


    I managed to solve my problem by declaring the DeckOfCards-object before the constructor and instantiating the object in the constructor, thanks for all the help though.
Sign In or Register to comment.