Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Java Help.

Options
  • 22-02-2012 10:14am
    #1
    Closed Accounts Posts: 3


    Can someone please help me with this simple piece of Java?

    import java.util.Scanner;
    class Books
    {
    public static void main (String args[]) throws Exception
    {
    Scanner in = new Scanner(System.in);
    //These are my variables.
    int updateStock;
    int code;
    int category;
    //These are my arrays
    int [] stockCounter = new int [4];
    String[] bookCategory = {"Novel", "Historical Reference", "Non-Fiction", "Educational",};
    int [] userCode = {100, 200, 350, 400,};

    //Loop to count through the array.
    for (int i = 0; i < 4; i ++){; //This is the loop counter.
    System.out.println("Please enter your stock code " + userCode);
    //System.out.println("Stock for " + userCode "is " + updateStock);
    if (userCode == code [0]){
    System.out.println("How many do you want");
    updateStock[0] = in.nextint;
    }
    else
    userCode = code[1];
    System.out.println("How many do you want");
    updateStock[1] = in.nextInt;
    if (userCode = code[2]){
    System.out.println("How many do you want");
    updateStock[2] = in.nextInt;}
    else
    userCode = code[3];
    System.out.println("How many do you want");
    updateStock[3] = in.nextInt;}
    if (userCode = code[4]){
    System.out.println("How many do you want");
    updateStock[4] = in.nextInt;}


    }
    }


Comments

  • Registered Users Posts: 1,990 ✭✭✭lynchie


    Where to start...

    For statement syntactically incorrect.
    Referencing ints as arrays.
    Function calls are not correct.

    You need to fix the basic language errors first.


  • Closed Accounts Posts: 3 maximus_power


    Its pretty rough at the moment but I cant figure out how to resolve the array issue.


  • Registered Users Posts: 11,977 ✭✭✭✭Giblet


    = is an assignment
    == is equality

    So testing x = y is actually assigning y to x, not comparing.

    also, you have your arrays backwards

    code is an int, not an array.
    userCode is an array.

    so you need to do

    userCode[0] == code

    Arrays are 0 indexed, so an array with 4 items contains items at
    [0] [1] [2] and [3] only.


  • Closed Accounts Posts: 3 maximus_power


    Thank you Giblet.


Advertisement