Jump to content

Need Help on arrays and linking to arrays

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
15 replies to this topic

#1
Trendnet18

Trendnet18

    Newbie

  • Members
  • Pip
  • 8 posts
Hi I got a problem linking the array together not sure if I have done it correctly...


mainmethod.java....

import java.util.Scanner;

public class mainmethod {
public static void main(String args[]) {

IssueCarUI myIssue = new IssueCarUI();
Scanner input = new Scanner(System.in);

String name, ic,requestModel;

System.out.print("Please enter the model to rent");
requestModel = input.nextLine();
myIssue.showCarDetails(requestModel);








// System.out.print("Please Enter your name: ");
// name = input.nextLine();

// System.out.print("Please Enter your IC: ");
// ic = input.nextLine();

// myIssue.createCustomer(name, ic);

}
}

Database.java.........

public class Database {


public void showDetails(){
Car carList[] = new Car[9];

carList[0] = new Car();
carList[0].setCar("Toyota","Altis", "SJC2456 X", 100, 60);

carList[1].setCar("Toyota","Vios", "SJG 9523 B", 100, 50);

carList[2].setCar("Nissan","Latio", "SJB 7412 B", 100, 50);

carList[3].setCar("Nissan","Murano", "SJC 8761 B", 300, 150);

carList[4].setCar("Honda","Jazz", "SJB 4875 N", 100, 60);

carList[5].setCar("Honda","Civic", "SJD 73269 C", 120, 70);

carList[6].setCar("Honda","Stream", "SJL 5169 J", 120, 70);

carList[7].setCar("Honda","Odyssey", "SJB 3468 E", 200, 150);

carList[8].setCar("Subaru","WRX", "SJB 8234 L", 300, 200);

carList[9].setCar("Subaru","Impressa", "SJE 8234 K", 150, 80);



}

public Car findCarByModel(String findModel){

for(int i = 0; i <=9; i++){

if(carList[i].getModel().equalsIgnoreCase(findModel)== true)
{
return carList[i]; }
} return null;
}
}

Car.java..........
public class Car {
private String regno;
private String make;
private String model;
private int deposit;
private int rate;


public void setCar(String m, String mo, String reg, int dep, int r) {
make = m;
model = mo;
regno = reg;
deposit = dep;
rate = r;

}

public String getRegNo() {
return regno;
}

public String getmake() {
return make;
}

public String getModel() {
return model;
}

public int getdeposit() {
return deposit;
}

public int getrate() {
return rate;
}




}

Car.java.........

public class IssueCarUI {
public static void main(String args[]){}
private String requestModel;
private Customer myCustomer;
// Payment myPayment = new Payment();
Rental myRental = new Rental();
Database myDatabase = new Database();
private int noOfDays;

public IssueCarUI() {
myCustomer = new Customer("", "");

}//end Constructor

public void createCustomer(String n, String i) {

myCustomer.setName(n);
myCustomer.setIC(i);
System.out.println("Name: " + myCustomer.getName());
System.out.println("Name: " + myCustomer.getIC());

}// end Create customer method

public void calculateCost(int d){

myRental.setdays(d);
System.out.println("Duration: 5 days");


}// End Calculate cost


public void showCarDetails(String requestModel) {
requestModel=null;
if(myDatabase.findCarByModel().equalsIgnoreCase(requestModel)==true){

System.out.println("Model " +requestModel + " is available");

}else{ System.out.println("Model " +requestModel + " not is available");}



}






}// End Class

#2
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
By having a quick look i find this suspicious
public void showCarDetails(String requestModel) {
requestModel=null;
...
} 
What is it supposed to do? the requestModel = null;?

#3
Trendnet18

Trendnet18

    Newbie

  • Members
  • Pip
  • 8 posts
I assume u mean this method...


public void showCarDetails(String requestModel) {
requestModel=null;
if(myDatabase.findCarByModel().equalsIgnoreCase(requestModel)==true){

System.out.println("Model " +requestModel + " is available");

}else{ System.out.println("Model " +requestModel + " not is available");}



}


this is to compare what the user types in with the one inside the array...

