uniquity
02-21-2009, 09:46 PM
i'm writing a program that has an inventory that contains a bunch of products. everything in my program works except the last part where i'm trying to read from a file, and initialize products based on data read from Scanner from a certain text file. the data is read from scanner, and the scanner is then passed to a toObject method which is the method that is supposed to initialize the object. i don't quite know where i'm messing up, but the products are not getting initialized so i'm assuming my toObject method or one of my "addtoinventory" methods. if someone could take a look and point out any glaring mistakes i'd greatly appreciate it. i've attached the methods and the file that is being read from. if you need more code, let me know and i'll post em.
This is the code for the first add method.
public ArrayList<String> addNew( String filename, char delimiter )
{
ArrayList<String> errors = new ArrayList<String>();
int lineNum = 0;
File file = new File( filename );
try
{
Scanner sc = new Scanner( file );
while( sc.hasNextLine() )
{
lineNum++;
String delim = ( "[" + delimiter + "]" );
Scanner sc2 = new Scanner( sc.next() );
sc2.useDelimiter( delim );
while( sc2.hasNext() )
{
if( sc2.next().equals( "Book" ) )
{
Book b = new Book( Integer.parseInt(sc2.next()) );
b.toObject( sc );
this.addNew( b );
}
if( sc2.next().equals( "BookOnTape" ) )
{
BookOnTape bot = new BookOnTape( Integer.parseInt( sc2.next() ) );
bot.toObject( sc );
this.addNew( bot );
}
if( sc2.next().equals( "CD" ) )
{
CD cd = new CD( Integer.parseInt( sc2.next() ) );
cd.toObject( sc );
this.addNew( cd );
}
}
}
}
catch( Exception exc )
{
errors.add( "Line " + lineNum + " - " /*+ sc.next()*/ );
}
return errors;
} //end method addNew
This is the code for ONE of my toObject methods, but i have one in every class that represents a type of Product.
public void toObject( Scanner input )
{
ArrayList<String> tokens = new ArrayList<String>();
String delimiter = ( "[" + "," + "]" );
input.useDelimiter( delimiter );
while( input.hasNext() )
{
tokens.add( input.next().trim() );
}
prodID = Integer.parseInt( tokens.get( 0 ) );
numInStock = Integer.parseInt( tokens.get( 1 ) );
description = tokens.get( 2 );
wholesalePrice = Double.parseDouble( tokens.get( 3 ) );
retailPrice = Double.parseDouble( tokens.get( 4 ) );
}
And this is the text file that is being read from.
Book|2248Description 1000|1000.11|1000.22|Some Title 1000|Some Publisher 1000|Author Last1000|Author Middle1000|Author First1000|1000|2
Book|2248Description 1000|1000.11|1000.22|Some Title 1000|Some Publisher 1000|Author Last1000|Author Middle1000|Author First1000|1000|2
BookOnTape|4248Description 2000|2000.11|2000.22|Some Title 2000|Some Publisher 2000|Author Last2000|Author Middle2000|Author First2000|Reader Last2000|Reader Middle2000|Reader First2000|2000|0
CD|6248Description 3000|3000.11|3000.22Artist Last3000|Artist Middle3000|Artist First3000
Book|2250Description 1001|1001.11|1001.22|Some Title 1001|Some Publisher 1001|Author Last1001|Author Middle1001|Author First1001|1001|2
BookOnTape|4250Description 2001|2001.11|2001.22|Some Title 2001|Some Publisher 2001|Author Last2001|Author Middle2001|Author First2001|Reader Last2001|Reader Middle2001|Reader First2001|2001|0
CD|6250Description 3001|3001.11|3001.22Artist Last3001|Artist Middle3001|Artist First3001
Book|2252Description 1002|1002.11|1002.22|Some Title 1002|Some Publisher 1002|Author Last1002|Author Middle1002|Author First1002|1002|2
BookOnTape|4252Description 2002|2002.11|2002.22|Some Title 2002|Some Publisher 2002|Author Last2002|Author Middle2002|Author First2002|Reader Last2002|Reader Middle2002|Reader First2002|2002|0
CD|6252Description 3002|3002.11|3002.22Artist Last3002|Artist Middle3002|Artist First3002
Book|2254Description 1003|1003.11|1003.22|Some Title 1003|Some Publisher 1003|Author Last1003|Author Middle1003|Author First1003|1003|2
BookOnTape|4254Description 2003|2003.11|2003.22|Some Title 2003|Some Publisher 2003|Author Last2003|Author Middle2003|Author First2003|Reader Last2003|Reader Middle2003|Reader First2003|2003|0
CD|6254Description 3003|3003.11|3003.22Artist Last3003|Artist Middle3003|Artist First3003
Book|2256Description 1004|1004.11|1004.22|Some Title 1004|Some Publisher 1004|Author Last1004|Author Middle1004|Author First1004|1004|2
BookOnTape|4256Description 2004|2004.11|2004.22|Some Title 2004|Some Publisher 2004|Author Last2004|Author Middle2004|Author First2004|Reader Last2004|Reader Middle2004|Reader First2004|2004|0
CD|6256Description 3004|3004.11|3004.22Artist Last3004|Artist Middle3004|Artist First3004
Book|2258Description 1005|1005.11|1005.22|Some Title 1005|Some Publisher 1005|Author Last1005|Author Middle1005|Author First1005|1005|2
BookOnTape|4258Description 2005|2005.11|2005.22|Some Title 2005|Some Publisher 2005|Author Last2005|Author Middle2005|Author First2005|Reader Last2005|Reader Middle2005|Reader First2005|2005|0
CD|6258Description 3005|3005.11|3005.22Artist Last3005|Artist Middle3005|Artist First3005
Book|2260Description 1006|1006.11|1006.22|Some Title 1006|Some Publisher 1006|Author Last1006|Author Middle1006|Author First1006|1006|2
BookOnTape|4260Description 2006|2006.11|2006.22|Some Title 2006|Some Publisher 2006|Author Last2006|Author Middle2006|Author First2006|Reader Last2006|Reader Middle2006|Reader First2006|2006|0
CD|6260Description 3006|3006.11|3006.22Artist Last3006|Artist Middle3006|Artist First3006
Book|2262Description 1007|1007.11|1007.22|Some Title 1007|Some Publisher 1007|Author Last1007|Author Middle1007|Author First1007|1007|2
BookOnTape|4262Description 2007|2007.11|2007.22|Some Title 2007|Some Publisher 2007|Author Last2007|Author Middle2007|Author First2007|Reader Last2007|Reader Middle2007|Reader First2007|2007|0
CD|6262Description 3007|3007.11|3007.22Artist Last3007|Artist Middle3007|Artist First3007
Book|2264Description 1008|1008.11|1008.22|Some Title 1008|Some Publisher 1008|Author Last1008|Author Middle1008|Author First1008|1008|2
BookOnTape|4264Description 2008|2008.11|2008.22|Some Title 2008|Some Publisher 2008|Author Last2008|Author Middle2008|Author First2008|Reader Last2008|Reader Middle2008|Reader First2008|2008|0
CD|6264Description 3008|3008.11|3008.22Artist Last3008|Artist Middle3008|Artist First3008
Book|2266Description 1009|1009.11|1009.22|Some Title 1009|Some Publisher 1009|Author Last1009|Author Middle1009|Author First1009|1009|2
BookOnTape|4266Description 2009|2009.11|2009.22|Some Title 2009|Some Publisher 2009|Author Last2009|Author Middle2009|Author First2009|Reader Last2009|Reader Middle2009|Reader First2009|2009|0
CD|6266Description 3009|3009.11|3009.22Artist Last3009|Artist Middle3009|Artist First3009
Thanks in advance!
This is the code for the first add method.
public ArrayList<String> addNew( String filename, char delimiter )
{
ArrayList<String> errors = new ArrayList<String>();
int lineNum = 0;
File file = new File( filename );
try
{
Scanner sc = new Scanner( file );
while( sc.hasNextLine() )
{
lineNum++;
String delim = ( "[" + delimiter + "]" );
Scanner sc2 = new Scanner( sc.next() );
sc2.useDelimiter( delim );
while( sc2.hasNext() )
{
if( sc2.next().equals( "Book" ) )
{
Book b = new Book( Integer.parseInt(sc2.next()) );
b.toObject( sc );
this.addNew( b );
}
if( sc2.next().equals( "BookOnTape" ) )
{
BookOnTape bot = new BookOnTape( Integer.parseInt( sc2.next() ) );
bot.toObject( sc );
this.addNew( bot );
}
if( sc2.next().equals( "CD" ) )
{
CD cd = new CD( Integer.parseInt( sc2.next() ) );
cd.toObject( sc );
this.addNew( cd );
}
}
}
}
catch( Exception exc )
{
errors.add( "Line " + lineNum + " - " /*+ sc.next()*/ );
}
return errors;
} //end method addNew
This is the code for ONE of my toObject methods, but i have one in every class that represents a type of Product.
public void toObject( Scanner input )
{
ArrayList<String> tokens = new ArrayList<String>();
String delimiter = ( "[" + "," + "]" );
input.useDelimiter( delimiter );
while( input.hasNext() )
{
tokens.add( input.next().trim() );
}
prodID = Integer.parseInt( tokens.get( 0 ) );
numInStock = Integer.parseInt( tokens.get( 1 ) );
description = tokens.get( 2 );
wholesalePrice = Double.parseDouble( tokens.get( 3 ) );
retailPrice = Double.parseDouble( tokens.get( 4 ) );
}
And this is the text file that is being read from.
Book|2248Description 1000|1000.11|1000.22|Some Title 1000|Some Publisher 1000|Author Last1000|Author Middle1000|Author First1000|1000|2
Book|2248Description 1000|1000.11|1000.22|Some Title 1000|Some Publisher 1000|Author Last1000|Author Middle1000|Author First1000|1000|2
BookOnTape|4248Description 2000|2000.11|2000.22|Some Title 2000|Some Publisher 2000|Author Last2000|Author Middle2000|Author First2000|Reader Last2000|Reader Middle2000|Reader First2000|2000|0
CD|6248Description 3000|3000.11|3000.22Artist Last3000|Artist Middle3000|Artist First3000
Book|2250Description 1001|1001.11|1001.22|Some Title 1001|Some Publisher 1001|Author Last1001|Author Middle1001|Author First1001|1001|2
BookOnTape|4250Description 2001|2001.11|2001.22|Some Title 2001|Some Publisher 2001|Author Last2001|Author Middle2001|Author First2001|Reader Last2001|Reader Middle2001|Reader First2001|2001|0
CD|6250Description 3001|3001.11|3001.22Artist Last3001|Artist Middle3001|Artist First3001
Book|2252Description 1002|1002.11|1002.22|Some Title 1002|Some Publisher 1002|Author Last1002|Author Middle1002|Author First1002|1002|2
BookOnTape|4252Description 2002|2002.11|2002.22|Some Title 2002|Some Publisher 2002|Author Last2002|Author Middle2002|Author First2002|Reader Last2002|Reader Middle2002|Reader First2002|2002|0
CD|6252Description 3002|3002.11|3002.22Artist Last3002|Artist Middle3002|Artist First3002
Book|2254Description 1003|1003.11|1003.22|Some Title 1003|Some Publisher 1003|Author Last1003|Author Middle1003|Author First1003|1003|2
BookOnTape|4254Description 2003|2003.11|2003.22|Some Title 2003|Some Publisher 2003|Author Last2003|Author Middle2003|Author First2003|Reader Last2003|Reader Middle2003|Reader First2003|2003|0
CD|6254Description 3003|3003.11|3003.22Artist Last3003|Artist Middle3003|Artist First3003
Book|2256Description 1004|1004.11|1004.22|Some Title 1004|Some Publisher 1004|Author Last1004|Author Middle1004|Author First1004|1004|2
BookOnTape|4256Description 2004|2004.11|2004.22|Some Title 2004|Some Publisher 2004|Author Last2004|Author Middle2004|Author First2004|Reader Last2004|Reader Middle2004|Reader First2004|2004|0
CD|6256Description 3004|3004.11|3004.22Artist Last3004|Artist Middle3004|Artist First3004
Book|2258Description 1005|1005.11|1005.22|Some Title 1005|Some Publisher 1005|Author Last1005|Author Middle1005|Author First1005|1005|2
BookOnTape|4258Description 2005|2005.11|2005.22|Some Title 2005|Some Publisher 2005|Author Last2005|Author Middle2005|Author First2005|Reader Last2005|Reader Middle2005|Reader First2005|2005|0
CD|6258Description 3005|3005.11|3005.22Artist Last3005|Artist Middle3005|Artist First3005
Book|2260Description 1006|1006.11|1006.22|Some Title 1006|Some Publisher 1006|Author Last1006|Author Middle1006|Author First1006|1006|2
BookOnTape|4260Description 2006|2006.11|2006.22|Some Title 2006|Some Publisher 2006|Author Last2006|Author Middle2006|Author First2006|Reader Last2006|Reader Middle2006|Reader First2006|2006|0
CD|6260Description 3006|3006.11|3006.22Artist Last3006|Artist Middle3006|Artist First3006
Book|2262Description 1007|1007.11|1007.22|Some Title 1007|Some Publisher 1007|Author Last1007|Author Middle1007|Author First1007|1007|2
BookOnTape|4262Description 2007|2007.11|2007.22|Some Title 2007|Some Publisher 2007|Author Last2007|Author Middle2007|Author First2007|Reader Last2007|Reader Middle2007|Reader First2007|2007|0
CD|6262Description 3007|3007.11|3007.22Artist Last3007|Artist Middle3007|Artist First3007
Book|2264Description 1008|1008.11|1008.22|Some Title 1008|Some Publisher 1008|Author Last1008|Author Middle1008|Author First1008|1008|2
BookOnTape|4264Description 2008|2008.11|2008.22|Some Title 2008|Some Publisher 2008|Author Last2008|Author Middle2008|Author First2008|Reader Last2008|Reader Middle2008|Reader First2008|2008|0
CD|6264Description 3008|3008.11|3008.22Artist Last3008|Artist Middle3008|Artist First3008
Book|2266Description 1009|1009.11|1009.22|Some Title 1009|Some Publisher 1009|Author Last1009|Author Middle1009|Author First1009|1009|2
BookOnTape|4266Description 2009|2009.11|2009.22|Some Title 2009|Some Publisher 2009|Author Last2009|Author Middle2009|Author First2009|Reader Last2009|Reader Middle2009|Reader First2009|2009|0
CD|6266Description 3009|3009.11|3009.22Artist Last3009|Artist Middle3009|Artist First3009
Thanks in advance!