• 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

ok, in Magic(char letter)

secretWordArray is actually inputString.length() since you cant do a .length on an array. but that is fine since i guess Strings will be easier to work with.

the actionPerformed method is a toughy. when the user clicks the OK button it takes the text from tfInput and puts it in the string textfieldString. then i want to execute Magic. however it doesnt like the arguments for magic.

Code:
import java.awt.BorderLayout;
import java.awt.Component;
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 timebombtest extends JFrame implements ActionListener
{
	public String inputString = "";
	public String textfieldString = "";
	public String inputMask = "*";//mask
	public String mask = "";
	public String temp = "";
	
	JTextField tfInput = new JTextField(2);
	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 timebombtest() //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);		

		
		//set font
		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);
		
		//gui properties
		getContentPane().setLayout(new BorderLayout(1,1));

		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);
		getContentPane().add(southPanel, "South");
		
		setSize(350, 200);
		setVisible(true);
		setDefaultCloseOperation(EXIT_ON_CLOSE);
	}
	
	private Boolean Magic(char letter)
	{
		Boolean hit = false;

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

		return hit;
	}
	
	public void actionPerformed(ActionEvent e)
	{
		if ("OK".equals(e.getActionCommand()))
		{
			textfieldString = tfInput.getText();
			Magic();
		}
	}
	
	public static void main(String[] args) 
	{
		new timebombtest();
	}
}//end TimeBomb class

im using Eclipse !
 
The argument should be the letter the user input. My guess is you should do Magic(tfInput.getText().charAt(0)). After you call Magic, make sure to update your bomb if Magic returns false.

Rename the "temp" outside of Magic to "reveal" or something else. "temp" should only exist inside of Magic.

Code:
public String mask = "";
mask should be a char and it should be set once with initilization and used after that point instead of using .charAt() to access it.

It is Array.length instead of Array.length().


Eclipse should allow you to use break points to see what is in variables at that point in the application. It is very helpful.
 
this doesnt work. cant invoke charAt(int) on the array type char[]

Code:
if (letter == secretWordArray.charAt(i))
				{
					temp += secretWordArray.charAt(i);
					hit = true;
 
If it is defined as a char[] array, use brackets:

secreWordArray


.charAt() only applies to Strings.
 
i think it all comes down to actionPerformed...

also, in Magic... inputMask is a class level variable String

public String inputMask = "*";

while mask is

public char mask = '*';

so how would you suggest i encounter that?

Code:
public void actionPerformed(ActionEvent e)
	{
		if ("OK".equals(e.getActionCommand()))
		{
			textfieldString = tfInput.getText();
			Magic(tfInput.getText().charAt(0));
		}
	}

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at timebombtest.Magic(timebombtest.java:91)
at timebombtest.actionPerformed(timebombtest.java:120)
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)
 
Last edited:
i think it all comes down to actionPerformed...

also, in Magic... inputMask is a class level variable String
I guessed what names belonged where because I don't follow your naming at all. :( I think you had a variable missing (equivilent of reveal) but I really don't know.


public String inputMask = "*";

while mask is

public char mask = '*';

so how would you suggest i encounter that?
I don't follow? You can make a char a string by appending it to an empty string. e.g.
Code:
inputMask += mask;
The two should now equal although doing so is kind of pointless.



There's a NullPointerException at Magic(timebombtest.java:91). If you can copy that area and point out which line is 91, I should be able to help you with that.
 
I guessed what names belonged where because I don't follow your naming at all. :( I think you had a variable missing (equivilent of reveal) but I really don't know.



I don't follow? You can make a char a string by appending it to an empty string. e.g.
Code:
inputMask += mask;
The two should now equal although doing so is kind of pointless.

well cause in Magic inputMask.charAt(i) and if that is a char then you cant do .charAt(i)

There's a NullPointerException at Magic(timebombtest.java:91). If you can copy that area and point out which line is 91, I should be able to help you with that.

Code:
for (int i = 0; i < secretWordArray.length; i++)

and here is my entire code

