• Welcome to TechPowerUp Forums, Guest! Please check out our forum guidelines for info related to our community.
  • The forums have been upgraded with support for dark mode. By default it will follow the setting on your system/browser. You may override it by scrolling to the end of the page and clicking the gears icon.

Rhino's "I need Java help" thread

letterArray has only one char (containing "2").

strange because letterArray is populated using

int len2 = textfieldString.length()

and textfieldString is a String that = tfInput.getText() and at that point it isnt populated at all.
 
fixed that little issue. i had to move the letterArray and secretWordArray creation to the actionPerformed section. i also moved the if statement that compares the two arrays to the actionPerformed method.

now i just need to get it to actually write the input letter to the mask which is hiding the secret word. for that i have...

Code:
for(int i = 0; i < inputString.length(); i++)
			{
				if(secretWordArray[i] == letterArray[j])
				{
					temp += textfieldString;
				}	
				else
				{
					temp += inputMask;
				}
			}

but when i hit the OK button nothing happens. maybe my code isnt actually changing the value??

Code:
import java.awt.BorderLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;


public class TimeBomb extends JFrame implements ActionListener
{
	public String inputString = "";
	public String textfieldString = "";
	public String inputMask = "*";//mask
	public String temp = "";
	int i,j;
	
	JTextField tfInput = new JTextField(1);
	JLabel lblMessage = new JLabel("Guess A Letter: ");
	JLabel maskedWord = new JLabel();
	JLabel guessedLetter = new JLabel();
	JButton btnOK = new JButton("OK");
	JLabel timer = new JLabel("*-----");
	
	public TimeBomb() //constructor for GUI
	{
		super("Time Bomb Game");
	
		inputString = JOptionPane.showInputDialog("Enter Secret Word: ");//user enters secret word
		
		for(int i = 0; i < inputString.length(); i++)//gets the length of the secret word
		{
			temp +=  inputMask;//creates a mask of the secret word of the appropriate length
			maskedWord = new JLabel(temp);
		}
		

		Font f1 = new Font("Dialog", Font.PLAIN, 24);//set main font
		Font f2 = new Font("Dialog", Font.PLAIN, 32);//set larger font
		lblMessage.setFont(f1);
		tfInput.setFont(f1);
		guessedLetter.setFont(f1);
		btnOK.setFont(f1);
		timer.setFont(f2);
		maskedWord.setFont(f1);
		
		getContentPane().setLayout(new BorderLayout(1,1));//simple gui layout

		JPanel northPanel = new JPanel();
		northPanel.add(lblMessage);
		northPanel.add(tfInput);
		northPanel.add(btnOK);
		btnOK.addActionListener(this);
		getContentPane().add(northPanel, "North");
	
		JPanel centerPanel = new JPanel();
		centerPanel.add(maskedWord);
		centerPanel.add(guessedLetter);
		getContentPane().add(centerPanel, "Center");
		
		JPanel southPanel = new JPanel();
		southPanel.add(timer);//count down to explosion
		getContentPane().add(southPanel, "South");
		
		setSize(350, 200);//should be large enough on all platforms
		setVisible(true);
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		
	}
	

	
	public void actionPerformed(ActionEvent e)
	{
		if ("OK".equals(e.getActionCommand()))//OK button gets entered letter and puts it in the south panel
		{
			textfieldString = tfInput.getText();//string created from JTextField
			int len2 = textfieldString.length();//gets the length of the letter
			char[] letterArray = new char[len2];//creates array using len2
			for(int j = 0; j < len2; j++)
			{
				letterArray[j] = textfieldString.charAt(j);//populates array using textfieldString
			}
			
			int len = inputString.length();//gets the length of the secret word and stores it in len
			char[] secretWordArray = new char[len];//create array using len
			for(int i = 0; i < len; i++)
			{
				secretWordArray[i] = inputString.charAt(i);//populate array using inputString
			}
			
			for(int i = 0; i < inputString.length(); i++)
			{
				if(secretWordArray[i] == letterArray[j])
				{
					temp += textfieldString;
				}	
				else
				{
					temp += inputMask;
				}
			}
		}

	}//end of actionPerformed
	
	public static void main(String[] args) 
	{
		new TimeBomb();
	}
}//end TimeBomb class
 
You need to do something with "temp" once it exits the for loop (probably assign to both your equivilent of _Reveal and also to the text display).
 
You need to do something with "temp" once it exits the for loop (probably assign to both your equivilent of _Reveal and also to the text display).

the problem tho is you cant do a .setText or a .getText with strings, only jlabels, jtextfields, etc.

edit! ok so the command is maskedWord.setText(temp);

however it is a little glitchy lol! it creates a whole new masked array with only the letter guessed revealed. so my code isnt perfect yet obviously.
if i guess a letter wrong it creates a whole new ****** and if i guess a letter right it creates s***** and then when i guess another letter right it creates a new se**** so it jus t keeps making them and my field has ******s******e****** just running on.

so i need to have it override the original maskedWord and keep the letters correctly guessed.
 
Last edited:
You have to update your equvilent of _Reveal so every time the code is executed, it is comparing the _Secret to the current _Reveal.

If you are dealing with strings:
Get: (something) = myvar;
Set: myvar = (something);

set variable = get variable
 
uhm, ok let me work on this a bit.
 
Last edited:
well i cant seem to troubleshoot this issue. here is my actionPerformed()

you can see the arrays are populated here and then compared using a for statement. temp is actually equal to the masked string which happens in the constructor. so then i want textFieldString, which is the letter entered in the JTextField, to += temp. but it only puts out the guessed letter and keeps the initial temp mask.

Code:
	public void actionPerformed(ActionEvent e)
	{
		if ("OK".equals(e.getActionCommand()))//OK button gets entered letter and puts it in the south panel
		{
			textfieldString = tfInput.getText();//string created from JTextField
			int len2 = textfieldString.length();//gets the length of the letter
			char[] letterArray = new char[len2];//creates array using len2
			for(int j = 0; j < len2; j++)
			{
				letterArray[j] = textfieldString.charAt(j);//populates array using textfieldString
			}
			
			int len = inputString.length();//gets the length of the secret word and stores it in len
			char[] secretWordArray = new char[len];//create array using len
			for(int i = 0; i < len; i++)
			{
				secretWordArray[i] = inputString.charAt(i);//populate array using inputString
			}
			
			
			for(int i = 0; i < inputString.length(); i++)
			{
				if(secretWordArray[i] == letterArray[j])
				{
					temp += textfieldString;
					
				}
				else
				{
					temp += inputMask;
					timer.setText(boom.getText());
				}
				
			}
			maskedWord.setText(temp);
			
		}

	}//end of actionPerformed
 
You could use String.getChar() instead of copying it manually.

e.g.
Code:
textfieldString.getChars(0, textfieldString.length() - 1, letterArray, 0);


Once you have your temp populated, you'll want to do "new JTextField(temp)" to update that JTextField or, if possible, .setText("temp"). "temp" should be defined as a String.
 
Last edited:
i did a little trouble shooting by adding

System.out.println(temp) to my if statement. like so...

Code:
for(int i = 0; i < inputString.length(); i++)
			{
				if(secretWordArray[i] == letterArray[j])
				{
					System.out.println(temp);
					temp += textfieldString;
					System.out.println(temp);
				}
				else
				{
					temp += inputMask;
					System.out.println(temp);
				}
			}
			maskedWord.setText(temp);

if the secret word is secret then the console prints out that temp is equal too...

****** <--- this is temp before textfieldString is += to it
******s <--- this is temp after textfieldString is += to it
******s* <--- and the next 5 lines are what happens in the else statement temp += inputMask
******s**
******s***
******s****
******s*****

im not sure why it runs both the if and else part of the code. isnt it either or??? (if the letter does match a letter in the secret word it runs both, if it doesnt match it only runs else) and this explains that with temp += textfieldString it is only adding the entered letter onto the mask, NOT replacing the * with the letter.

so ive screwed something up somewhere.
 
Last edited:
temp needs to be cleared (temp = "") before it is used (the for loop).
 
temp needs to be cleared (temp = "") before it is used (the for loop).

hrm, i think i am getting there. except if my if else statement when the letter is correct is does both if and else!! and when it is incorrect is only does else(like it should here)
 
It can never do if and else with the same evaluation. It has to be reevaluated to send it on a different code path.
 
It can never do if and else with the same evaluation. It has to be reevaluated to send it on a different code path.

i dont want it to do if and else, but it is. that is the strange thing.
 
It is an impossibility for it to do both. The syntax is correct.
 
It is an impossibility for it to do both. The syntax is correct.

weird glitch. anyway, i cant seem to replace the correct letter with the correct *. += just addes the letter to the existing mask, it doesnt replace the correct * with the letter.
 
In a String, you can't replace just one letter. That's why you append it. You are creating a new string one charater at a time.

Edit: Did you put that temp = ""; in there yet?
Code:
			temp = "";
			for(int i = 0; i < inputString.length(); i++)
			{
				if(secretWordArray[i] == letterArray[j])
				{
					temp += textfieldString;
					
				}
				else
				{
					temp += inputMask;
					timer.setText(boom.getText());
				}
				
			}
			maskedWord.setText(temp);
 
In a String, you can't replace just one letter. That's why you append it. You are creating a new string one charater at a time.

Edit: Did you put that temp = ""; in there yet?
Code:
			temp = "";
			for(int i = 0; i < inputString.length(); i++)
			{
				if(secretWordArray[i] == letterArray[j])
				{
					temp += textfieldString;
					
				}
				else
				{
					temp += inputMask;
					timer.setText(boom.getText());
				}
				
			}
			maskedWord.setText(temp);

yes i put temp = ""; in there and it simply wipes out the value of temp but that doesnt really solve my issue. my issue is that when i choose a letter if it is correct it replaces the *.

i want this to happen. secret word is secret
it creates a mask ******
i guess s
the output on the gui is now s*****
i guess e
the output on the gui is now se**e*
i guess t
the output on the gui is now se**et
and so on.

so with my current code it totally replaces ****** with s
and then when i enter e it reads ee
and when i enter t it reads t

so it isnt
1) replacing * with the correct letter
2) storing that value in memory

NOTE: my algorithm is going to blow my professor away lol.
 
Code:
secretWordArray[i] == letterArray[j]

You have i and j there. they should both be i or j (most likely i).
 
Code:
secretWordArray[i] == letterArray[j]

You have i and j there. they should both be i or j (most likely i).

they have to be different or the program poops out a massive error listing

Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 1
at TimeBomb.actionPerformed(TimeBomb.java:107)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2012)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2335)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:404)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:253)
at java.awt.Component.processMouseEvent(Component.java:6108)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3276)
at java.awt.Component.processEvent(Component.java:5873)
at java.awt.Container.processEvent(Container.java:2105)
at java.awt.Component.dispatchEventImpl(Component.java:4469)
at java.awt.Container.dispatchEventImpl(Container.java:2163)
at java.awt.Component.dispatchEvent(Component.java:4295)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4461)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4125)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4055)
at java.awt.Container.dispatchEventImpl(Container.java:2149)
at java.awt.Window.dispatchEventImpl(Window.java:2478)
at java.awt.Component.dispatchEvent(Component.java:4295)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:604)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:275)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:200)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:190)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:185)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:177)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:138)
 
Why are they not equal in length?
 
Why are they not equal in length?

letterArray is obviously only going to be 1 char long and secretWordArray is going to be however long inputString is.
 
If that is the case, then you aren't comparing your equivlent of _Secret and _Reveal. You must perform that comparison in order to update your display (temp). Without it, you would get "s*****" on the first one then "*e**e*" on the second instead of "se**e*"


If the array is only one character long, it should be a char instead of an array. String.charAt(0) to get the first character of a String.
 
If that is the case, then you aren't comparing your equivlent of _Secret and _Reveal. You must perform that comparison in order to update your display (temp). Without it, you would get "s*****" on the first one then "*e**e*" on the second instead of "se**e*"


If the array is only one character long, it should be a char instead of an array. String.charAt(0) to get the first character of a String.

so let me double check then...this line

Code:
if(secretWordArray[i] == textfieldString.charAt(0))

says that if the char at position 0 on textfieldString is equal to any of the characters in the array secretWordArray...

right?
 
my prof just told me that it would be easier to use the indexof() and lastindexof() methods to determine if the letter is part of the word. so i should go with strings rather than arrays. bah,

now i am totally lost.
 
so let me double check then...this line

Code:
if(secretWordArray[i] == textfieldString.charAt(0))

says that if the char at position 0 on textfieldString is equal to any of the characters in the array secretWordArray...

right?
The if statement only checks one. The for loop makes it run the if statement for the length of secretWordArray.

I converted Magic to Java for you:
Code:
	private Boolean Magic(char letter)
	{
		Boolean hit = false;

		String temp = "";
		for (int i = 0; i < secretWordArray.length(); i++)
		{
			if (textfieldString.charAt(i) == inputMask.charAt(i))
			{
				if (letter == secretWordArray.charAt(i))
				{
					temp += secretWordArray.charAt(i);
					hit = true;
				}
				else
				{
					temp += inputMask.charAt(i);
				}
			}
			else
			{
				temp += textfieldString.charAt(i);
			}
		}
		textfieldString = temp;

		return hit;
	}
I didn't test it but what it is trying to do is exactly what you need. Notice how there is more than one if statement.

After running that code, you would still have to update the GUI with the text.


What IDE are you using to create this?
 
Back
Top