java - I'm really having trouble figuring out how to make my GUI window display the values in the array -


f nudge me in right direction great.

or tell me if have redo chunk or something.

this program rolling dice. i'm trying make each individual 'roll' display in ediefield.

here class:

import java.util.random;  public class dice {   private int times;   private int roll;   private int side;   public int[] each;   random roller = new random();    public void settimes(int rolls)   {     times = rolls;   }    public void setsides(int die)   {     side = die;   }    public int getroll()   {      int[] each = new int[times];     int total = 0;     int c = 0;     int = 0;     while (c < times)     {       c = c + 1;       roll = roller.nextint(side);       roll = roll + 1;       each[i] = roll;       total = total + roll;       system.out.print(each[i] + " ");       = + 1;     }     return total;   }    public int geteach()   {     return each[/*each number in array*/];   } } 

here guiwindow:

import javax.swing.*; import java.awt.*; import java.awt.event.*;  public class guiwindow extends jframe {    private dice dices = new dice();     private jlabel dice = new jlabel("# of dice");    private jlabel sides = new jlabel("# of sides");    private jlabel total = new jlabel("total:");    private jtextfield dicefield = new jtextfield("");    private jtextfield sidefield = new jtextfield("");    private jtextfield totfield = new jtextfield("");    private jbutton button = new jbutton ("roll!");    private jtextfield ediefield = new jtextfield("");    private jlabel empt = new jlabel("here each roll");     // constructor    public guiwindow()    {       jpanel datapanel = new jpanel(new gridlayout(2, 2, 12, 6));       datapanel.add(dice);       datapanel.add(sides);       datapanel.add(dicefield);       datapanel.add(sidefield);       jpanel rpanel = new jpanel(new gridlayout(1, 3, 12, 6));       rpanel.add(button);       rpanel.add(total);       rpanel.add(totfield);             jpanel ediepan = new jpanel(new gridlayout(2, 1, 12, 6));       ediepan.add(empt);       ediepan.add(ediefield);       container container = getcontentpane();       container.add(datapanel, borderlayout.west);       container.add(rpanel, borderlayout.east);       container.add(ediepan, borderlayout.south);       button.addactionlistener(new dielistener());    }     // >>>>>>> controller <<<<<<<<        private class dielistener implements actionlistener    {       public void actionperformed(actionevent e)       {         try         {           string input = dicefield.gettext();           int die = integer.parseint(input);           dices.settimes(die);           string inputa = sidefield.gettext();           int side = integer.parseint(inputa);           dices.setsides(side);           int tot = dices.getroll();           totfield.settext("" + tot);         }         catch(exception ex)          {           joptionpane.showmessagedialog(guiwindow.this,                                          "sorry,\nyou can dice.",                                          "dice fail",                                          joptionpane.error_message);          }          int eachd = dices.geteach();         ediefield.settext("" + eachd);         }    } } 

and main:

import javax.swing.*;  public class diceroller {    public static void main(string[] args)    {       guiwindow thegui = new guiwindow();       thegui.settitle("dice roller");       thegui.setdefaultcloseoperation(jframe.exit_on_close);       thegui.pack();       thegui.setvisible(true);    } } 

i hope not bad conduct or anything, have no idea do. (and stupid textbook useless) enter 4 dice 4 sides , numbers on if rolled them hand 2, 4, 3, 4. added 13. 13 goes in 'totfield' , 2 4 3 4 go in 'ediefield'. cant work. keep getting nullpointerexception , don't know how keep array each[] can numbers ediefield (or else list or whatever).

there few problems approach, 1 causing main problem is:

public int getroll()   {      int[] each = new int[times]; 

you created local variable called "each", never instantiate 1 in dice class. once function leaves scope, local "each" lost, causing null pointer error. think you're setting class variable when did not.

get rid of local definition of each variable (the int[] portion).

the next part geteach function. see you're trying that's not code do. try (though may want change name getrolllist()):

  public string geteach()    {      stringbuffer sb = new stringbuffer();       (int : each) {        sb.append(i).append(",");      }       return sb.tostring();    } 

and change guiwindow class dielistener actionevent to:

string eachd = dices.geteach(); 

(yes, i'll leave figure out last comma, heh, use space instead of comma). able code work these changes.

something else should change. general convention naming java class make first character of class name capital letter. "dice" class should changed "dice".


Comments

Popular posts from this blog

javascript - Enclosure Memory Copies -

php - Replacing tags in braces, even nested tags, with regex -