Code:
import java.awt.BorderLayout;
import java.awt.Component;
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 timebombtest extends JFrame implements ActionListener
{
	public String inputString = "";
	public String textfieldString = "";
	public String inputMask = "*";
	public char mask = '*';
	public String hidereveal = "";
	public char[] secretWordArray;
	
	JTextField tfInput = new JTextField(2);
	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 timebombtest() //constructor for GUI
	{
		super("Time Bomb Game");
		
		inputString = JOptionPane.showInputDialog("Enter Secret Word: ");//user enters secret word
		textfieldString = tfInput.getText();
		
		for(int i = 0; i < inputString.length(); i++)
		{
			hidereveal +=  mask;//creates a mask of the secret word of the appropriate length
			maskedWord = new JLabel(hidereveal);		
		}
		
		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 <inputString.length(); i++)
		{
			secretWordArray[i] = inputString.charAt(i);//populate array using inputString
		}

		//set font
		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);
		
		//gui properties
		getContentPane().setLayout(new BorderLayout(1,1));

		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);
		getContentPane().add(southPanel, "South");
		
		setSize(350, 200);
		setVisible(true);
		setDefaultCloseOperation(EXIT_ON_CLOSE);
	}
	
	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[i])
				{
					temp += secretWordArray[i];
					hit = true;
				}
				else
				{
					temp += inputMask.charAt(i);
				}
			}
			else
			{
				temp += textfieldString.charAt(i);
			}
		}
		textfieldString = temp;

		return hit;
	}
	
	public void actionPerformed(ActionEvent e)
	{
		if ("OK".equals(e.getActionCommand()))
		{
			Magic(tfInput.getText().charAt(0));
		}
	}
	
	public static void main(String[] args) 
	{
		new timebombtest();
	}
}//end TimeBomb class
 
secretWordArray is null in Magic.

Edit:
Code:
	private Boolean Magic(char letter)
	{
		Boolean hit = false;

		String temp = "";
		for (int i = 0; i < inputString.length(); i++)
		{
			if (hidereveal.charAt(i) == mask)
			{
				if (hidereveal.charAt(i) == inputString.charAt(i))
				{
					temp += inputString.charAt(i);
					hit = true;
				}
				else
				{
					temp += mask;
				}
			}
			else
			{
				temp += hidereveal.charAt(i);
			}
		}
		hidereveal = temp;

		return hit;
	}
	
	public void actionPerformed(ActionEvent e)
	{
		if ("OK".equals(e.getActionCommand()))
		{
			if (tfInput.getText().length() > 0)
			{
			Magic(tfInput.getText().charAt(0));
			tfInput.setText(""); // clear for next letter
			maskedWord.setText(hidereveal);
			}
		}
	}

Before running Magic, make sure inputString has a value (e.g. "blamo"), make sure hidereveal matches inputString in length but containing all masks (e.g. "*****").
 
Last edited:
k i did a little troubleshooting with System.out.println and i noticed that in Magic that while
hidereveal does equal the number letters in the secret word it does not equal mask.

the mask before the for statement equals *

but after the for statement ( the secret word is awesome(7 letters) it shows

*
*
*
*
*
*
*

so 1 for every letter
 
mask should always be *

Only hidereveal and temp should change. temp is especially the one to watch because it is merging hidereveal, the letter, and inputString.


I don't see a problem. :confused:
 
for this code

Code:
	private Boolean Magic(char letter)
	{
		Boolean hit = false;

		String temp = "";
		System.out.println("inputString = " + inputString);
		System.out.println("hidereveal = " + hidereveal);
		System.out.println("mask = " + mask);
		for (int i = 0; i < inputString.length(); i++)
		{
			if (hidereveal.charAt(i) == mask)
			{
				System.out.println("mask = " + mask);
				if (hidereveal.charAt(i) == inputString.charAt(i))
				{
					temp += inputString.charAt(i);
					System.out.println("temp = " + temp);
					hit = true;
				}
				else
				{
					temp += mask;
					System.out.println("temp = " + temp);
					System.out.println("mask = " + mask);
				}
			}
			else
			{
				temp += hidereveal.charAt(i);
			}
		}
		hidereveal = temp;

i get this in console

inputString = awesome
hidereveal = *******
mask = *
mask = *
temp = *
mask = *
mask = *
temp = **
mask = *
mask = *
temp = ***
mask = *
mask = *
temp = ****
mask = *
mask = *
temp = *****
mask = *
mask = *
temp = ******
mask = *
mask = *
temp = *******
mask = *


as you can see inputString does equal the correct value and hidereveal equals the exact amount of necessary * but inputString.charAt(i) never adds anything to temp. and then it just keeps looping asterisks.
 
What's the letter you gave to Magic?
 
I think I see the bug. I check my code...

Code:
				System.out.println("mask = " + mask);
				if (hidereveal.charAt(i) == inputString.charAt(i))
				{
					temp += inputString.charAt(i);
					System.out.println("temp = " + temp);
					hit = true;
				}
				else
				{
					temp += mask;
					System.out.println("temp = " + temp);
					System.out.println("mask = " + mask);
				}
change to:
Code:
				System.out.println("mask = " + mask);
				if (letter == inputString.charAt(i))
				{
					temp += inputString.charAt(i);
					System.out.println("temp = " + temp);
					hit = true;
				}
				else
				{
					temp += mask;
					System.out.println("temp = " + temp);
					System.out.println("mask = " + mask);
				}

I deleted the "letter" bit. :( temp should then be correct. :)
 
damn man, id celebrate but im too exhausted :laugh: :toast:

ok, so now i just have to work on the timer. im guessing i should put some sort of for loop inside the nested else statement...
 
When Magic (recommend you change the name of that to something more descriptive, by the way) return false, redraw the timer. I'd use a for loop to create the dashes.
 
When Magic (recommend you change the name of that to something more descriptive, by the way) return false, redraw the timer. I'd use a for loop to create the dashes.

shouldnt i put a hit = false; after

temp += hidereveal.charAt(i);

sidenote: what exactly is letter doing in your magic method?
 
Last edited:
It depends on how creative you want to get with it.
 
not sure what is going on here but if you guess wrong on the very first guess it does not deduct 1. you have to guess right first and then no matter what your guess the second time it deducts 1.

am i putting the timer in the wrong spot??
hit = false; isnt necessary is it since we declare it false before the for loop runs?
 
if (!Magic(char))
{
// decrement timer
}

Don't do anything if it returns true.
 
The button click action performed.
 
right now i set it up to simply count down to 0 and go boom when ever the user enters a letter right or wrong. here is my code for that. now i need ot get actionPerformed to recognize the boolean hit results from compareSecret.

int guessedWrong = 5; // class level variable
JLabel timer = new JLabel(" ==- 5 -== "); // class level variable that represents the initial value of gussedWrong

Code:
public void actionPerformed(ActionEvent e)
	{
		if ("OK".equals(e.getActionCommand()))
		{
			if (tfInput.getText().length() > 0)
			{
			compareSecret(tfInput.getText().charAt(0));
			tfInput.setText(""); // clear for next letter
			maskedWord.setText(hidereveal);

			guessedWrong--; // subtract 1 from guessedWrong
			String wrongGuess = String.valueOf(guessedWrong); // turn the int gussedWrong into a String so it can be put into setText
			timer.setText(" ==- " + wrongGuess + " -== "); // place the value of guessedWrong into the JLabel timer. 
			if(guessedWrong == 0) // set condition for BOOM!
			{
				timer.setText("BOOM!"); // set "BOOM!"
			}
			}
			
		}

	}
 
Last edited:
I think (note the Boolean hit = )...
Code:
	public void actionPerformed(ActionEvent e)
	{
		if ("OK".equals(e.getActionCommand()))
		{
			if (tfInput.getText().length() > 0)
			{
				Boolean hit = compareSecret(tfInput.getText().charAt(0));
				tfInput.setText(""); // clear for next letter
				maskedWord.setText(hidereveal);
				
				if (!hit)
				{
					guessedWrong--; // subtract 1 from guessedWrong
					String wrongGuess = String.valueOf(guessedWrong); // turn the int gussedWrong into a String so it can be put into setText
					timer.setText(" ==- " + wrongGuess + " -== "); // place the value of guessedWrong into the JLabel timer. 
					if(guessedWrong == 0) // set condition for BOOM!
					{
						timer.setText("BOOM!"); // set "BOOM!"
					}
				}
			}
			
		}

	}
 
yup that did it.
 
Back
Top