Java

Moderators: zibadian
Number of threads: 7818
Number of posts: 18218

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
Problem with IOException Posted by Perforated on 30 Sept 2005 at 2:20 PM
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.
Report
Re: Problem with IOException Posted by Vilanye on 30 Sept 2005 at 2:47 PM
: 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
Just my 2 bits

Report
Re: Problem with IOException Posted by Perforated on 30 Sept 2005 at 3:57 PM
This message was edited by Perforated at 2005-9-30 15:57:58

: : 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
: Just my 2 bits
:
:

//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 :)
Report
Re: Problem with IOException Posted by Vilanye on 30 Sept 2005 at 7:00 PM
: This message was edited by Perforated at 2005-9-30 15:57:58

: : : 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
: : Just my 2 bits
: :
: :
:
: //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()?
Just my 2 bits

Report
Re: Problem with IOException Posted by Perforated on 1 Oct 2005 at 11:14 AM
: hmm is there anything in Card that could throw an exception? What is the : entire error message, including a printStackTrace()?
: Just my 2 bits

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 + "\t\t" + 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.<init>(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
Report
Re: Problem with IOException Posted by Vilanye on 1 Oct 2005 at 12:38 PM
: : hmm is there anything in Card that could throw an exception? What is the : entire error message, including a printStackTrace()?
: : Just my 2 bits
:
: 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 + "\t\t" + 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.<init>(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.
Just my 2 bits

Report
Re: Problem with IOException Posted by Perforated on 1 Oct 2005 at 2:26 PM
: : : hmm is there anything in Card that could throw an exception? What is the : entire error message, including a printStackTrace()?
: : : Just my 2 bits
: :
: : 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 + "\t\t" + 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.<init>(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.
: Just my 2 bits
:
:


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?
Report
Re: Problem with IOException Posted by Vilanye on 1 Oct 2005 at 3:37 PM
This message was edited by Vilanye at 2005-10-1 15:41:28

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")); ?
Just my 2 bits



Report
Re: Problem with IOException Posted by Perforated on 1 Oct 2005 at 7:28 PM
: This message was edited by Vilanye at 2005-10-1 15:41:28

: 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")); ?
: Just my 2 bits
:
:
:
:


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...

Report
Re: Problem with IOException Posted by Vilanye on 1 Oct 2005 at 7:57 PM
: : This message was edited by Vilanye at 2005-10-1 15:41:28

: : 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")); ?
: : Just my 2 bits
: :
: :
: :
: :
:
:
: 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.

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.
:
: 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.
Just my 2 bits

Report
Re: Problem with IOException Posted by Perforated on 3 Oct 2005 at 4:27 PM
: : : This message was edited by Vilanye at 2005-10-1 15:41:28

: : : 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")); ?
: : : Just my 2 bits
: : :
: : :
: : :
: : :
: :
: :
: : 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.
:
: 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.
: :
: : 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.
: Just my 2 bits
:
:


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.



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.