Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

Java Help.

  • 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, Registered Users 2, Paid Member Posts: 2,032 ✭✭✭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, Registered Users 2 Posts: 12,026 ✭✭✭✭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