Results 1 to 3 of 3

Thread: Array Input

  1. #1
    Rose88 is offline Member
    Join Date
    Apr 2009
    Posts
    19
    Rep Power
    0

    Default 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;
    }

  2. #2
    Mr.Beans's Avatar
    Mr.Beans is offline Senior Member
    Join Date
    Apr 2009
    Location
    Virginia Beach, Virginia
    Posts
    149
    Rep Power
    0

    Default

    What type of array are you using? I'm guessing it's an array of drivers?

    Then do
    Java Code:
    Driver tmp = new Driver();
    tmp.setLicenseNumber(licenseNumber);
    ....
    driversArr[i] = tmp;
    All the value's in driversArr are null unless you set them.

    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.

  3. #3
    Rose88 is offline Member
    Join Date
    Apr 2009
    Posts
    19
    Rep Power
    0

    Default

    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

  1. Problem - input in two-dimensional array
    By PVL268 in forum New To Java
    Replies: 5
    Last Post: 03-09-2009, 05:58 AM
  2. Perfect Square Array Input Using For Loop
    By dalangley in forum New To Java
    Replies: 9
    Last Post: 01-27-2009, 02:33 AM
  3. Reading input file into an array
    By littlefire in forum New To Java
    Replies: 6
    Last Post: 10-19-2008, 12:51 AM
  4. input placed in array
    By smilejava in forum New To Java
    Replies: 5
    Last Post: 11-12-2007, 08:29 AM
  5. input placed in array
    By smilejava in forum New To Java
    Replies: 1
    Last Post: 11-05-2007, 01:32 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •