Re: Need assistance with arrays
RookThis wrote:
I made the suggested changes, but it didn't make a difference.
Anything else I can try to get this to work? Thanks for the
suggestions though.
Hard to make suggestions without seeing a complete example of the current
state of affairs, but let me try - after pointing you to
<http://www.physci.org/codes/sscce.html>
for next time.
package example;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.Scanner;
public class Car
{
private String type;
private String color;
private String description;
private String make;
public void setType( String val ) { type = val; }
public String get() { return type; }
public void setColor( String val ) { color = val; }
public String get() { return color; }
public void setDescription( String val ) { description = val; }
public String get() { return description; }
public void setMake( String val ) { make = val; }
public String get() { return make; }
public static void main( String [] args )
{
if ( args.length < 2 )
{
System.err.println( "Wrong args" );
return;
}
Scanner carIn;
try
{
carIn = new Scanner( new BufferedReader( new FileReader( args [0] )));
}
catch ( IOException ex )
{
System.err .println( "Bad File "+ args [0] +". "+ ex.getMessage() );
ex.printStackTrace( System.err );
return;
}
Car[] cars;
{
int nc;
try
{
nc = Integer.parseInt( args [1] );
if ( nc < 0 )
{
nc = 50;
}
}
catch ( NumberFormatException ex )
{
nc = 50;
}
cars = new Car [nc];
}
try
{
for ( int ix = 0; ix < cars.length && carIn.hasNext(); ++ix )
{
Car car = new Car();
String val = carIn.next();
car.setType( val );
val = carIn.next();
car.setColor( val );
val = carIn.next();
car.setDescription( val );
val = carIn.nextLine();
car.setMake( val );
cars [ix] = car;
}
// do something here with cars
}
finally
{
carIn.close();
}
}
}
--
Lew