Results 1 to 2 of 2
  1. #1
    corney_16 is offline Member
    Join Date
    Mar 2010
    Posts
    2
    Rep Power
    0

    Default > Operator cannot be applied error and return incompatible types error

    Hi i've only been doing java for a few weeks so yes some of the following may be completely wrong i am aware of that.

    im working on a stock control system that is run from the command promt and you can add items, view items or exit.

    I have three java files: Stock, Inventory and KeyInput.
    the key input file is for reading information from the keyboard (my tutor made that file).

    inventory initiates the program and it then works from the stock file for the most part of the program.

    i am recieveing several errors to do with incaompatible return types and
    > operator cannot be applied error.


    If anybody could point me in the right direction of how to fix this it would be greatly appreciated.

    i think my main problem may be that im not converting strings ints and doubles correctly

    thanks heaps
    Courtenay

    See below for my errors:


    \Stock.java:213: operator > cannot be applied to int[],int
    if (quantity > 0)
    ^
    \Stock.java:222: operator > cannot be applied to double[],double
    if (cost >0.0)
    ^

    \Stock.java:251: incompatible types
    found : java.lang.String[]
    required: java.lang.String
    return name;
    ^
    \Stock.java:258: incompatible types
    found : int[]
    required: int
    return quantity;
    ^
    \Stock.java:264: incompatible types
    found : double[]
    required: double
    return cost;
    ^
    5 errors

    Tool completed with exit code 1


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


    and here is my code:

    --------------------------------------------------------------
    //file 1 = Inventory.java


    class Inventory
    {
    public static void main(String commandArgs[ ])
    {
    Stock SK = new Stock(commandArgs);
    //Stock SK = new Stock();
    SK.displayMenu();
    //SK.setMaxStock();
    }
    }

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

    //file 2: Stock.java

    //Description: Stock Entry System


    class Stock



    {

    int maxStock;
    int current = 0;
    String name [];
    int quantity [];
    double cost [];
    double totalStockValue = 0.0;



    public void setMaxStock(int maxStock)
    {
    if (maxStock > 0)
    this.maxStock = maxStock;
    else
    this.maxStock = 0;
    }


    Stock(String itemName, int itemQuantity, double itemCost)
    {
    this.name = name;
    this.quantity = quantity;
    this.cost = cost;
    }


    Stock (int max)
    {
    maxStock = max;
    name = new String [max];
    quantity = new int [max];
    cost = new double [max];
    }


    public int getMaxStock() //do i need this?
    {
    return maxStock;
    }

    Stock(String commandArgs[ ]) //constructor
    {
    if (commandArgs.length != 0)

    {
    maxStock = validateArg(commandArgs[0]);
    }
    else
    System.out.println("Invalid or missing maximum stock level supplied");
    System.out.println("Usage: java Inventory <n> where <n> is the maximum number of stock items");
    System.exit(1);
    System.exit(6);
    }

    public int validateArg(String theArg)
    {
    for (int i = 0; i < theArg.length(); i++)
    {
    if ( !Character.isDigit(theArg.charAt(i)))
    {
    System.out.println("You need to supply a numeric value");
    return -1;
    }
    }
    Integer tempInt = new Integer (theArg);
    return tempInt.intValue();
    }

    public void displayMenu()
    {
    if (maxStock != -1)
    {
    System.out.println("\n\nStock Menu");
    System.out.println("============================== ");
    System.out.println("<1> Add an Item");
    System.out.println("<2> View Inventory");
    System.out.println("<0> Exit");
    System.out.println("\nSelect an option <1>, <2> or <0>: "); //allow for new item object on same line
    int x = KeyInput.readInt();
    //testMenu(x);
    }
    }

    public void testMenu(int x)
    {

    if (x == 1)
    {
    setItem();
    }

    else if (x == 2)
    {
    toString(); //viewStock();
    }

    else if (x == 0)
    {
    System.exit(0);
    }

    else
    {
    System.out.println("Invalid menu option, must be 1, 2 or 0");
    displayMenu();
    }

    }


    public void setItem()
    {

    System.out.print("Enter the name of the item: ");
    name[current] = KeyInput.readString();



    System.out.println("\nNow enter the quantity of "+name+"'s: "); //name or current???
    quantity[current] = KeyInput.readInt();



    if (quantity > 0)
    {

    System.out.println("\nNow enter the cost price of "+name+"'s: ");
    cost[current] = KeyInput.readDouble();



    if (cost >0.0)
    {
    int current = current++;
    System.out.println("Item " + current + "has been stored successfully ");
    while(current != maxStock)
    {
    displayMenu();
    }
    }
    else
    {
    System.out.println("Not a valid input, should be a real number eg. 10.65");
    System.exit(1);
    System.exit(6);
    }
    }
    else
    {
    System.out.println("Not a valid input, should be a whole number");
    System.exit(1);
    System.exit(6);
    }

    }

    public String getName()
    {

    return name;
    }

    public int getQuantity()
    {

    return quantity;
    }

    public double getCost()
    {
    return cost;
    }

    public double getGst()
    {
    return getCost()*1.125;
    }

    public double getTotalSell()
    {
    return getGst()*getQuantity();
    }


    //displays the stock on hand showing name, quantity, cost, sell price with GST, and Total selling value
    public String toString()//viewStock()
    {
    System.out.println("Name\t\t Quantity\t Cost Price\t Sell Price inc GST\t Total Selling Value");
    System.out.println("============================== ================================================== ============");
    System.out.println(getName()+ "\t" +getQuantity()+ "\t" +getCost() + "\t" +getGst()+ "\t" +getTotalSell());


    }






    }




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


    //file 3: KeyInput.java


    import java.io.*;

    abstract class KeyInput
    {
    private static String fetch()
    {
    BufferedReader In = new BufferedReader(new InputStreamReader(System.in));

    String temp;
    try
    {
    temp = In.readLine();
    }
    catch (IOException e)
    {
    temp = "";
    }
    return temp;
    }

    public static String readString()
    {
    return fetch();
    }

    public static int readInt()
    {
    String line = fetch();
    int iVal = 0;
    try
    {
    iVal = Integer.parseInt(line);
    }
    catch(NumberFormatException e)
    {
    }
    return iVal;
    }

    public static double readDouble()
    {
    String line = fetch();
    double dVal = 0.0;
    try
    {
    dVal = Double.parseDouble(line);
    }
    catch(NumberFormatException e)
    {
    }
    return dVal;
    }


    static char readChar()
    {
    return fetch().charAt(0);
    }
    };

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,525
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by corney_16 View Post
    Java Code:
    Stock.java:213: operator > cannot be applied to int[],int
    		if (quantity > 0)
    		             ^
    \Stock.java:222: operator > cannot be applied to double[],double
    			if (cost >0.0)
    			         ^
    
    \Stock.java:251: incompatible types
    found   : java.lang.String[]
    required: java.lang.String
    		return name;
    		       ^
    \Stock.java:258: incompatible types
    found   : int[]
    required: int
    		return quantity;
    		       ^
    \Stock.java:264: incompatible types
    found   : double[]
    required: double
    		return cost;
    		       ^
    5 errors
    All your errors are similar errors: you supply the operator with an entire array while it expected a single element, e.g. you try to return quantity (an array) while you have to return an int (a single element). Read the details of each error message; they are all self explanatory if you know how to read them.

    kind regards,

    Jos

Similar Threads

  1. operator || cant be applied to OlimpicFrog, boolean
    By darkblue24 in forum New To Java
    Replies: 2
    Last Post: 02-16-2010, 12:37 AM
  2. Replies: 4
    Last Post: 11-27-2008, 06:09 AM
  3. Replies: 6
    Last Post: 10-08-2008, 08:09 AM
  4. Error: cannot be applied to (java.lang.String)
    By carl in forum New To Java
    Replies: 1
    Last Post: 08-05-2007, 06:33 AM
  5. Replies: 1
    Last Post: 08-01-2007, 06:10 AM

Tags for this Thread

Posting Permissions

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