#4
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
>>WALL OF TEXT, QUOTES, AND CODE :rules:<<

I'll give a list of things i think are wrong with the following method:

public void showCarDetails(String requestModel)

-------------------------------------------------------------------------------------------------------------

Quote

requestModel=null;
i think this should be:

this.requestModel=null;

Or it can be removed as requestModel isn't used anywhere else in the class (remove class declaration aswell then)
-------------------------------------------------------------------------------------------------------------

Quote

if(myDatabase.findCarByModel().equalsIgnoreCase(re questModel)==true){
-The findCarByModel() function doesn't return a boolean (==true), it returns a car.
-You don't give the parameter with it.
-equalsIgnoreCase(..) is used within the findCarByModel(), no need to do it twice
So i think it should be:

Car tempCar = myDatabase.findCarByModel(requestModel)

if(tempCar != null){

-------------------------------------------------------------------------------------------------------------

Quote

System.out.println("Model " +requestModel + " is available");

}else{ System.out.println("Model " +requestModel + " not is available");}
Nothing wrong there ;)
-------------------------------------------------------------------------------------------------------------
The full changed method in one code block:

public void showCarDetails(String requestModel) {

    this.requestModel=null;

    Car tempCar = myDatabase.findCarByModel(requestModel);

    if(tempCar != null){

        System.out.println("Model " +requestModel + " is available");


    }else{ System.out.println("Model " +requestModel + " not is available");}


-------------------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------------------------
Database class:

Quote

Car carList[] = new Car[9];
should be:

Car[] carList = new Car[9];

In addition, this line should be outside showDetails() AND in the showDetails you have 10 cars, 0 to 9, thus you must declare the array as a size of 10 instead of 9.
thus:

public class Database {

    Car[] carList = new Car[10];


    public void showDetails() {

...

...

...

    } 

}


Change this line to the following, it should happen when you create the database (constructor):

Quote

public void showDetails() {

public Database(){


Whole database class:
public class Database {

    Car[] carList = new Car[10];


    public Database() {




        carList[0]= new Car("Toyota", "Altis", "SJC2456 X", 100, 60);


        carList[1]= new Car("Toyota", "Vios", "SJG 9523 B", 100, 50);


        carList[2]= new Car("Nissan", "Latio", "SJB 7412 B", 100, 50);


        carList[3]= new Car("Nissan", "Murano", "SJC 8761 B", 300, 150);


        carList[4]= new Car("Honda", "Jazz", "SJB 4875 N", 100, 60);


        carList[5]= new Car("Honda", "Civic", "SJD 73269 C", 120, 70);


        carList[6]= new Car("Honda", "Stream", "SJL 5169 J", 120, 70);


        carList[7]= new Car("Honda", "Odyssey", "SJB 3468 E", 200, 150);


        carList[8]= new Car("Subaru", "WRX", "SJB 8234 L", 300, 200);


        carList[9]= new Car("Subaru", "Impressa", "SJE 8234 K", 150, 80);



    }


    public Car findCarByModel(String findModel) {


        for (int i = 0; i <= 9; i++) {


            if (carList[i].getModel().equalsIgnoreCase(findModel) == true) {

                return carList[i];

            }

        }

        return null;

    }

}

Noticed how i do "carList[0]= new Car("Toyota", "Altis", "SJC2456 X", 100, 60);"
Instead of .setCar.. the Cars do not exist yet. Thus you must create one first using new Car(...)

This will call the constructor of the class Car. The constructor exists but is named wrong.
Class Car:
Change public void setCar to public car:

public Car(String m, String mo, String reg, int dep, int r) {

        make = m;

        model = mo;

        regno = reg;

        deposit = dep;

        rate = r;


    }


And now try again :D

Edited by wim DC, 18 January 2010 - 05:28 AM.


#5
Trendnet18

Trendnet18

    Newbie

  • Members
  • Pip
  • 8 posts
hi I changed the part for the array..

im getting array on => carList[] = new Car[10];
gives me illegal statement

thanks for your help earlier

public class Database {

private Car [] carList;
Car car = null;

public Database(){


carList[] = new Car[10];
carList[0] = new Car("Toyota","Altis", "SJC2456 X", 100, 60);

carList[1]= new Car("Toyota","Vios", "SJG 9523 B", 100, 50);

carList[2]= new Car("Nissan","Latio", "SJB 7412 B", 100, 50);

carList[3]= new Car("Nissan","Murano", "SJC 8761 B", 300, 150);

carList[4]= new Car("Honda","Jazz", "SJB 4875 N", 100, 60);

carList[5]= new Car("Honda","Civic", "SJD 73269 C", 120, 70);

carList[6]= new Car("Honda","Stream", "SJL 5169 J", 120, 70);

carList[7]= new Car("Honda","Odyssey", "SJB 3468 E", 200, 150);

carList[8]= new Car("Subaru","WRX", "SJB 8234 L", 300, 200);

carList[9]= new Car("Subaru","Impressa", "SJE 8234 K", 150, 80);





}


public void showDetails(){



}

public Car findCarByModel(String findModel){

for(int i = 0; i < carList.length; i++){

if(carList[i].getModel().equalsIgnoreCase(findModel))
{
mycar = carList[i];
break;

}
} return myCar;
}

public int calculateCost(int noOfDays){

}

}//END CLASS

#6
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
It's not what i proposed in my post the rule where i mentioned it like that was a quote from your code ;)
So either use
Car[] carList = new Car[10];
Or if you like to use the 'new' statement within the constructor do it like:

public class Database {

private Car [] carList; 
Car[] car;        //Change here <<---

public Database(){


carList = new Car[10];      //Change here <<---
carList[0] = new Car("Toyota","Altis", "SJC2456 X", 100, 60); 

carList[1]= new Car("Toyota","Vios", "SJG 9523 B", 100, 50); 

carList[2]= new Car("Nissan","Latio", "SJB 7412 B", 100, 50); 

carList[3]= new Car("Nissan","Murano", "SJC 8761 B", 300, 150); 

carList[4]= new Car("Honda","Jazz", "SJB 4875 N", 100, 60); 

carList[5]= new Car("Honda","Civic", "SJD 73269 C", 120, 70); 

carList[6]= new Car("Honda","Stream", "SJL 5169 J", 120, 70); 

carList[7]= new Car("Honda","Odyssey", "SJB 3468 E", 200, 150); 

carList[8]= new Car("Subaru","WRX", "SJB 8234 L", 300, 200); 

carList[9]= new Car("Subaru","Impressa", "SJE 8234 K", 150, 80); 





}


#7
Trendnet18

Trendnet18

    Newbie

  • Members
  • Pip
  • 8 posts
public Car findCarByModel(String findModel){

for(int i = 0; i < carList.length; i++){

if(carList[i].getModel().equalsIgnoreCase(findModel))
{
Car = carList[i];
break;

}
} return Car;
}
ok im having problem with this part doesnt detect Car..

#8
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
Put return Car inside the if { }
public Car findCarByModel(String findModel){ 

    for(int i = 0; i < carList.length; i++){ 

        if(carList[i].getModel().equalsIgnoreCase(findModel))
        { 
            return carList[i]
        } 
    } 
}


#9
Trendnet18

Trendnet18

    Newbie

  • Members
  • Pip
  • 8 posts
==> like this??
public Car findCarByModel(String findCarModel) {

for (int i = 0; i < carList.length; i++) {

if (carList[i].getModel().equalsIgnoreCase(findCarModel)) {
findCarModel = carList[i];
break;
return findCarModel;

}


}
}

#10
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
Just use the code from my post above.
The "break;" isn't needed as when you return the method stops there anyway
findCarModel = carList[i]; Has no use as findCarModel isn't defined anywhere outside the method.

so just:
public Car findCarByModel(String findModel){

for(int i = 0; i < carList.length; i++){

if(carList[i].getModel().equalsIgnoreCase(findModel)){
return carList[i]
}

}
}

#11
Trendnet18

Trendnet18

    Newbie

  • Members
  • Pip
  • 8 posts
im getting a missing return statement at the end of that

#12
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
ah ye (DOH:thumbdown:)
put
return null;
after the for loop.