Bakeaynado
Bakeaynado

Reputation: 25

Type missmatch cannot convert from String to int

The goal of the application is as following: I want to create objects (airplanes) of the class "Flugzeug" (German word for airplane). I want to create an array which refers to the different attributes of the objects.

The problem is (as far as I know) that one single array can only refer to variables of the exact same type.

How can I change my program that it works? Is it inevitable to create an array for each attribute (e.g. for each different type of variable)?

The code:

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

        Flugzeug [] airline = new Flugzeug [4]; 
        for (int i = 0; i < 4; i=i+1){  
            airline[i] = new Flugzeug ();
            airline[0].type = "A320";
            airline[0].idNumber = "1";
            airline[0].seats = "165";
            airline[0].velocity = "890";
            airline[0].range = "12600";
            airline[1].type = "Boeing 747";
            airline[1].idNumber = "2";
            airline[1].seats = "416";
            airline[1].velocity = "907";
            airline[1].range = "12700";
            airline[2].type = "Avro RJ 85";
            airline[2].idNumber = "3";
            airline[2].seats = "93";
            airline[2].velocity = "760";
            airline[2].range = "2200";
            airline[3].type = "Airbus 380";
            airline[3].idNumber = "4";
            airline[3].seats = "516";
            airline[3].velocity = "907";
            airline[3].range = "12000";
        }

        for (int i=0; i < 4; i=i+1) {
            airline[i].printInfo();
            double time = airline[i].getTime(6320);  //distance from Zurich to New York
            System.out.println("duration: " + time + " h");
            int capacity = airline[i].getCapacity(365);
            System.out.println("capacity: " + capacity + " passengers / year");
        }        
   }             
}   


public class Flugzeug {

    String type;
    int idNumber;
    int seats;
    double velocity;
    double range;
    double distance;
    int days;

    public void printInfo() {
        System.out.println("type: " + this.type);
        System.out.println("ID-number: " +this.idNumber);
        System.out.println("seats: " + this.seats);
        System.out.println("velocity: " + this.velocity);
        System.out.println("range: " + this.range);
    }

    public double getTime (double dist) {
        double result = 0;
        result = dist / velocity;
        double time = result;
        return time;    
    }

    public int getCapacity(int days) {
        int capacity = seats * days;
        return capacity;
    }

}

Upvotes: 0

Views: 121

Answers (4)

Joachim Sauer
Joachim Sauer

Reputation: 308001

The core of your problem is this:

one single array can only refer to variables of the exact same type.

That is correct (or mostly correct, all elements of an array must have a common base type, but that's not a relevant distinction right now).

But the type inside of your array is Flugzeug, not String!

So each element of the array must be a Flugzeug. That doesn't mean that the fields of that class have to all share a single type (and indeed, as you posted, they don't).

Look at this line:

airline[0].idNumber = "1";

this is almost correct, but since idNumber is an int you must assign it an int value (such as 1) instead:

airline[0].idNumber = 1;

The second (mostly unrelated) problem is that you try to access all 4 Flugzeug instances inside of the loop that creates them. That means when you try to access the second instance after just having created the first one (only!) it will crash:

Replace this:

for (int i = 0; i < 4; i=i+1) {  
    airline[i] = new Flugzeug ();
    airline[0].type = "A320";
    airline[1].type = "Boeing 747";
    airline[2].type = "Avro RJ 85";
    airline[3].type = "Airbus 380";
}

with this:

for (int i = 0; i < 4; i=i+1) {  
    airline[i] = new Flugzeug ();
}
airline[0].type = "A320";
airline[1].type = "Boeing 747";
airline[2].type = "Avro RJ 85";
airline[3].type = "Airbus 380";

Upvotes: 2

yoggSong
yoggSong

Reputation: 11

if some type like int,double,long... was used " " ,They almost all become String type

Upvotes: 1

Guy
Guy

Reputation: 50809

The problem is the variables are not only String type but ints and doubles as well. You need to assign the correct type. In addition you shouldn't access class variables like that, make them private create a constructor with getters and setters.

public class Flugzeug {
    private String type;
    private int idNumber;
    private int seats;
    private double velocity;
    private double range;
    private double distance;
    private int days;

    public Flugzeug(String type, int idNumber, int seats, double velocity, double range) {
        this.type = type;
        this.idNumber = idNumber;
        this.seats = seats;
        this.velocity = velocity;
        this.range = range;
    }

    public double getDistance() {
        return this.distance;
    }

    public void setDistance(double distance) {
        this.distance = distance;
    }
}


public class Fluggesellschaft {
    public static void main(String[] args) {
        Flugzeug[] airline = new Flugzeug [4];
        airline[0] = new Flugzeug("A320", 1, 165, 890, 12600);
        airline[1] = new Flugzeug(...);
    }
}

Upvotes: 0

Soumyajit
Soumyajit

Reputation: 349

I found two problems with your code. First, you have declared idNumber as int int idNumber; but while assigning the value you are inserting a string value airline[0].idNumber = "1";. NOTE: "1" is a string not integer. The solution here would be airline[0].idNumber = 1; You need to assign same type of values to every variable as they are declared.

And second, you are creating multiple objects in the loop airline[i] = new Flugzeug (); but overwriting the same single object (stored in the 0th position of the array) everytime. I would suggest to do,

airline[i].type = "A320";
airline[i].idNumber = 1;          // Again this should not be "1"
airline[i].seats = 165;           // And this should not be "165"
airline[i].velocity = 890;        // Same is applicable here
airline[i].range = 12600;         // and here

Upvotes: 0

Related Questions