Hi,

I am new to the java world.

I am getting the following error.

ClassB.java:62: incompatible types
found   : int
required: order.ClassB
        return Integer.parseInt(conString);

This what I am trying to do..

I have two classes. Class A and Class B. Class A calls a method located in Class B (refer to code below).

Class A

        Product prod = null;
       ComputerPart compart = new ComputerPart(); 
       prod = compart.getComputerPart();

Class B

I have three local variables (xdesc,xmfg,xprice) and I have three instance variables defined (instDesc,instMfg,instPrice). instDesc and instMfg are String variables and instPrice is a float variable. the local variables are assigned to the instance variables.

Then I converted the float variable instPrice to String and concentated call three variable into a Sting variable called conString. then try to return conString back the variable called prod in the calling class (Class A).

   public Class B getComputerPart() {
     .
     .
     .

    instDesc = xdesc;
    instMfg  = xmfg;
    instPrice = xprice;

    String floatToString=Float.toString(instPrice);
    String conString = instDesc+instMfg+floatToString;
    return Integer.parseInt(conString);
} 

any assistance on what I can do to convert the string variable conString to a type that is accepted by the prod variable in Class A. in addition, I believe it may have something to do with the following line of code in ClassA "Product prod = null". prod will not accept conString.

Recommended Answers

All 11 Replies

Hi,

I am new to the java world.

I am getting the following error.

ClassB.java:62: incompatible types
found   : int
required: order.ClassB
        return Integer.parseInt(conString);

This what I am trying to do..

I have two classes. Class A and Class B. Class A calls a method located in Class B (refer to code below).

Class A

        Product prod = null;
       ComputerPart compart = new ComputerPart(); 
       prod = compart.getComputerPart();

Class B

I have three local variables (xdesc,xmfg,xprice) and I have three instance variables defined (instDesc,instMfg,instPrice). instDesc and instMfg are String variables and instPrice is a float variable. the local variables are assigned to the instance variables.

Then I converted the float variable instPrice to String and concentated call three variable into a Sting variable called conString. then try to return conString back the variable called prod in the calling class (Class A).

   public Class B getComputerPart() {
     .
     .
     .

    instDesc = xdesc;
    instMfg  = xmfg;
    instPrice = xprice;

    String floatToString=Float.toString(instPrice);
    String conString = instDesc+instMfg+floatToString;
    return Integer.parseInt(conString);
} 

any assistance on what I can do to convert the string variable conString to a type that is accepted by the prod variable in Class A. in addition, I believe it may have something to do with the following line of code in ClassA "Product prod = null". prod will not accept conString.

You declare the method as returning a value of type ClassB
public Class B getComputerPart() {
( I assume the blank between Class and B is a typo)
The value you actually return is an Integer
return Integer.parseInt(conString);
So either return an instance of ClassB, or change the method declaration to have a return type of Integer

I tried changing the getmethod to return an Integer instead of ClassB and revised the returm line (See bold code below).

  public [B]Integer [/B] getComputerPart() {
    String floatToString=Float.toString(instPrice);
    String conString = instDesc+instMfg+floatToString;
    [B]return Integer.parseInt(conString);[/B]
   }

the calling class (Class A) has prod defined as Product prod = null;
where Product is a class with subclasses.

        Product prod = null;
        ComputerPart compart = new ComputerPart();
                   prod = compart.getComputerPart();
                   System.out.println("[ordergenerator back ] " + prod);

The following is the new error I am receiving. I am thinking that the type returning from the getComputerPart() is not being accepted by the prod variable. it may help if I understood what
type is "Product prod = null".

ERROR 
OrderGenerator.java:33: incompatible types
found   : java.lang.Integer
required: net.lois.order.Product
                   prod = compart.getComputerPart();
                                                 ^

If ComputerPart is the name of a class and you want to return an instance of that class, then getComputerPart shouldn't return an Integer. It should return something of type ComputerPart. Is there a class called ClassB somewhere?

Regardless, parseInt returns an int.

What are you TRYING to do? You'll probably need a little more of an explanation. We can explain the errors, but we can't suggest much of a solution without more of an explanation and a larger code snippet. Please use code tags

Regardless, parseInt returns an int.

Oops, did I say Integer? Sorry. Auto boxing/unboxing seems to lead to lazy thinking!

Hi ..
I will try to explain what I am trying to do.... but before I start... I will say I was able to successfully return the concatenated String variable back to the calling class. I verified this by entering a print statement after the call to the get method in ComputerPart.

What I am trying to do is generate test data. I need to create an order (this works) and create a product that is assoicated with the order. You can have multi. orders with multi products associated with the individual order #.

here is a sample of my code...
the calling class can be referred to as OrderGenerator
the getmethod is located in ComputerPart class
also included is code for Product.java & GenericOrder.java (these are referenced in the OrderGenerator class. )

1 of 4

public class OrderGenerator {

private Random rand = new Random();

public GenericOrder generateOrder() {                                                            //create an order

      System.out.println("[ORDER GENERATOR] starting ...");
      GenericOrder genericorder = null; 

      int orderType = rand.nextInt(1);
      switch (orderType) {  
        case 0:                                                                                                   genericorder = new ComputerOrder();
            break;
     }

      int numberOfProducts = rand.nextInt(6);
      numberOfProducts++; // We always want at least one egg.
      for (int counter=0; counter<numberOfProducts; counter++) {
        // Determine the type of egg to add.
        String product = null;
        ComputerPart compart = new ComputerPart();
        int productType = rand.nextInt(1);
        switch (productType) {
            case 0:
                product = compart.getComputerPart();
                System.out.println("[back in calling class - Class A] " + product);
        break;
         }
        // Add the product to order
           genericorder.addProduct(product);                                         
    }
   return genericorder;
}

}

2of 4
NEXT CLASS CALLED ComputerPart.java

class ComputerPart extends Product {
private static Random rand = new Random(47); 

  String instDesc;
  String instMfg;
  float instPrice;

public ComputerPart() {}
public ComputerPart(float p) {
price = p;
 }

public float price() { return price; }


public String getComputerPart() {
    String[] descTypes = {"Motherboard"};
    int d = rand.nextInt(descTypes.length);                 
    String xdesc = descTypes[d];


    float [] priceTypes = {88.88f, 222.22f};
    int p = rand.nextInt(priceTypes.length);                 
    float xprice = priceTypes[p];  


    String[] mfgTypes = {"Asus", "BSus", "Csus"};
    int m = rand.nextInt(mfgTypes.length);  
    String xmfg = mfgTypes[m];


    instDesc = xdesc;
    instMfg  = xmfg;
    instPrice = xprice;     


    String floatToString=Float.toString(instPrice);
    String conString = instDesc+instMfg+floatToString;
    return conString;

   }
}

class Motherboard extends ComputerPart {
    protected String manufacturer;
    public Motherboard(String mfg, float p) {
    super(p);
    manufacturer = mfg;
    }
    public String getManufacturer() { return manufacturer; }
}



class RAM extends ComputerPart {
    protected int size;
    protected String manufacturer;
    public RAM(String mfg, int size, float p) {
    super(p);
    this.manufacturer = mfg;
    this.size = size;
    }
    public String getManufacturer() { return manufacturer; }
}


class Drive extends ComputerPart {
    protected String type;
    protected int speed;
    public Drive(String type, int speed, float p) {
    super(p);
    this.type = type;
    this.speed = speed;
    }
    public String getType() { return type; }
    public int getSpeed() { return speed; }
}

3of 4 GENERICORDER CLASS

public abstract class GenericOrder  {
private Collection products = new ArrayList<Product>();
private static int orderCount=2000;
private int orderNumber;



public GenericOrder() {
  orderNumber = orderCount++;  
  } 


public void addProduct(Product product) {                                            
  products.add(product);                           
  }


public Collection getProduct() {                                                     
  return products;
  }


public abstract String getOrderDescription();      


public int getOrderNumber() {                                                       //return order number 
 // System.out.println("[GENERIC ORDER  ] RETRIEVE orderNumber"); 
  return orderNumber;
  }
}

4 of 4

abstract class Product {
    protected float price;


 // return the price of a particular product
    abstract float price();

}

I meant to include in previous entry the following error message:

OrderGenerator.java:37: addProduct(net.allenwhite.order.Product) in net.allenwhi
te.order.GenericOrder cannot be applied to (java.lang.String)
genericorder.addProduct(product);
^

thank you any assistance would be appreciated. I know something is not compatible....
^

Code tags:

[code]

// paste code here

[/code]

or

[code=JAVA] // paste code here

[/code]

Please edit your post and add them.

Sorry I forgot .. I now have (CODE) before and after each of the 4 classes.

Again any assiatance would be greatly appreciated,

Hi ..
I will try to explain what I am trying to do.... but before I start... I will say I was able to successfully return the concatenated String variable back to the calling class. I verified this by entering a print statement after the call to the get method in ComputerPart.

What I am trying to do is generate test data. I need to create an order (this works) and create a product that is associated with the order. You can have multi. orders with multi products associated with the individual order #.

Here is the error message I am getting.

OrderGenerator.java:37: addProduct(net.allenwhite.order.Product) in net.allenwhi
te.order.GenericOrder cannot be applied to (java.lang.String)
genericorder.addProduct(product);
                    ^

thank you any assistance would be appreciated. I know something is not compatible....

here is a sample of my code...
the calling class can be referred to as OrderGenerator
the getmethod is located in ComputerPart class
also included is code for Product.java & GenericOrder.java (these are referenced in the OrderGenerator class. )

1 of 4

public class OrderGenerator {

private Random rand = new Random();

public GenericOrder generateOrder() { //create an order

System.out.println("[ORDER GENERATOR] starting ...");
GenericOrder genericorder = null; 

int orderType = rand.nextInt(1);
switch (orderType) { 
case 0: genericorder = new ComputerOrder();
break;
}

int numberOfProducts = rand.nextInt(6);
numberOfProducts++; // We always want at least one egg.
for (int counter=0; counter<numberOfProducts; counter++) {
// Determine the type of egg to add.
String product = null;
ComputerPart compart = new ComputerPart();
int productType = rand.nextInt(1);
switch (productType) {
case 0:
product = compart.getComputerPart();
System.out.println("[back in calling class - Class A] " + product);
break;
}
// Add the product to order
genericorder.addProduct(product); 
}
return genericorder;
}

}

2of 4

NEXT CLASS CALLED ComputerPart.java



class ComputerPart extends Product {
private static Random rand = new Random(47); 

String instDesc;
String instMfg;
float instPrice;

public ComputerPart() {}
public ComputerPart(float p) {
price = p;
}

public float price() { return price; }


public String getComputerPart() {
String[] descTypes = {"Motherboard"};
int d = rand.nextInt(descTypes.length); 
String xdesc = descTypes[d];


float [] priceTypes = {88.88f, 222.22f};
int p = rand.nextInt(priceTypes.length); 
float xprice = priceTypes[p]; 


String[] mfgTypes = {"Asus", "BSus", "Csus"};
int m = rand.nextInt(mfgTypes.length); 
String xmfg = mfgTypes[m];


instDesc = xdesc;
instMfg = xmfg;
instPrice = xprice; 


String floatToString=Float.toString(instPrice);
String conString = instDesc+instMfg+floatToString;
return conString;

}
}

class Motherboard extends ComputerPart {
protected String manufacturer;
public Motherboard(String mfg, float p) {
super(p);
manufacturer = mfg;
}
public String getManufacturer() { return manufacturer; }
}



class RAM extends ComputerPart {
protected int size;
protected String manufacturer;
public RAM(String mfg, int size, float p) {
super(p);
this.manufacturer = mfg;
this.size = size;
}
public String getManufacturer() { return manufacturer; }
}


class Drive extends ComputerPart {
protected String type;
protected int speed;
public Drive(String type, int speed, float p) {
super(p);
this.type = type;
this.speed = speed;
}
public String getType() { return type; }
public int getSpeed() { return speed; }
}

3of 4 GENERICORDER CLASS

public abstract class GenericOrder {
private Collection products = new ArrayList<Product>();
private static int orderCount=2000;
private int orderNumber;



public GenericOrder() {
orderNumber = orderCount++; 
} 


public void addProduct(Product product) { 
products.add(product); 
}


public Collection getProduct() { 
return products;
}


public abstract String getOrderDescription(); 


public int getOrderNumber() { //return order number 
// System.out.println("[GENERIC ORDER ] RETRIEVE orderNumber"); 
return orderNumber;
}
}

4 of 4

abstract class Product {
protected float price;


// return the price of a particular product
abstract float price();

}

Close. Click "Edit this post" and make the tag at the end [/code] - note the /.

I was able to revise my code. now have it working. thank you for your assistance on this posting.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.