8 Replies - 2995 Views - Last Post: 28 March 2012 - 01:33 PM Rate Topic: -----

#1 Mememe1002091  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 28-March 12

Generate random string from array and display the number of JTextField

Posted 28 March 2012 - 11:02 AM

I have an array of strings here. I need to randomly generate a string and display the number of JTextField on my interface. if the randomly generated string has 8 character, then I need to display 8 text field. I tried with my code below but its not showing. Where am I doing wrong here?

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Random;

class PuzzleSolving extends JFrame{
	private JTextField textField1;
	private JTextField textField2;
	private JTextField textField3;
	private JTextField textField4;
	private JTextField textField5;
	private JTextField textField6;
	private JTextField textField7;
	private JTextField textField8;
	private JTextField textField9;
	private JTextField textField10;
	private JTextField textField11;
	public PuzzleSolving() {
		setTitle("Puzzle-Solving Game");
		setLocation(50,25);
		setSize(600,700);

		textField1 = new JTextField(1);
		textField1.setEditable(false);
		textField2 = new JTextField(1);
		textField2.setEditable(false);
		textField3 = new JTextField(1);
		textField3.setEditable(false);
		textField4 = new JTextField(1);
		textField4.setEditable(false);
		textField5 = new JTextField(1);
		textField5.setEditable(false);
		textField6 = new JTextField(1);
		textField6.setEditable(false);
		textField7 = new JTextField(1);
		textField7.setEditable(false);
		textField8 = new JTextField(1);
		textField8.setEditable(false);
		textField9 = new JTextField(1);
		textField9.setEditable(false);
		textField10 = new JTextField(1);
		textField10.setEditable(false);
		textField11 = new JTextField(1);
		textField11.setEditable(false);

		String [] country = {"Malaysia", "Papua New Guinea", "New Zealand", "Hong Kong", "Sri Lanka",
							"Indonesia", "Thailand", "Singapore", "Philippines", "Australia"};

		Container container = getContentPane();
		container.setLayout(new BorderLayout());
		JPanel guessPanel = new JPanel();

		Random generator = new Random();
		for( int i=0; i < country.length; i++ ){
			int n = generator.nextInt(country.length);
			if(country[n] == country[0]){
				guessPanel.add(textField1);
				guessPanel.add(textField2);
				guessPanel.add(textField3);
				guessPanel.add(textField4);
				guessPanel.add(textField5);
				guessPanel.add(textField6);
				guessPanel.add(textField7);
				guessPanel.add(textField8);
			}
		}
		container.add(guessPanel,BorderLayout.NORTH);
	}
	public static void main(String[]args)
	{
		PuzzleSolving guessing = new PuzzleSolving();
		guessing.setVisible(true);
		guessing.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}
}



Is This A Good Question/Topic? 0
  • +

Replies To: Generate random string from array and display the number of JTextField

#2 Ryano121  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1363
  • View blog
  • Posts: 3,002
  • Joined: 30-January 11

Re: Generate random string from array and display the number of JTextField

Posted 28 March 2012 - 11:13 AM

Why have some many individual JTextField instance variables? With the current design a predict A LOT of code repetition.

If you make an array of JTextField instances, when you create your random number, you can then just loop through each element in the array and add it/ set its visibility or whatever.

Saves you from having a ton of variables all over the place which gets way out of hand very quickly.
Was This Post Helpful? 1
  • +
  • -

#3 Mememe1002091  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 28-March 12

Re: Generate random string from array and display the number of JTextField

Posted 28 March 2012 - 11:35 AM

View PostRyano121, on 28 March 2012 - 11:13 AM, said:

Why have some many individual JTextField instance variables? With the current design a predict A LOT of code repetition.

If you make an array of JTextField instances, when you create your random number, you can then just loop through each element in the array and add it/ set its visibility or whatever.

Saves you from having a ton of variables all over the place which gets way out of hand very quickly.


I made the changes like what you said. I'm getting only 1 text field. Is there something wrong with my generate random country loop?
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Random;

class PuzzleSolving extends JFrame{
	private JTextField textField[];

	public PuzzleSolving() {
		setTitle("Puzzle-Solving Game");
		setLocation(50,25);
		setSize(600,700);
		
		JTextField textField[] = new JTextField[11];
		for(int i=0; i<textField.length; i++){
			textField[i] = new JTextField(1);
			textField[i].setEditable(false);
		}

		String [] country = {"Malaysia", "Papua New Guinea", "New Zealand", "Hong Kong", "Sri Lanka",
							"Indonesia", "Thailand", "Singapore", "Philippines", "Australia"};

		Container container = getContentPane();
		container.setLayout(new BorderLayout());
		JPanel guessPanel = new JPanel();

		Random generator = new Random();
		for( int i=0; i < country.length; i++ ){
			int n = generator.nextInt(country.length);
			if(country[n] == country[0]){
				guessPanel.add(textField[7]);
			}
		}

		container.add(guessPanel,BorderLayout.NORTH);
	}

	public static void main(String[]args)
	{
		PuzzleSolving guessing = new PuzzleSolving();
		guessing.setVisible(true);
		guessing.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

	}
}

Was This Post Helpful? 0
  • +
  • -

#4 Ryano121  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1363
  • View blog
  • Posts: 3,002
  • Joined: 30-January 11

Re: Generate random string from array and display the number of JTextField

Posted 28 March 2012 - 11:37 AM

Why do you have two arrays of JTextFields?

private JTextField textField[];
...
JTextField textField[] = new JTextField[11];


What is this code supposed to be doing?

if(country[n] == country[0]){
        guessPanel.add(textField[7]);


This essentially does nothing. You are trying to check whether or not the random string is equal to the first string in the array. But why? Also this will not work because the == operator is checking the equality of the memory addresses and not the actual values of the string. You need to use the .equals method instead.

Then why do you want to be adding just the seventh JTextField? Why the seventh?

This post has been edited by Ryano121: 28 March 2012 - 11:59 AM

Was This Post Helpful? 0
  • +
  • -

#5 Mememe1002091  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 28-March 12

Re: Generate random string from array and display the number of JTextField

Posted 28 March 2012 - 11:56 AM

View PostRyano121, on 28 March 2012 - 11:37 AM, said:

Why do you have two arrays of JTextFields?

private JTextField textField[];
...
JTextField textField[] = new JTextField[11];


Edit - Scrap that. What is this code supposed to be doing?

if(country[n] == country[0]){
        guessPanel.add(textField[7]);


This essentially does nothing. You are trying to check whether or not the random string is equal to the first string in the array. But why? Also this will not work because the == operator is checking the equality of the memory addresses and not the actual values of the string. You need to use the .equals method instead.

Then why do you want to be adding just the seventh JTextField? Why the seventh?


I'm trying to check whether the generated string equals to the first string or not to determine how many text field i should display. Lets say if the generated string is Malaysia, then i need to create 8 text field. Else if generated string is Indonesia, I need to display 9 text field. I haven't get to that part yet, cuz I still have not get the loop part correct.

Erm..is this correct? I still get only 1 text field. =(
for( int i=0; i < country.length; i++ ){
			int n = generator.nextInt(country.length);
			if(country[n].equals(country[0]){
				guessPanel.add(textField[n]);
			}
		}

Was This Post Helpful? 0
  • +
  • -

#6 Ryano121  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1363
  • View blog
  • Posts: 3,002
  • Joined: 30-January 11

Re: Generate random string from array and display the number of JTextField

Posted 28 March 2012 - 12:03 PM

Comparing the random string to the first element doesn't help you in any way.

Lets step through this a bit more logically.

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


Doesn't really make sense. We don't need to loop through each element of the array at this point.

Instead we can generate the random number straight away -

int n = generator.nextInt(country.length);


Great, we now have a random number that corresponds to an index in the array.

No we just need to get the length of that particular String to determine how many textfields should be added -

int length = county[n].length


Now we have the number of textfields we need to add, we can start adding them to the panel -

for(int j = 0; j < length; j++)
{
    // add a new JTextField to the panel
}

Was This Post Helpful? 1
  • +
  • -

#7 Mememe1002091  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 28-March 12

Re: Generate random string from array and display the number of JTextField

Posted 28 March 2012 - 12:25 PM

View PostRyano121, on 28 March 2012 - 12:03 PM, said:

Comparing the random string to the first element doesn't help you in any way.

Lets step through this a bit more logically.

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


Doesn't really make sense. We don't need to loop through each element of the array at this point.

Instead we can generate the random number straight away -

int n = generator.nextInt(country.length);


Great, we now have a random number that corresponds to an index in the array.

No we just need to get the length of that particular String to determine how many textfields should be added -

int length = county[n].length


Now we have the number of textfields we need to add, we can start adding them to the panel -

for(int j = 0; j < length; j++)
{
    // add a new JTextField to the panel
}


Now it makes sense. But there's an error for this.

int length = country[n].length;

Was This Post Helpful? 0
  • +
  • -

#8 Ryano121  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1363
  • View blog
  • Posts: 3,002
  • Joined: 30-January 11

Re: Generate random string from array and display the number of JTextField

Posted 28 March 2012 - 12:29 PM

Sorry its a typo - I forgot to include the parenthesis. The String class presents the length of the String as a method

int length = country[n].length();

Was This Post Helpful? 0
  • +
  • -

#9 Mememe1002091  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 28-March 12

Re: Generate random string from array and display the number of JTextField

Posted 28 March 2012 - 01:33 PM

View PostRyano121, on 28 March 2012 - 12:03 PM, said:

Comparing the random string to the first element doesn't help you in any way.

Lets step through this a bit more logically.

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


Doesn't really make sense. We don't need to loop through each element of the array at this point.

Instead we can generate the random number straight away -

int n = generator.nextInt(country.length);


Great, we now have a random number that corresponds to an index in the array.

No we just need to get the length of that particular String to determine how many textfields should be added -

int length = county[n].length


Now we have the number of textfields we need to add, we can start adding them to the panel -

for(int j = 0; j < length; j++)
{
    // add a new JTextField to the panel
}


Wait, i can't get the length of the string n then determine no. of textfields should be added. I would have trouble when the generated country is Papua New Guinea. Okay, so if the generated country is Papua New Guinea..I should have 3 lines of textfields.Something like this...

_ _ _ _ _
_ _ _
_ _ _ _ _ _
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1