TextListeners and arrays

Ok i'm working throught this online tutorial for java and almost done. I worked through a program that has u create a lotto game, u can use quick pick or enter ur own numbers. It runs the game until u get 6 out of 6 numbers. then u can play again. Now the winning numbers which the computer generates a random number 1-50 is a TextField array. and user numbers is assigned byTextField[ ] numbers = new TextField[6]. What i am having probs with is that i'm suppose to use the textlistener to detemine if the user enters the numbers that it is a number between 1-50 and not allow the game to start... this is all i can think of and it doesn't work

numbers.addTextListener();

this i get an cannot resolve symbol, but only happens when i try to add

a listener to an array

public void textValueChanged(TextEvent txt)

{

this i'm just getting confused on how i'm using the argument

}

any advice

[947 byte] By [asiahdaia] at [2007-11-27 4:55:22]
# 1
let's see more of your code. BTW, you can't add a listener to an array, you have to loop through the array and add the listener to each JTextField item.
petes1234a at 2007-7-12 10:10:18 > top of Java-index,Java Essentials,New To Java...
# 2

For instance if the enclosing class implements the keyListener interface, you could add the keylisteners like so:

for (int i = 0; i < numbers.length; i++)

{

numbers[i] = new JTextField(3);

numbers[i].addKeyListener(this);

pane.add(numbers[i]);

}

Message was edited by:

petes1234

petes1234a at 2007-7-12 10:10:18 > top of Java-index,Java Essentials,New To Java...
# 3

ok i got the text listener to work , it compiles it runs but anytime a textfield is set to null i get a long list of exception in the background about java.lang.NumberFormatException: for input string: " "

here is all my code cause i don't know what u need.. sorry little messy

import java.awt.*;

import java.awt.event.*;

import java.awt.event.TextListener.*;

public class LottoMadness extends java.applet.Applet implements ItemListener, ActionListener, TextListener, Runnable

{

Thread playing;

Panel row1 = new Panel();

CheckboxGroup option = new CheckboxGroup();

Checkbox quickpick = new Checkbox("Quick Pick", option, false);

Checkbox personal = new Checkbox("Personal", option, false);

Panel row2 = new Panel();

Label numbersLabel = new Label("Your picks: ", Label.RIGHT);

TextField[] numbers = new TextField[6];

Label winnerLabel = new Label("Winners: ", Label.RIGHT);

TextField[] winners = new TextField[7]; //array for 6 textfields to declare winning numbers, 1 for slowdown

TextField slowDown = winners[6];

Panel row3 = new Panel();

Button stop = new Button("Stop");

Button play = new Button("Play");

Button reset = new Button("Reset");

Panel row4 = new Panel();

Label got3Label = new Label("3 of 6: ", Label.RIGHT);

TextField got3 = new TextField();

Label got4Label = new Label("4 of 6: ", Label.RIGHT);

TextField got4 = new TextField();

Label got5Label = new Label("5 of 6: ", Label.RIGHT);

TextField got5 = new TextField(); //creates text field next to 5 of 6

Label got6Label = new Label("6 of 6: ", Label.RIGHT);

TextField got6 = new TextField(); //creates text field next to 6 of 6

Label drawingsLabel = new Label("Drawings: ", Label.RIGHT);

TextField drawings = new TextField(); //creates text field next to drawings

Label yearsLabel = new Label("Years: ", Label.RIGHT);

TextField years = new TextField(); //creates text field next to years

public void init()

{

setBackground(Color.lightGray);

GridLayout appletLayout = new GridLayout(5, 1, 10, 10);

setLayout(appletLayout);

//add Listeners

quickpick.addItemListener(this);

personal.addItemListener(this);

stop.addActionListener(this);

play.addActionListener(this);

reset.addActionListener(this);

FlowLayout layout1 = new FlowLayout(FlowLayout.CENTER, 10, 10);

row1.setLayout(layout1); //associates layout1 with Panel row1

row1.add(quickpick); //adds quickpick to row1

row1.add(personal); //adds personal to row1

add(row1); //adds row1 to applet

GridLayout layout2 = new GridLayout(2, 7, 10, 10);

row2.setLayout(layout2);

row2.add(numbersLabel);

for(int i = 0; i < 6; i++)

{

numbers = new TextField();

row2.add(numbers);

}

for(int i = 0; i < 6; i++)

{

numbers.addTextListener(this);

}

row2.add(winnerLabel); //adds winnerLabel to row2

for(int i = 0; i < 6; i++)

{

winners = new TextField(); //sets up new TextField object

winners.setEditable(false); //does not allow user to edit this text box

row2.add(winners); //adds each of the text boxes next to winners

}

add(row2); //adds row2 to applet

FlowLayout layout3 = new FlowLayout(FlowLayout.CENTER, 10, 10);

row3.setLayout(layout3); //associates layout3 with Panel row3

stop.setEnabled(false);

row3.add(stop); //adds stop button to row3

row3.add(play); //adds play button to row3

row3.add(reset); //adds reset button to row3

add(row3); //adds row3 to applet

GridLayout layout4 = new GridLayout(2, 2, 20, 10);

row4.setLayout(layout4); //associates layout4 with Panel row4

row4.add(got3Label); //add got3Label to row4

got3.setEditable(false); //don't allow user to edit it

row4.add(got3); //add textfield got3 to row4

row4.add(got4Label); //ect same as above

got4.setEditable(false);

row4.add(got4);

row4.add(got5Label);

got5.setEditable(false);

row4.add(got5);

row4.add(got6Label);

got6.setEditable(false);

row4.add(got6);

row4.add(drawingsLabel);

drawings.setEditable(false);

row4.add(drawings);

row4.add(yearsLabel);

years.setEditable(false);

row4.add(years);

add(row4); //add row4 to applet

}

public void actionPerformed(ActionEvent event) //if an action occurs from a button event info sent here

{

String command = event.getActionCommand(); //id's the component responsible for event

if(command == "Reset") //if component is Reset jump to clearAllFields() method

clearAllFields();

if(command == "Play") //if is Play

{

playing = new Thread(this); //create new thread object

playing.start(); //start game

play.setEnabled(false); //since game started play button not needed

stop.setEnabled(true); //since game started stop button should be available

reset.setEnabled(false); //since game running reset not needed

quickpick.setEnabled(false); //since game running quickpick or personal should not be able to be selected

personal.setEnabled(false);

}

if(command == "Stop") //if stop

{

playing.stop(); //stop game

stop.setEnabled(false);

play.setEnabled(true);

reset.setEnabled(true);

quickpick.setEnabled(true);

personal.setEnabled(true);

}

}

public void itemStateChanged(ItemEvent event)

{

String command = (String) event.getItem(); //determines which item is picked

if(command == "Quick Pick")

{

for(int i = 0; i < 6; i++)

{

int pick; //variable to hold the random number picked

do

{

pick = (int)Math.floor(Math.random() * 50 + 1);

}

while(numberGone(pick, numbers, i)); //create random number until this is no longer true from numberGone method

numbers.setText("" + pick); //sets the number text field to random numbers picked

}

}

else //if quick pick is not selected set all number text fields to null so user can enter the numbers

{

for(int i = 0; i < 6; i++)

numbers.setText(null);

}

}

public void textValueChanged(TextEvent txt)

{

Object source = txt.getSource();

for(int i = 0; i < 6; i++)

{

if(source == numbers)

{

int newNumber = Integer.parseInt(numbers.getText());

if(newNumber < 1 || newNumber > 50)

{

numbers.setText(null);

play.setEnabled(false);

}

}

}

}

void clearAllFields()

{

for(int i = 0; i < 6; i++)

{

numbers.setText(null);

winners.setText(null);

}

got3.setText(null);

got4.setText(null);

got5.setText(null);

got6.setText(null);

drawings.setText(null);

years.setText(null);

}

void addOneToField(TextField field)

{

int num = Integer.parseInt("0" + field.getText()); //get current number from field. 0 if nothing is there

num++;//adds one to number

field.setText("" + num); //displayes new number in field

}

boolean numberGone(int num, TextField[] pastNums, int count)

{

for(int i = 0; i < count; i++)

if(Integer.parseInt(pastNums.getText()) == num)

return true;

return false;

}

boolean matchedOne(TextField win, TextField[] allPicks)

{

for(int i = 0; i < 6; i++)

{

String winText = win.getText(); //gets number computer picks

if(winText.equals(allPicks.getText())) //sees if that number is equal to any numbers that user(or quick pick) picked

return true; //if computer number matches any of user numbers return true

}

return false; //if none were the same return false

}

public void run()

{

while(true)

{

addOneToField(drawings); //add one to drawings field

int draw = Integer.parseInt(drawings.getText()); //gets the number in the drawing field

float numYears = (float)draw/104; //divides that number by 104, to assume 104 lotto drawings are played in one year

years.setText("" + numYears); //sets numYears to the answer to display number of years playing lotto

int matches = 0;//variable for number of matches made

for(int i = 0; i < 7; i++)

{

if(winners == slowDown) //is new textField is equal to winner[6] (right after all numbers are picked for this round)

{

try{Thread.sleep(500);} catch(InterruptedException e){} //pause

break; //break from loop since winner[6] has no real values given to it

}

int ball; //variable which is goin to hold the numbers the computer generates

do

{

ball = (int)Math.floor(Math.random() * 50 + 1); //creates random number 1 - 50

}

while(numberGone(ball, winners, i)); //make sure random number is not the same as any other ones for this drawing

winners.setText("" + ball); //set random number to winners textfield

if(matchedOne(winners, numbers)) //if there was a match

matches++; //add one to matches

}

switch(matches)

{

case 3:

addOneToField(got3); //if 3 were matched add 1 to got3 field

break;

case 4:

addOneToField(got4); //if 4 were matched add 1 to got4 field

break;

case 5:

addOneToField(got5); //if 5 were matched add 1 to got5 field

break;

case 6:

addOneToField(got6); //if 6 were matched add 1 to got6 field

stop.setEnabled(false);

play.setEnabled(true); //allow user to press play button

playing.stop();//stop applet

}

}

}

}

asiahdaia at 2007-7-12 10:10:18 > top of Java-index,Java Essentials,New To Java...