Results 1 to 3 of 3
Thread: Array Input
- 04-19-2009, 08:43 PM #1
Member
- Join Date
- Apr 2009
- Posts
- 19
- Rep Power
- 0
Array Input
I have a problem with inputting to array. Can u help me find why?
public boolean addRaceDriverToArray(String licenseNumber, String fullName,String nationality,Date dateOfBirth,String team, int worldChampionships,int totalScore, int highestRaceFinish) {
//TODO this method
if(driversArr.length<MAX_ARRAYS_LENGTH)
{
for(int i=current_Length_Driver_Array;i<MAX_ARRAYS_LENGTH; i++)
if(driversArr[i]==null)
{
driversArr[i].setLicenseNumber(licenseNumber);
driversArr[i].setFullName(fullName);
driversArr[i].setDateOfBirth(dateOfBirth);
driversArr[i].setNationality(nationality);
driversArr[i].SetSkillLevel();
driversArr[i].setTotalScore(totalScore);
driversArr[i].setTeam(team);
driversArr[i].setWorldChampionships(worldChampionships);
driversArr[i].setHighestRaceFinish(highestRaceFinish);
current_Length_Driver_Array++;
return true;
}
}
return false;
}
- 04-19-2009, 09:52 PM #2
What type of array are you using? I'm guessing it's an array of drivers?
Then do
All the value's in driversArr are null unless you set them.Java Code:Driver tmp = new Driver(); tmp.setLicenseNumber(licenseNumber); .... driversArr[i] = tmp;
Also next time please post what your program is supposed to do and all essential parts (in code tags). (ie where driversArr is declared)
Also take a look at How to ask a question.
- 04-19-2009, 10:39 PM #3
Member
- Join Date
- Apr 2009
- Posts
- 19
- Rep Power
- 0
My project is about Formula 1. It has a lot of classes and imports from another project.
The inputs has to be from a txt file..
public class SysF1 {
/**
* Maximum length of any array in this system
*/
public static final int MAX_ARRAYS_LENGTH =50;
/**
* Current length of race car array
*/
private int current_Length_Car_Array;
/**
* Current length of race driver array
*/
private int current_Length_Driver_Array;
/**
* Array of race cars in the system
*/
public RaceCar[] carsArr;
/**
* Array of race drivers in the system
*/
public RaceDriver[] driversArr;
/**
* Constructor for SysF1 class.
* Perform initialization for race cars array and race drivers array.
* @see carsArr
* @see driversArr
*/
public SysF1() {
carsArr=new RaceCar[MAX_ARRAYS_LENGTH];
driversArr=new RaceDriver[MAX_ARRAYS_LENGTH];
}
/**
* This method finds the best race driver - the one with maximum TotalScore and SkillLevel = D,
* also his race car color has to be BLACK. <br/> In case there are more than one driver suitable
* for this conditions, then return the first one we found.
* @param driversArr
* @return best RaceDriver object
*/
public RaceDriver findTheBestRaceDriver(){
//TODO this method
boolean doMore = true;
while (doMore) {
doMore = false; // assume this is last pass over array
for (int i=0; i<current_Length_Driver_Array; i++) {
if (driversArr[i].totalScore > driversArr[i+1].totalScore) {
// exchange elements
RaceDriver temp = driversArr[i]; driversArr[i] = driversArr[i+1]; driversArr[i+1] = temp;
doMore = true; // after an exchange, must look again
}
}
}
for(int i=current_Length_Driver_Array;i>0;i--)
{
if((driversArr[i].skillLevel=='D')&&(driversArr[i].equals("Black")==true))
return driversArr[i];
}
return null;
}
/**
* This method sort race cars Array by performanceRate attribute in Acceding order.
* @param driversArr
* @return sorted array
*/
public RaceCar[] getSortedRaceCars(){
//TODO this method
boolean doMore = true;
while (doMore) {
doMore = false; // assume this is last pass over array
for (int i=0; i<current_Length_Car_Array; i++) {
if (carsArr[i].performanceRate > carsArr[i+1].performanceRate) {
// exchange elements
RaceCar temp = carsArr[i]; carsArr[i] = carsArr[i+1]; carsArr[i+1] = temp;
doMore = true; // after an exchange, must look again
}
}
}
return carsArr;
}
/**
* This methods finds the race car which belongs to a race driver
* with given driverLicenseNumber as parameter.
* @param driverLicenseNumber
* @return RaceCar of driver
*/
public RaceCar getDriverRaceCar(String driverLicenseNumber){
//TODO this method
for(int i=0;i<current_Length_Driver_Array;i++)
{
if(driversArr[i].licenseNumber.equals(driverLicenseNumber)==true)
return (driversArr[i].Car);
}
return null;
}
/**
* This method creates and adds new Race Car to Race Cars array.
* @param licensePlate
* @param year
* @param manufacturer
* @param model
* @param colour
* @param numDoors
* @param TopSpeed
* @param totalRaces
* @return true if new car added successfully to race cars array, false if array has no room for more cars. */
public boolean addCarToArray(int licensePlate,int year,
String manufacturer, String model,Color colour,int numDoors
, double TopSpeed, int totalRaces) {
//TODO this method
if(carsArr.length<MAX_ARRAYS_LENGTH)
{
for(int i=current_Length_Car_Array;i<MAX_ARRAYS_LENGTH;i++ )
if(carsArr[i]==null)
{
carsArr[i].licensePlate=licensePlate; carsArr[current_Length_Car_Array].setColour(colour);
carsArr[i].manufacturer=manufacturer;
carsArr[i].year=year;
carsArr[i].model=model;
carsArr[i].numDoors=numDoors;
carsArr[i].setTopSpeed(TopSpeed);
carsArr[i].setTotalRaces(totalRaces);
current_Length_Car_Array++;
return true;
}
}
return false;
}
/** This method creates and adds new Race Driver to Race Drivers array.
* @param licenseNumber
* @param fullName
* @param nationality
* @param dateOfBirth
* @param team
* @param worldChampionships
* @param totalScore
* @param highestRaceFinish
* @return true if new race driver added successfully to race drivers array, false if array has no room for more drivers. */
public boolean addRaceDriverToArray(String licenseNumber, String fullName,String nationality,Date dateOfBirth,String team, int worldChampionships,int totalScore, int highestRaceFinish) {
//TODO this method
if(driversArr.length<MAX_ARRAYS_LENGTH)
{
for(int i=current_Length_Driver_Array;i<MAX_ARRAYS_LENGTH; i++)
if(driversArr[i]==null)
{
driversArr[i].setLicenseNumber(licenseNumber);
driversArr[i].setFullName(fullName);
driversArr[i].setDateOfBirth(dateOfBirth);
driversArr[i].setNationality(nationality);
driversArr[i].SetSkillLevel();
driversArr[i].setTotalScore(totalScore);
driversArr[i].setTeam(team);
driversArr[i].setWorldChampionships(worldChampionships);
driversArr[i].setHighestRaceFinish(highestRaceFinish);
current_Length_Driver_Array++;
return true;
}
}
return false;
}
/**
* This method creates a connection between race driver to race car,
* the method finds a car by carLicensePlate in an array and finds race driver in drivers
* array by driverLicenseNumber. If both objects are found then we can relate this specific car to specific
* driver. The adding car to driver performed by sending car object to driver and driver object to car.
* If driver already has other car connecting a new car will fail, also if the car already connected to other driver
* new connection will fail.
* @param driverLicenseNumber
* @param carLicensePlate
* @return true if both objects found and connected, false otherwise.
*/
public boolean addRaceCarToDriver(String driverLicenseNumber,int carLicensePlate) {
if(!driverLicenseNumber.equals("") || carLicensePlate>0){
RaceCar tempCar = null;
RaceDriver tempDriver=null;
for(RaceCar car:carsArr){
if(car!=null && car.getLicensePlate()==carLicensePlate){
tempCar=car;
for(RaceDriver driver:driversArr){
if(driver!=null && driver.getLicenseNmber().equals(driverLicenseNumbe r)){
tempDriver=driver;
}
}
}
}
if(tempCar!=null && tempDriver!= null){
if(tempCar.addRaceDriver(tempDriver)){
if(tempDriver.addRaceCar(tempCar)){
return true;
}
}
}
}
return false;
}
public class MainClass {
/**
* The main method of this system, gets input from text file.
* Writes output to output.txt file
* @param args
* @throws IOException
* @throws ParseException
*/
public static void main(String[] args) throws IOException, ParseException {
String command;
File f=new File("output.txt"); // declaration of output file
FileWriter writer=null; // writer buffer to file declaration
writer = new FileWriter(f); // writer buffer creation
Scanner input=null;
DateFormat df = DateFormat.getDateInstance(DateFormat.DATE_FIELD);
SysF1 sys=new SysF1(); // creation of sysF1 object
input = new Scanner(new File("input.txt")); // creation of connection to input file
//read the commands
// loop which performed until end of input file [false in [input.hasnext()]
while(input.hasNext()){
command = input.next();//read the command, must be first at line.
/*
* command value determine which method will be activated in sys object.
*/
if(command.equals("addRaceDriverToArray")){
/*
* Since we established the input method, we read from input file
* more data which the method requires.
*/
sys.addRaceDriverToArray(input.next(),input.next() +" "+input.next(),
input.next(),df.parse(input.next()),
input.next(),input.nextInt(),input.nextInt(),input .nextInt());
// This method creates and adds new Race Driver to Race Drivers array
}
else if (command.equals("addRaceCarToArray")){
sys.addCarToArray(input.nextInt(),
input.nextInt(), input.next(), input.next(),
new Color(input.nextInt(),input.nextInt()
,input.nextInt()), input.nextInt(),
input.nextDouble(), input.nextInt());
// This method creates and adds new Race Car to Race Cars array
}
else if (command.equals("addRaceCarToDriver")){
// This method attach Race Driver to Race Car
String driverLicenseNumber=input.next();
int carLicensePlate=input.nextInt();
boolean isUpdated=sys.addRaceCarToDriver(driverLicenseNumb er,carLicensePlate);
if(isUpdated){ /* if attached successfully, than true returned, and following message written
output file*/
writer.write("addRaceCarToDriver returns: "+
"Successfully adding car "+carLicensePlate+
" to driver "+driverLicenseNumber+"\n");
}
else{
writer.write("addRaceCarToDriver returns: "+
"Failed to add car "+carLicensePlate+
" to driver "+driverLicenseNumber+"\n");
}
}
else if(command.equals("getDriverRaceCar")){ /* This method finds the car of a Race Driver
by searching on his driverLicenseNumber*/
String driverLicenseNumber=input.next();
RaceCar tempCar=sys.getDriverRaceCar(driverLicenseNumber);
if(tempCar==null)
writer.write("\ngetDriverRaceCar returns:\n"+
"driver "+driverLicenseNumber+" don't have a race car yet!\n");
else
writer.write("\ngetDriverRaceCar returns:\n"+
"driver "+driverLicenseNumber+" have a race car:\n"+
tempCar);
}
else if(command.equals("findTheBestRaceDriver")){
/*
* This method finds best race driver by his points
*/
writer.write("\nfindTheBestRaceDriver returns:\n"+sys.findTheBestRaceDriver()+"\n");
}
else if(command.equals("getSortedRaceCars")){
/*
* This method returns sorted race car array.
*/
sys.getSortedRaceCars();
}
else System.out.printf("Command %s does not exist\n",command);
} // end of clause - while input has next
writer.close(); // close writer buffer to output file
input.close(); // close connection to input file
System.out.println("[End process]");
}
}
it runs perfectly without any error..but in the output text file it writes:
addRaceCarToDriver returns: Failed to add car 1123408 to driver A22XY234
addRaceCarToDriver returns: Failed to add car 2265607 to driver B33ZB435
addRaceCarToDriver returns: Failed to add car 3368706 to driver C44WE765
addRaceCarToDriver returns: Failed to add car 4467207 to driver D55FG556
addRaceCarToDriver returns: Failed to add car 5543307 to driver E66VB958
addRaceCarToDriver returns: Failed to add car 6643309 to driver F77HJ567
addRaceCarToDriver returns: Failed to add car 7711108 to driver G88LK545
addRaceCarToDriver returns: Failed to add car 8822205 to driver H99KU865
addRaceCarToDriver returns: Failed to add car 9933306 to driver I10UT546
addRaceCarToDriver returns: Failed to add car 1044407 to driver J20DF763
addRaceCarToDriver returns: Failed to add car 2055508 to driver K30SD544
addRaceCarToDriver returns: Failed to add car 2055508 to driver P30SD544
addRaceCarToDriver returns: Failed to add car 8933306 to driver I10UT546
getDriverRaceCar returns:
driver A22XY234 don't have a race car yet!
getDriverRaceCar returns:
driver K30SD544 don't have a race car yet!
getDriverRaceCar returns:
driver L40AS457 don't have a race car yet!
getDriverRaceCar returns:
driver K40AS457 don't have a race car yet!
findTheBestRaceDriver returns:
null
now did I make myself clear?!
Similar Threads
-
Problem - input in two-dimensional array
By PVL268 in forum New To JavaReplies: 5Last Post: 03-09-2009, 04:58 AM -
Perfect Square Array Input Using For Loop
By dalangley in forum New To JavaReplies: 9Last Post: 01-27-2009, 01:33 AM -
Reading input file into an array
By littlefire in forum New To JavaReplies: 6Last Post: 10-18-2008, 11:51 PM -
input placed in array
By smilejava in forum New To JavaReplies: 5Last Post: 11-12-2007, 07:29 AM -
input placed in array
By smilejava in forum New To JavaReplies: 1Last Post: 11-05-2007, 12:32 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks