• Welcome to TechPowerUp Forums, Guest! Please check out our forum guidelines for info related to our community.

Rhino's "I need Java help" thread

FordGT90Concept

"I go fast!1!11!1!"
Joined
Oct 13, 2008
Messages
26,263 (4.41/day)
Location
IA, USA
System Name BY-2021
Processor AMD Ryzen 7 5800X (65w eco profile)
Motherboard MSI B550 Gaming Plus
Cooling Scythe Mugen (rev 5)
Memory 2 x Kingston HyperX DDR4-3200 32 GiB
Video Card(s) AMD Radeon RX 7900 XT
Storage Samsung 980 Pro, Seagate Exos X20 TB 7200 RPM
Display(s) Nixeus NX-EDG274K (3840x2160@144 DP) + Samsung SyncMaster 906BW (1440x900@60 HDMI-DVI)
Case Coolermaster HAF 932 w/ USB 3.0 5.25" bay + USB 3.2 (A+C) 3.5" bay
Audio Device(s) Realtek ALC1150, Micca OriGen+
Power Supply Enermax Platimax 850w
Mouse Nixeus REVEL-X
Keyboard Tesoro Excalibur
Software Windows 10 Home 64-bit
Benchmark Scores Faster than the tortoise; slower than the hare.
Where it was ****** and turned to s, it should show "s*****" or whatever is correct. Unless you are using that space to show what was previously used?

What's the one on the bottom for ( * _ _ _ _ _ )?
 

Easy Rhino

Linux Advocate
Staff member
Joined
Nov 13, 2006
Messages
15,643 (2.35/day)
Location
Mid-Atlantic
System Name Desktop
Processor i5 13600KF
Motherboard AsRock B760M Steel Legend Wifi
Cooling Noctua NH-U9S
Memory 4x 16 Gb Gskill S5 DDR5 @6000
Video Card(s) Gigabyte Gaming OC 6750 XT 12GB
Storage WD_BLACK 4TB SN850x
Display(s) Gigabye M32U
Case Corsair Carbide 400C
Audio Device(s) On Board
Power Supply EVGA Supernova 650 P2
Mouse MX Master 3s
Keyboard Logitech G915 Wireless Clicky
Software Fedora KDE Spin
Where it was ****** and turned to s, it should show "s*****" or whatever is correct. Unless you are using that space to show what was previously used?

What's the one on the bottom for ( * _ _ _ _ _ )?

yea when it is ****** and i enter s in the txtfield it should read s*****

and *----- is the timer. so if you get 1 wrong it will be *---- and two wrong *--- until you get to just * and then it will have to say BOOM!
 

FordGT90Concept

"I go fast!1!11!1!"
Joined
Oct 13, 2008
Messages
26,263 (4.41/day)
Location
IA, USA
System Name BY-2021
Processor AMD Ryzen 7 5800X (65w eco profile)
Motherboard MSI B550 Gaming Plus
Cooling Scythe Mugen (rev 5)
Memory 2 x Kingston HyperX DDR4-3200 32 GiB
Video Card(s) AMD Radeon RX 7900 XT
Storage Samsung 980 Pro, Seagate Exos X20 TB 7200 RPM
Display(s) Nixeus NX-EDG274K (3840x2160@144 DP) + Samsung SyncMaster 906BW (1440x900@60 HDMI-DVI)
Case Coolermaster HAF 932 w/ USB 3.0 5.25" bay + USB 3.2 (A+C) 3.5" bay
Audio Device(s) Realtek ALC1150, Micca OriGen+
Power Supply Enermax Platimax 850w
Mouse Nixeus REVEL-X
Keyboard Tesoro Excalibur
Software Windows 10 Home 64-bit
Benchmark Scores Faster than the tortoise; slower than the hare.
Ah, so use CompareSecret before you display it. That's exactly what it is for.
 

Easy Rhino

Linux Advocate
Staff member
Joined
Nov 13, 2006
Messages
15,643 (2.35/day)
Location
Mid-Atlantic
System Name Desktop
Processor i5 13600KF
Motherboard AsRock B760M Steel Legend Wifi
Cooling Noctua NH-U9S
Memory 4x 16 Gb Gskill S5 DDR5 @6000
Video Card(s) Gigabyte Gaming OC 6750 XT 12GB
Storage WD_BLACK 4TB SN850x
Display(s) Gigabye M32U
Case Corsair Carbide 400C
Audio Device(s) On Board
Power Supply EVGA Supernova 650 P2
Mouse MX Master 3s
Keyboard Logitech G915 Wireless Clicky
Software Fedora KDE Spin
Ah, so use CompareSecret before you display it. That's exactly what it is for.

well i understand comparesecret does all the work but i dont understand how to use it before i display it. for instance, in my code it says that both

Code:
byte[] secret = inputString.getBytes();//convert secret word String into byte array
		byte[] user = keyboard.readLine().getBytes();

are never read locally. could it be that i am not utilizing these two arrays correctly?
 

FordGT90Concept

"I go fast!1!11!1!"
Joined
Oct 13, 2008
Messages
26,263 (4.41/day)
Location
IA, USA
System Name BY-2021
Processor AMD Ryzen 7 5800X (65w eco profile)
Motherboard MSI B550 Gaming Plus
Cooling Scythe Mugen (rev 5)
Memory 2 x Kingston HyperX DDR4-3200 32 GiB
Video Card(s) AMD Radeon RX 7900 XT
Storage Samsung 980 Pro, Seagate Exos X20 TB 7200 RPM
Display(s) Nixeus NX-EDG274K (3840x2160@144 DP) + Samsung SyncMaster 906BW (1440x900@60 HDMI-DVI)
Case Coolermaster HAF 932 w/ USB 3.0 5.25" bay + USB 3.2 (A+C) 3.5" bay
Audio Device(s) Realtek ALC1150, Micca OriGen+
Power Supply Enermax Platimax 850w
Mouse Nixeus REVEL-X
Keyboard Tesoro Excalibur
Software Windows 10 Home 64-bit
Benchmark Scores Faster than the tortoise; slower than the hare.
Are you ever using CompareSecrets? keyboard.readLine() would only work in the console. If you are inputting text through that button, the button should get the bytes, run it through CompareSecrets, and update the label.
 

Easy Rhino

Linux Advocate
Staff member
Joined
Nov 13, 2006
Messages
15,643 (2.35/day)
Location
Mid-Atlantic
System Name Desktop
Processor i5 13600KF
Motherboard AsRock B760M Steel Legend Wifi
Cooling Noctua NH-U9S
Memory 4x 16 Gb Gskill S5 DDR5 @6000
Video Card(s) Gigabyte Gaming OC 6750 XT 12GB
Storage WD_BLACK 4TB SN850x
Display(s) Gigabye M32U
Case Corsair Carbide 400C
Audio Device(s) On Board
Power Supply EVGA Supernova 650 P2
Mouse MX Master 3s
Keyboard Logitech G915 Wireless Clicky
Software Fedora KDE Spin
Are you ever using CompareSecrets? keyboard.readLine() would only work in the console. If you are inputting text through that button, the button should get the bytes, run it through CompareSecrets, and update the label.

well i think so. lol. check out my code!
 

Easy Rhino

Linux Advocate
Staff member
Joined
Nov 13, 2006
Messages
15,643 (2.35/day)
Location
Mid-Atlantic
System Name Desktop
Processor i5 13600KF
Motherboard AsRock B760M Steel Legend Wifi
Cooling Noctua NH-U9S
Memory 4x 16 Gb Gskill S5 DDR5 @6000
Video Card(s) Gigabyte Gaming OC 6750 XT 12GB
Storage WD_BLACK 4TB SN850x
Display(s) Gigabye M32U
Case Corsair Carbide 400C
Audio Device(s) On Board
Power Supply EVGA Supernova 650 P2
Mouse MX Master 3s
Keyboard Logitech G915 Wireless Clicky
Software Fedora KDE Spin
i spent time reworking my code. one of the project rules is that you cant show the secret word which sort of means you cant use the console. so im using JOptionPane. not a big change but i will have to rework some code. regardless it will be a lot cleaner in the end. also, i think i have located the problem when the GUI actually executes after i enter the secret word i get

Exception in thread "main" java.lang.NullPointerException
at TimeBomb.CompareSecret(TimeBomb.java:86)
at TimeBomb.main(TimeBomb.java:117)

which as you can see points to the CompareSecret method.

currently i have the array that breaks inputString down into a byte array. now i need a for it to read the input from the user textfield and check it against the inputString. i know that is what the compareSecret is for but this is clearly where my problem is in discovering the solution. isnt the compareSecret method actually just checking to see if the words are the same length? instead of matching characters it expects a full word input? i need to somehow break the inputString down into an array that can be checked against the letter the user inputs into the textfield.


NOTE: the lines that are commented are there to remind me of the changes.

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;

import java.io.IOException;

public class TimeBomb extends JFrame implements ActionListener
{
	public String inputString;
	public String inputMask = "*";//mask
	public String temp = "";
	public static byte[] user, secret;
	
	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("*-----");
	
	//BufferedReader keyboard = new BufferedReader(new InputStreamReader(System.in));	
	
	public TimeBomb() //constructor for GUI
	{
		super("Time Bomb Game");
		
		//secret word
		inputString = JOptionPane.showInputDialog("Enter Secret Word: ");
	//	inputString = keyboard.readLine();
	//	System.out.println("Enter Secret Word: ");//enter secret word into console
	//	inputString = keyboard.readLine();//store 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);

		byte[] secret = inputString.getBytes();//convert secret word String into byte array
	//	byte[] user = keyboard.readLine().getBytes();
		
		//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 byte[] CompareSecret(byte[] secret, final byte[] user)
	{
	  if (secret.length == user.length)
	  {
	    byte[] output = new byte[secret.length];
	    for (int i = 0; i < secret.length; i++)
	    {
	      if (secret[i] == user[i])
	        output[i] = secret[i];
	      else
	        output[i] = 42; // *
	    }
	    return output;
	  }
	  else
	  {
	    return null; // invalid
	  }
	}

	
	public void actionPerformed(ActionEvent e)
	{
		if ("OK".equals(e.getActionCommand()))//OK button gets entered letter and puts it in the south panel
		{
			guessedLetter.setText(tfInput.getText());
		//	txtDisplay.setText(new String(CompareSecret(secret, user)));
		}
	}
	
	public static void main(String[] args) 
	{
		TimeBomb x = new TimeBomb();
		x.CompareSecret(secret, user);
	}
}//end TimeBomb class
 
Last edited:

FordGT90Concept

"I go fast!1!11!1!"
Joined
Oct 13, 2008
Messages
26,263 (4.41/day)
Location
IA, USA
System Name BY-2021
Processor AMD Ryzen 7 5800X (65w eco profile)
Motherboard MSI B550 Gaming Plus
Cooling Scythe Mugen (rev 5)
Memory 2 x Kingston HyperX DDR4-3200 32 GiB
Video Card(s) AMD Radeon RX 7900 XT
Storage Samsung 980 Pro, Seagate Exos X20 TB 7200 RPM
Display(s) Nixeus NX-EDG274K (3840x2160@144 DP) + Samsung SyncMaster 906BW (1440x900@60 HDMI-DVI)
Case Coolermaster HAF 932 w/ USB 3.0 5.25" bay + USB 3.2 (A+C) 3.5" bay
Audio Device(s) Realtek ALC1150, Micca OriGen+
Power Supply Enermax Platimax 850w
Mouse Nixeus REVEL-X
Keyboard Tesoro Excalibur
Software Windows 10 Home 64-bit
Benchmark Scores Faster than the tortoise; slower than the hare.
Exception in thread "main" java.lang.NullPointerException
at TimeBomb.CompareSecret(TimeBomb.java:86)
at TimeBomb.main(TimeBomb.java:117)
The error numbers don't line up with the code you pasted so I can't help there.


currently i have the array that breaks inputString down into a byte array. now i need a for it to read the input from the user textfield and check it against the inputString. i know that is what the compareSecret is for but this is clearly where my problem is in discovering the solution. isnt the compareSecret method actually just checking to see if the words are the same length? instead of matching characters it expects a full word input? i need to somehow break the inputString down into an array that can be checked against the letter the user inputs into the textfield.
CompareSecret makes sure the two match in length. It should/would error if they weren't. If they match, it loops through them both comparing letter by letter. If it is a match, it outputs the letter. If it doesn't match, it outputs an astrisk (*).

Wait a minute, they only guess one letter at a time?
 

Easy Rhino

Linux Advocate
Staff member
Joined
Nov 13, 2006
Messages
15,643 (2.35/day)
Location
Mid-Atlantic
System Name Desktop
Processor i5 13600KF
Motherboard AsRock B760M Steel Legend Wifi
Cooling Noctua NH-U9S
Memory 4x 16 Gb Gskill S5 DDR5 @6000
Video Card(s) Gigabyte Gaming OC 6750 XT 12GB
Storage WD_BLACK 4TB SN850x
Display(s) Gigabye M32U
Case Corsair Carbide 400C
Audio Device(s) On Board
Power Supply EVGA Supernova 650 P2
Mouse MX Master 3s
Keyboard Logitech G915 Wireless Clicky
Software Fedora KDE Spin
The error numbers don't line up with the code you pasted so I can't help there.



CompareSecret makes sure the two match in length. It should/would error if they weren't. If they match, it loops through them both comparing letter by letter. If it is a match, it outputs the letter. If it doesn't match, it outputs an astrisk (*).

Wait a minute, they only guess one letter at a time?

yes. sorry that is my fault. ive continued to rework the code and i think i am getting there. i break down inputString into a byte array now i need to compare the letter entered into the textfield with the inputString byte array to see if the letter matches any in the inputString array. if there is a match then print the letter onthe GUI in the correct spot in the masked array. if it is wrong deduct one dash from the timer.

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 TimeBomb extends JFrame implements ActionListener
{
	public String inputString;
	public String inputMask = "*";//mask
	public String temp = "";
	public static byte[] user, secret;
	
	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 TimeBomb() //constructor for GUI
	{
		super("Time Bomb Game");
		
		//secret word
		inputString = JOptionPane.showInputDialog("Enter 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);

		byte[] secret = inputString.getBytes();//convert secret word String into byte array
		
		//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 byte[] CompareSecret(byte[] secret, final byte[] user)
	{
	  if (secret.length == user.length)
	  {
	    byte[] output = new byte[secret.length];
	    for (int i = 0; i < secret.length; i++)
	    {
	      if (secret[i] == user[i])
	        output[i] = secret[i];
	      else
	        output[i] = 42; // *
	    }
	    return output;
	  }
	  else
	  {
	    return null; // invalid
	  }
	}

	
	public void actionPerformed(ActionEvent e)
	{
		if ("OK".equals(e.getActionCommand()))//OK button gets entered letter and puts it in the south panel
		{
			CompareSecret(secret, user);
		}
	}
	
	public static void main(String[] args) 
	{
		new TimeBomb();
	}
}//end TimeBomb class
 

FordGT90Concept

"I go fast!1!11!1!"
Joined
Oct 13, 2008
Messages
26,263 (4.41/day)
Location
IA, USA
System Name BY-2021
Processor AMD Ryzen 7 5800X (65w eco profile)
Motherboard MSI B550 Gaming Plus
Cooling Scythe Mugen (rev 5)
Memory 2 x Kingston HyperX DDR4-3200 32 GiB
Video Card(s) AMD Radeon RX 7900 XT
Storage Samsung 980 Pro, Seagate Exos X20 TB 7200 RPM
Display(s) Nixeus NX-EDG274K (3840x2160@144 DP) + Samsung SyncMaster 906BW (1440x900@60 HDMI-DVI)
Case Coolermaster HAF 932 w/ USB 3.0 5.25" bay + USB 3.2 (A+C) 3.5" bay
Audio Device(s) Realtek ALC1150, Micca OriGen+
Power Supply Enermax Platimax 850w
Mouse Nixeus REVEL-X
Keyboard Tesoro Excalibur
Software Windows 10 Home 64-bit
Benchmark Scores Faster than the tortoise; slower than the hare.
I made a console app quick to try what you are doing. Here's the magic maker:
Code:
        private static string _Secret = "";
        private static string _Reveal = "";
        private const char MASK = '*';

        // Make sure _Reveal is all MASK before calling Magic (e.g. _Secret = "blamo", _Reveal = "*****")

        private static bool Magic(char letter)
        {
            bool hit = false; // True for a hit, false for no hit.

            string temp = ""; // We are going to be reading from _Reveal so we can't write to it.
            for (int i = 0; i < _Secret.Length; i++) // Loop through _Secret and _Reveal --length must match
            {
                if (_Reveal[i] == MASK) // Does this character in _Reveal match the mask?
                {
                    if (letter == _Secret[i])  // Yes, but does the letter match?
                    {
                        temp += _Secret[i];  // Yes, add the new letter to the soon-to-be mask.
                        hit = true;  // Don't reduce the clock on the user for this one.
                    }
                    else
                        temp += MASK; // They guessed wrong for this character.
                }
                else
                    temp += _Reveal[i]; // This one is already correct.
            }
            _Reveal = temp; // copy our updated _Reveal to the _Reveal variable

            return hit;  // Return with whether or not the user needs to be slapped
        }
This function supercedes CompareSecret.
 
Last edited:

Easy Rhino

Linux Advocate
Staff member
Joined
Nov 13, 2006
Messages
15,643 (2.35/day)
Location
Mid-Atlantic
System Name Desktop
Processor i5 13600KF
Motherboard AsRock B760M Steel Legend Wifi
Cooling Noctua NH-U9S
Memory 4x 16 Gb Gskill S5 DDR5 @6000
Video Card(s) Gigabyte Gaming OC 6750 XT 12GB
Storage WD_BLACK 4TB SN850x
Display(s) Gigabye M32U
Case Corsair Carbide 400C
Audio Device(s) On Board
Power Supply EVGA Supernova 650 P2
Mouse MX Master 3s
Keyboard Logitech G915 Wireless Clicky
Software Fedora KDE Spin
I made a console app quick to try what you are doing. Here's the magic maker:
Code:
        private static string _Secret = "";
        private static string _Reveal = "";
        private const char MASK = '*';

        // Make sure _Reveal is all MASK before calling Magic (e.g. _Secret = "blamo", _Reveal = "*****")

        private static bool Magic(char letter)
        {
            bool hit = false; // True for a hit, false for no hit.

            string temp = ""; // We are going to be reading from _Reveal so we can't write to it.
            for (int i = 0; i < _Secret.Length; i++) // Loop through _Secret and _Reveal --length must match
            {
                if (_Reveal[i] == MASK) // Does this character in _Reveal match the mask?
                {
                    if (letter == _Secret[i])  // Yes, but does the letter match?
                    {
                        temp += _Secret[i];  // Yes, add the new letter to the soon-to-be mask.
                        hit = true;  // Don't reduce the clock on the user for this one.
                    }
                    else
                        temp += MASK; // They guessed wrong for this character.
                }
                else
                    temp += _Reveal[i]; // This one is already correct.
            }
            _Reveal = temp; // copy our updated _Reveal to the _Reveal variable

            return hit;  // Return with whether or not the user needs to be slapped
        }
This function supercedes CompareSecret.

how do you get the input from the textfield tho?
 

Easy Rhino

Linux Advocate
Staff member
Joined
Nov 13, 2006
Messages
15,643 (2.35/day)
Location
Mid-Atlantic
System Name Desktop
Processor i5 13600KF
Motherboard AsRock B760M Steel Legend Wifi
Cooling Noctua NH-U9S
Memory 4x 16 Gb Gskill S5 DDR5 @6000
Video Card(s) Gigabyte Gaming OC 6750 XT 12GB
Storage WD_BLACK 4TB SN850x
Display(s) Gigabye M32U
Case Corsair Carbide 400C
Audio Device(s) On Board
Power Supply EVGA Supernova 650 P2
Mouse MX Master 3s
Keyboard Logitech G915 Wireless Clicky
Software Fedora KDE Spin
for _Secret and _Reveal it says the type of expression must be an array but it resolved to a string. and for MASK it says it cannot make a static reference to a nonstatic field. didnt you say something about replacing with a method? i think you edited your post and erased that comment.

also, im guessing i do not want to create a byte array for inputString? i think i am more confused now so i am going to bed.

but first, all i really need to do is break inputString down into an array that can be used to check against the letter guessed by the user in JTextField.
do i do this with a character array instead of a byte array?

if the letter matches any indices of the inputString array then replace the mask in that array indices with the letter.

if not then do nothing BUT reduced one from the counter.
 
Last edited:

FordGT90Concept

"I go fast!1!11!1!"
Joined
Oct 13, 2008
Messages
26,263 (4.41/day)
Location
IA, USA
System Name BY-2021
Processor AMD Ryzen 7 5800X (65w eco profile)
Motherboard MSI B550 Gaming Plus
Cooling Scythe Mugen (rev 5)
Memory 2 x Kingston HyperX DDR4-3200 32 GiB
Video Card(s) AMD Radeon RX 7900 XT
Storage Samsung 980 Pro, Seagate Exos X20 TB 7200 RPM
Display(s) Nixeus NX-EDG274K (3840x2160@144 DP) + Samsung SyncMaster 906BW (1440x900@60 HDMI-DVI)
Case Coolermaster HAF 932 w/ USB 3.0 5.25" bay + USB 3.2 (A+C) 3.5" bay
Audio Device(s) Realtek ALC1150, Micca OriGen+
Power Supply Enermax Platimax 850w
Mouse Nixeus REVEL-X
Keyboard Tesoro Excalibur
Software Windows 10 Home 64-bit
Benchmark Scores Faster than the tortoise; slower than the hare.
how do you get the input from the textfield tho?
TextField.getText();


for _Secret and _Reveal it says the type of expression must be an array but it resolved to a string. and for MASK it says it cannot make a static reference to a nonstatic field. didnt you say something about replacing with a method? i think you edited your post and erased that comment.

You'll need to use .getChar(i) instead of .

MASK should be constant but it doesn't have to be. Const naturally means static. It is only static in my example because I did the whole thing in a console.
 

Easy Rhino

Linux Advocate
Staff member
Joined
Nov 13, 2006
Messages
15,643 (2.35/day)
Location
Mid-Atlantic
System Name Desktop
Processor i5 13600KF
Motherboard AsRock B760M Steel Legend Wifi
Cooling Noctua NH-U9S
Memory 4x 16 Gb Gskill S5 DDR5 @6000
Video Card(s) Gigabyte Gaming OC 6750 XT 12GB
Storage WD_BLACK 4TB SN850x
Display(s) Gigabye M32U
Case Corsair Carbide 400C
Audio Device(s) On Board
Power Supply EVGA Supernova 650 P2
Mouse MX Master 3s
Keyboard Logitech G915 Wireless Clicky
Software Fedora KDE Spin
TextField.getText();



You'll need to use .getChar(i) instead of .

MASK should be constant but it doesn't have to be. Const naturally means static. It is only static in my example because I did the whole thing in a console.


i think you mean charAt(i) because .getChars(i) doesnt work in this instance.

so then i need to create a character array from the user word rather than a byte array?

this info seems to be what i am aiming at

http://java.sun.com/docs/books/tutorial/java/data/strings.html

ok i think ive worked through the code now i just need to have it actually execute when i hit the OK button.
 
Last edited:

Easy Rhino

Linux Advocate
Staff member
Joined
Nov 13, 2006
Messages
15,643 (2.35/day)
Location
Mid-Atlantic
System Name Desktop
Processor i5 13600KF
Motherboard AsRock B760M Steel Legend Wifi
Cooling Noctua NH-U9S
Memory 4x 16 Gb Gskill S5 DDR5 @6000
Video Card(s) Gigabyte Gaming OC 6750 XT 12GB
Storage WD_BLACK 4TB SN850x
Display(s) Gigabye M32U
Case Corsair Carbide 400C
Audio Device(s) On Board
Power Supply EVGA Supernova 650 P2
Mouse MX Master 3s
Keyboard Logitech G915 Wireless Clicky
Software Fedora KDE Spin
aha! ok i think ive written some good code here.

Code:
		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);

		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
		}

                textfieldString = tfInput.getText();
		int len2 = textfieldString.length();
		char[] letterArray = new char[len2];
		
		for(int j = 0; j < len2; j++)
		{
			letterArray[j] = textfieldString.charAt(j);
		}

as yuo can see it turns inputString into a char array! and it turns the letter enetered into the textfield into an array!!!!!! now i need to compare these two!

btw: you do know that you dont have to guess the letters in order. meaning if the secret word is awesome you can guess 's' and it will put s in the proper spot. if you guess 'e' it will reveal BOTH 'e's
 
Last edited:

FordGT90Concept

"I go fast!1!11!1!"
Joined
Oct 13, 2008
Messages
26,263 (4.41/day)
Location
IA, USA
System Name BY-2021
Processor AMD Ryzen 7 5800X (65w eco profile)
Motherboard MSI B550 Gaming Plus
Cooling Scythe Mugen (rev 5)
Memory 2 x Kingston HyperX DDR4-3200 32 GiB
Video Card(s) AMD Radeon RX 7900 XT
Storage Samsung 980 Pro, Seagate Exos X20 TB 7200 RPM
Display(s) Nixeus NX-EDG274K (3840x2160@144 DP) + Samsung SyncMaster 906BW (1440x900@60 HDMI-DVI)
Case Coolermaster HAF 932 w/ USB 3.0 5.25" bay + USB 3.2 (A+C) 3.5" bay
Audio Device(s) Realtek ALC1150, Micca OriGen+
Power Supply Enermax Platimax 850w
Mouse Nixeus REVEL-X
Keyboard Tesoro Excalibur
Software Windows 10 Home 64-bit
Benchmark Scores Faster than the tortoise; slower than the hare.
btw: you do know that you dont have to guess the letters in order. meaning if the secret word is awesome you can guess 's' and it will put s in the proper spot. if you guess 'e' it will reveal BOTH 'e's
That's what my "Magic" method did.
 

Easy Rhino

Linux Advocate
Staff member
Joined
Nov 13, 2006
Messages
15,643 (2.35/day)
Location
Mid-Atlantic
System Name Desktop
Processor i5 13600KF
Motherboard AsRock B760M Steel Legend Wifi
Cooling Noctua NH-U9S
Memory 4x 16 Gb Gskill S5 DDR5 @6000
Video Card(s) Gigabyte Gaming OC 6750 XT 12GB
Storage WD_BLACK 4TB SN850x
Display(s) Gigabye M32U
Case Corsair Carbide 400C
Audio Device(s) On Board
Power Supply EVGA Supernova 650 P2
Mouse MX Master 3s
Keyboard Logitech G915 Wireless Clicky
Software Fedora KDE Spin
That's what my "Magic" method did.

k i guess im just having issues manipulating it. also, when i press the OK button what should the command look like to actually get magic to work? cause i cant do any of the .getText methods on Magic.

blerg. we only spent 1 week on strings and 1 week on arrays. im not sure but i think i am confusing myself more. for instance, i dont know where _Secret, _Reveal or MASK actually come from.
 
Last edited:

FordGT90Concept

"I go fast!1!11!1!"
Joined
Oct 13, 2008
Messages
26,263 (4.41/day)
Location
IA, USA
System Name BY-2021
Processor AMD Ryzen 7 5800X (65w eco profile)
Motherboard MSI B550 Gaming Plus
Cooling Scythe Mugen (rev 5)
Memory 2 x Kingston HyperX DDR4-3200 32 GiB
Video Card(s) AMD Radeon RX 7900 XT
Storage Samsung 980 Pro, Seagate Exos X20 TB 7200 RPM
Display(s) Nixeus NX-EDG274K (3840x2160@144 DP) + Samsung SyncMaster 906BW (1440x900@60 HDMI-DVI)
Case Coolermaster HAF 932 w/ USB 3.0 5.25" bay + USB 3.2 (A+C) 3.5" bay
Audio Device(s) Realtek ALC1150, Micca OriGen+
Power Supply Enermax Platimax 850w
Mouse Nixeus REVEL-X
Keyboard Tesoro Excalibur
Software Windows 10 Home 64-bit
Benchmark Scores Faster than the tortoise; slower than the hare.
You may want to handle the whole thing as a bytes instead of string/char. C# handles strings much better than Java.

_Secret (string), _Reveal (string), and MASK (const char) are class level variables. _Secret is where you stick the secret word (e.g. "blamo"), _Reveal is where you stick your string to display (initially, "*****" -- must always match _Secret in length), and MASK is used to create _Reveal and check if that character is a candidate to be updated.

You'll need to make some variables class-level like I did (_Secret and _Reveal). You'll have to accept the character or byte with which to compare to secret/reveal. Then you'll need to return whether or not it was a hit or a miss.

Inside your button, you need to do a two things:
1) Run Magic (or whatever you name it--that's a bad name) using your .getText from guess letter. ***
2) Using the returned value from Magic, update your timer.


*** Make sure the length of .getText is > 0 then use .charAt(0) to get the character or .getBytes()[0] to get the byte of the character.
 

Easy Rhino

Linux Advocate
Staff member
Joined
Nov 13, 2006
Messages
15,643 (2.35/day)
Location
Mid-Atlantic
System Name Desktop
Processor i5 13600KF
Motherboard AsRock B760M Steel Legend Wifi
Cooling Noctua NH-U9S
Memory 4x 16 Gb Gskill S5 DDR5 @6000
Video Card(s) Gigabyte Gaming OC 6750 XT 12GB
Storage WD_BLACK 4TB SN850x
Display(s) Gigabye M32U
Case Corsair Carbide 400C
Audio Device(s) On Board
Power Supply EVGA Supernova 650 P2
Mouse MX Master 3s
Keyboard Logitech G915 Wireless Clicky
Software Fedora KDE Spin
You may want to handle the whole thing as a bytes instead of string/char. C# handles strings much better than Java.

You'll need to make some variables class-level like I did (secret and reveal). You'll have to accept the character or byte with which to compare to secret/reveal. Then you'll need to return whether or not it was a hit or a miss.

Inside your button, you need to do a two things:
1) Run Magic (or whatever you name it--that's a bad name) using your .getText from guess letter. ***
2) Using the returned value from Magic, update your timer.


*** Make sure the length of .getText is > 0 then use .charAt(0) to get the character or .getBytes()[0] to get the byte of the character.

well i have made the char arrays just fine from the inputString and the textField. look at this code...

Code:
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);

		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
		}
		
		textfieldString = tfInput.getText();
		int len2 = textfieldString.length();
		char[] letterArray = new char[len2];
		
		for(int j = 0; j < len2; j++)
		{
			letterArray[j] = textfieldString.charAt(j);
		}

you see it creates a mask for inputString and creates a char array for inputString and the letter entered in the textfield jTextField.

it should be fairly simple to scan the inputString array and if the letter entered in the JTextField matches any of the char in the array to replace the * from the mask with that letter. i should be able to use my already created class level variables to do this.
 

Easy Rhino

Linux Advocate
Staff member
Joined
Nov 13, 2006
Messages
15,643 (2.35/day)
Location
Mid-Atlantic
System Name Desktop
Processor i5 13600KF
Motherboard AsRock B760M Steel Legend Wifi
Cooling Noctua NH-U9S
Memory 4x 16 Gb Gskill S5 DDR5 @6000
Video Card(s) Gigabyte Gaming OC 6750 XT 12GB
Storage WD_BLACK 4TB SN850x
Display(s) Gigabye M32U
Case Corsair Carbide 400C
Audio Device(s) On Board
Power Supply EVGA Supernova 650 P2
Mouse MX Master 3s
Keyboard Logitech G915 Wireless Clicky
Software Fedora KDE Spin
to help me visual my problem i wrote up a little program that compares two Strings by breaking them down into char arrays and then printing if they match by using boolean. then i have an if statement setup to compare indices. i need to take that concept one step further and have an if statement that checks to see if the guessed letter is equal to any of the letters in the secret word.
i thought something like

if(secretWordArray == guessedLetter)

would do it but it only checks the first indices.

Code:
import java.util.Arrays;

import javax.swing.*;

public class anotherarraytest 
{
	String inputString1 = "";
	String inputString2 = "";
	int i,j;
	
	public anotherarraytest()
	{
		inputString1 = JOptionPane.showInputDialog("Enter Secret Word: ");
		
		int len1 = inputString1.length();
		char[] secretWordArray = new char[len1];
		for(int i = 0; i < len1; i++)
		{
			secretWordArray[i] = inputString1.charAt(i);
		}
		
		inputString2 = JOptionPane.showInputDialog("Guess Letter: ");
		
		int len2 = inputString2.length();
		char[] guessedLetter = new char[len2];
		for (int j = 0; j < len2; j++)
		{
            guessedLetter[j] = inputString2.charAt(j);
		}
		
		boolean result = Arrays.equals(secretWordArray, guessedLetter);
		System.out.println("Are they the same? " + result);
		System.out.println("Secret Word = " + inputString1);
		System.out.println("Guessed Letter = " + inputString2);
		
		if(secretWordArray[i] == guessedLetter[j])
		{
			System.out.println("You Got A Match! ");
		}
		else
		{
			System.out.println("No Match! ");
		}
		return;
    }

	public static void main(String[] args)
	{
		new anotherarraytest();
	}
		
}
 

FordGT90Concept

"I go fast!1!11!1!"
Joined
Oct 13, 2008
Messages
26,263 (4.41/day)
Location
IA, USA
System Name BY-2021
Processor AMD Ryzen 7 5800X (65w eco profile)
Motherboard MSI B550 Gaming Plus
Cooling Scythe Mugen (rev 5)
Memory 2 x Kingston HyperX DDR4-3200 32 GiB
Video Card(s) AMD Radeon RX 7900 XT
Storage Samsung 980 Pro, Seagate Exos X20 TB 7200 RPM
Display(s) Nixeus NX-EDG274K (3840x2160@144 DP) + Samsung SyncMaster 906BW (1440x900@60 HDMI-DVI)
Case Coolermaster HAF 932 w/ USB 3.0 5.25" bay + USB 3.2 (A+C) 3.5" bay
Audio Device(s) Realtek ALC1150, Micca OriGen+
Power Supply Enermax Platimax 850w
Mouse Nixeus REVEL-X
Keyboard Tesoro Excalibur
Software Windows 10 Home 64-bit
Benchmark Scores Faster than the tortoise; slower than the hare.
You have to put that conditional in a loop so it increments i for each character in the string.

Code:
for (int i = 0; i < secretWordArray.length(); i++)
{
	if(secretWordArray[i] == guessedLetter[j])
	{
		System.out.println("You Got A Match! ");
	}
	else
	{
		System.out.println("No Match! ");
	}
}
 

Easy Rhino

Linux Advocate
Staff member
Joined
Nov 13, 2006
Messages
15,643 (2.35/day)
Location
Mid-Atlantic
System Name Desktop
Processor i5 13600KF
Motherboard AsRock B760M Steel Legend Wifi
Cooling Noctua NH-U9S
Memory 4x 16 Gb Gskill S5 DDR5 @6000
Video Card(s) Gigabyte Gaming OC 6750 XT 12GB
Storage WD_BLACK 4TB SN850x
Display(s) Gigabye M32U
Case Corsair Carbide 400C
Audio Device(s) On Board
Power Supply EVGA Supernova 650 P2
Mouse MX Master 3s
Keyboard Logitech G915 Wireless Clicky
Software Fedora KDE Spin
You have to put that conditional in a loop so it increments i for each character in the string.

Code:
for (int i = 0; i < secretWordArray.length(); i++)
{
	if(secretWordArray[i] == guessedLetter[j])
	{
		System.out.println("You Got A Match! ");
	}
	else
	{
		System.out.println("No Match! ");
	}
}

:rockout::rockout::rockout::rockout::rockout:

you cant do .length() to an array but i just changed it to inputString1.length() in the for loop and it worked!

and what is better i get a console readout of

Secret Word = secret
Guessed Letter = e
No Match!
You Got A Match!
No Match!
No Match!
You Got A Match!
No Match!

so it is working great!

now i just need to get a mask to work somehow
 

FordGT90Concept

"I go fast!1!11!1!"
Joined
Oct 13, 2008
Messages
26,263 (4.41/day)
Location
IA, USA
System Name BY-2021
Processor AMD Ryzen 7 5800X (65w eco profile)
Motherboard MSI B550 Gaming Plus
Cooling Scythe Mugen (rev 5)
Memory 2 x Kingston HyperX DDR4-3200 32 GiB
Video Card(s) AMD Radeon RX 7900 XT
Storage Samsung 980 Pro, Seagate Exos X20 TB 7200 RPM
Display(s) Nixeus NX-EDG274K (3840x2160@144 DP) + Samsung SyncMaster 906BW (1440x900@60 HDMI-DVI)
Case Coolermaster HAF 932 w/ USB 3.0 5.25" bay + USB 3.2 (A+C) 3.5" bay
Audio Device(s) Realtek ALC1150, Micca OriGen+
Power Supply Enermax Platimax 850w
Mouse Nixeus REVEL-X
Keyboard Tesoro Excalibur
Software Windows 10 Home 64-bit
Benchmark Scores Faster than the tortoise; slower than the hare.
Again, that Magic code does it and does it right. You'll for sure need three strings (reveal, secret, and temp). Secret can't be altered and reveal needs to be available to compare it to Secret so you need to put the results of the comparison in a third variable which is later used to update Reveal.
 

Easy Rhino

Linux Advocate
Staff member
Joined
Nov 13, 2006
Messages
15,643 (2.35/day)
Location
Mid-Atlantic
System Name Desktop
Processor i5 13600KF
Motherboard AsRock B760M Steel Legend Wifi
Cooling Noctua NH-U9S
Memory 4x 16 Gb Gskill S5 DDR5 @6000
Video Card(s) Gigabyte Gaming OC 6750 XT 12GB
Storage WD_BLACK 4TB SN850x
Display(s) Gigabye M32U
Case Corsair Carbide 400C
Audio Device(s) On Board
Power Supply EVGA Supernova 650 P2
Mouse MX Master 3s
Keyboard Logitech G915 Wireless Clicky
Software Fedora KDE Spin
sloooooooooooooow progress. hey ford, when you get a second could you look through my code. after i enter the secret word in JOptionPane it pulls up the GUI but then errors on

if(secretWordArray == letterArray[j])

and says array index out of bounds exception. now i know that means it hit an indices out of its range but not sure why it happened. here is my code.
note: i havnt set anything in actionEvent yet cause i have no idea what to put there to get the inputed letter to actually go into the mask.

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;
	public char secretWordArray[], letterArray[];
	
	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 TimeBomb() //constructor for GUI
	{
		super("Time Bomb Game");
		
		inputString = JOptionPane.showInputDialog("Enter Secret Word: ");//user enters secret word
		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++)//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);
		
		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
		}
		
		for(int i = 0; i < inputString.length(); i++)
		{
			if(secretWordArray[i] == letterArray[j])
			{
				temp += textfieldString.charAt(i);
			}	
			else
			{
				temp += inputMask;
			}
		}
	}
	

	
	public void actionPerformed(ActionEvent e)
	{
		if ("OK".equals(e.getActionCommand()))//OK button gets entered letter and puts it in the south panel
		{
			
		}

	}
	
	public static void main(String[] args) 
	{
		new TimeBomb();
	}
}//end TimeBomb class
 

FordGT90Concept

"I go fast!1!11!1!"
Joined
Oct 13, 2008
Messages
26,263 (4.41/day)
Location
IA, USA
System Name BY-2021
Processor AMD Ryzen 7 5800X (65w eco profile)
Motherboard MSI B550 Gaming Plus
Cooling Scythe Mugen (rev 5)
Memory 2 x Kingston HyperX DDR4-3200 32 GiB
Video Card(s) AMD Radeon RX 7900 XT
Storage Samsung 980 Pro, Seagate Exos X20 TB 7200 RPM
Display(s) Nixeus NX-EDG274K (3840x2160@144 DP) + Samsung SyncMaster 906BW (1440x900@60 HDMI-DVI)
Case Coolermaster HAF 932 w/ USB 3.0 5.25" bay + USB 3.2 (A+C) 3.5" bay
Audio Device(s) Realtek ALC1150, Micca OriGen+
Power Supply Enermax Platimax 850w
Mouse Nixeus REVEL-X
Keyboard Tesoro Excalibur
Software Windows 10 Home 64-bit
Benchmark Scores Faster than the tortoise; slower than the hare.
letterArray has only one char (containing "2").
 
Top