Results 1 to 12 of 12

Thread: array in GUI

  1. #1
    creedkorn is offline Member
    Join Date
    Apr 2011
    Posts
    17
    Rep Power
    0

    Default array in GUI

    I need to array each drink, so that when value is input, the supply decreases by 1, and it displays back to the supply JTField. Here's my coding but it returns null value instead.

    code:
    public void actionPerformed(ActionEvent e)
    {
    double num1 = Double.parseDouble(jtf2.getText().trim());
    double result = 0.0;
    double change = 0.0;
    VendMach v[] = new VendMach[0];
    VendMach v1[] = new VendMach[0];

    count--;
    for(int i = 10; i >= v.length; i--){
    if(e.getSource() == jbt1)
    {
    result = 1.20;
    change = num1 - result;
    coffee.setDrink("latte");
    coffee.setResult(result);
    coffee.setAmount(num1);
    coffee.setChange(change);
    coffee.setSupply(i--);
    }
    }

    count--;
    for(int i = 10; i >= v1.length; i--){
    if(e.getSource() == jbt2)
    {
    result = 1.30;
    change = num1 - result;
    coffee.setDrink("White Coffee");
    coffee.setResult(result);
    coffee.setAmount(num1);
    coffee.setChange(change);
    coffee.setSupply(i--);
    }
    }

    jtf1.setText(String.valueOf(result));
    jtf3.setText(String.valueOf(change));
    jtf4.setText(String.valueOf(supply));

    showMessageDialog(null, coffee.toString());

    }

  2. #2
    Join Date
    Apr 2011
    Posts
    17
    Rep Power
    0

    Default

    What returns a null value?
    Also does it give you any exceptions? If so post the whole stacktrace

  3. #3
    f1gh is offline Member
    Join Date
    Nov 2010
    Posts
    46
    Rep Power
    0

    Default

    i'm trying to decipher your code here:

    the i is from the for loop. and you are already decrementing it to begin with. Is that how you intend it?

    I'll try to be helpful but i need clarification on what the code itself is doing, thanks.
    Last edited by f1gh; 04-22-2011 at 03:32 AM.

  4. #4
    Join Date
    Apr 2011
    Posts
    17
    Rep Power
    0

    Default

    setText(String s); is a void method, it's not supposed to return anything.
    Btw try doing something like jtf4.setText(coffee.getSupply());

  5. #5
    Join Date
    Apr 2011
    Posts
    17
    Rep Power
    0

    Default

    Quote Originally Posted by creedkorn View Post

    Untitled.png picture by creedkorn1 - Photobucket

    Here's the program, and the supply shows null value. "i" is supposed to be a variable for 'supply' and, for example, if the latte button is pressed, 'i' decrements, and show's the after result.
    You decrement "i" but I can't see you decrementing the "supply" field

  6. #6
    creedkorn is offline Member
    Join Date
    Apr 2011
    Posts
    17
    Rep Power
    0

    Default

    well the value of i is returned to the supply field, i.e 'i' value is supply value. is it possible ?

  7. #7
    Join Date
    Apr 2011
    Posts
    17
    Rep Power
    0

    Default

    Quote Originally Posted by creedkorn View Post
    well the value of i is returned to the supply field, i.e 'i' value is supply value. is it possible ?
    i is not the supply value, i is i and supply is supply.

    What happened when you tried coffee.getSupply()?

  8. #8
    Join Date
    Apr 2011
    Posts
    17
    Rep Power
    0

    Default

    Please post the whole class where you put that "actionPerformed()" method, I'm still trying to understand what you are trying to do. (in code tags and not quote tags :p)

    Java Code:
    VendMach v[] = new VendMach[0];
    VendMach v1[] = new VendMach[0];
    Why are you creating two new arrays with zero size each time that method is run? As far as I can see, all you are using them for is the for-loops (which I have no idea why you are using either).

    The coffee.setSupply(i--) sends the decremented i value to the supply text field
    No, that sends the value of i to the coffee object.
    Last edited by OutputStream; 04-22-2011 at 04:33 AM.

  9. #9
    creedkorn is offline Member
    Join Date
    Apr 2011
    Posts
    17
    Rep Power
    0

    Default

    I need to for loop each object(i.e latte, white coffe, etc) into an array of size 10. Simply put, there is 10 of each drink in stock. Now, when the user chooses a drink, stock for drink decreases......so i was doing for loops for each objects for.

  10. #10
    creedkorn is offline Member
    Join Date
    Apr 2011
    Posts
    17
    Rep Power
    0

    Default

    What's the coding error with passing i value to supply text field ?

  11. #11
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    I have edited your post #14 above and have changed your [quote] [/quote] tags to [code] [/code] tags so that the code formatting won't be lost.

  12. #12
    creedkorn is offline Member
    Join Date
    Apr 2011
    Posts
    17
    Rep Power
    0

    Default

    Thanks, so any idea what the error is in the GUI class, because the output seems to be different from the coding ?

Similar Threads

  1. Replies: 23
    Last Post: 09-07-2010, 09:12 PM
  2. Replies: 2
    Last Post: 09-06-2010, 02:03 AM
  3. create a 2d char array from a 1D string array
    By jschmall12 in forum New To Java
    Replies: 1
    Last Post: 04-27-2010, 10:01 PM
  4. Array length and printing out uninitialized array.
    By nicolek808 in forum New To Java
    Replies: 4
    Last Post: 09-10-2009, 10:12 AM
  5. Replies: 1
    Last Post: 03-31-2009, 07:40 AM

Posting Permissions

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