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

Rhino's "I need Java help" thread

Easy Rhino

Linux Advocate
Staff member
Joined
Nov 13, 2006
Messages
15,449 (2.42/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 The Matrix
Code:
		String temp = "";
		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
		guessLetter = new JLabel(temp);

i dont know where i would be without your help. do you have a paypal account? i need to buy you a beer. :toast:
 

Kreij

Senior Monkey Moderator
Joined
Feb 6, 2007
Messages
13,817 (2.20/day)
Location
Cheeseland (Wisconsin, USA)
If you get it, let me know and then send me yours too.
I'll buy you both a beer.
Ford for being a great help to everyone and you for your diligence and patience when learning how to code. :toast:
 

Easy Rhino

Linux Advocate
Staff member
Joined
Nov 13, 2006
Messages
15,449 (2.42/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 The Matrix
If you get it, let me know and then send me yours too.
I'll buy you both a beer.
Ford for being a great help to everyone and you for your diligence and patience when learning how to code. :toast:

honestly i dont want to come off as a pain in the ass and i dont want anyone to think im taking their help for granted.
 

Kreij

Senior Monkey Moderator
Joined
Feb 6, 2007
Messages
13,817 (2.20/day)
Location
Cheeseland (Wisconsin, USA)
Don't flatter yourself. If we didn't want to help we wouldn't. :p :laugh:

I can only speak for myself as a coder, but perhaps Ford is the same.
I can't resist helping people if they need it. Just the way I am.
 

Easy Rhino

Linux Advocate
Staff member
Joined
Nov 13, 2006
Messages
15,449 (2.42/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 The Matrix
Don't flatter yourself. If we didn't want to help we wouldn't. :p :laugh:

oh i know!

now my next challenge is to break down inputString into an array and have it store the values of each character so that i can compare the input entered into JtextField tfInput with that of the secret word. i know how to create a string array but im not sure how to create an array that a user would input.
 

Easy Rhino

Linux Advocate
Staff member
Joined
Nov 13, 2006
Messages
15,449 (2.42/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 The Matrix
now i can do this...

Code:
import java.io.*;

public class ArrayDemo2 
{
	String inputString, secretWord;
	
	private BufferedReader keyboard = new BufferedReader(new InputStreamReader(System.in));
	
	public void wordInput() throws IOException
	{
		System.out.println("Enter Word: ");
		inputString = keyboard.readLine();

		String secretWord = (inputString);
			System.out.print(secretWord.charAt(5));
	}
	
	public static void main(String[] args) throws IOException
	{
		ArrayDemo2 x = new ArrayDemo2();
		x.wordInput();

	}//end of main
}//end of class

and it will output the letter from the 5th indices (it starts at 0 if you didnt know).
 

Kreij

Senior Monkey Moderator
Joined
Feb 6, 2007
Messages
13,817 (2.20/day)
Location
Cheeseland (Wisconsin, USA)
I'm not sure how you want to go about it, but you should not have to break the input string down into a character array. You can find out if a chatacter exists in the string by using the string.IndexOf() method.

This returns the first instance of the character in the string. To check if there are more of the same character (eg. "mississippi" lol), re-run the method starting at the index just after the last found occurance. Repeat until it returns no matches.

Each time you get an possitive match on an index, show the character in the "mask" string at the same index.
 

FordGT90Concept

"I go fast!1!11!1!"
Joined
Oct 13, 2008
Messages
26,259 (4.63/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.
Just do:

byte[] secret = inputString.getBytes();


You need to do the same with the user text:
byte[] user = keyboard.readLine().getBytes();


Compare the two:

Code:
private static byte[] CompareSecret(byte[] secret, byte[] user)
{
  if (secret.getLength() == user.getLength())
  {
    byte[] output = new byte[secret.getLength()];
    for (int i = 0; i < secret.getLength(); i++)
    {
      if (secret[i] == user[i])
        output[i] = secret[i];
      else
        output[i] = 42; // *
    }
    return output;
  }
  else
  {
    return null; // invalid
  }
}


Turn the result back into a string:

txtDisplay.setText(new String(CompareSecret(secret, user)));



@Kreij:
Array.IndexOf is much slower than going through the entire array once.
 
Last edited:

Kreij

Senior Monkey Moderator
Joined
Feb 6, 2007
Messages
13,817 (2.20/day)
Location
Cheeseland (Wisconsin, USA)
Why go to that length, Ford, the string is already a character array by definition?
Why not just use the existing methods to pick out what you want?
Am I missing something?
 

FordGT90Concept

"I go fast!1!11!1!"
Joined
Oct 13, 2008
Messages
26,259 (4.63/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.
Strings are not composed of an array of char like in .NET. This is why you have to use String.charAt(i) rather than String. Strings are constant (note how .charAt has no "set" equivilent). If you break it down to an array of bytes, you can change any index at any time so long as it is inside the constraints of the array (as one would expect). Loop through that array once and you're done.

Additionally, String is UTF-16, not ASCII. new String(byte[]) automatically makes the conversion. Not that it really matters but the byte array is half the size of String equivilent (smaller memory footprint and faster to work with).
 

Kreij

Senior Monkey Moderator
Joined
Feb 6, 2007
Messages
13,817 (2.20/day)
Location
Cheeseland (Wisconsin, USA)
Strings are constant

#region Start Rant

Good Lord, does Sun hold millions of shares of keyboard repair stock?
No wonder the source code for a simple Java program lists code like an unabridged dictionary.

I understand the need for the cross-platform compatibility, but non-mutable strings?!! :wtf:

In ER's code, he imports all of the controls (Jpanel, JButton, etc.) individually as namespaces (packages?).
Why are they not all in one package? (or namespace or whatever)
It would be like having to do..
Code:
using System.Windows.Panel;
using System.Windows.Label;
using System.Windows.ComboBox;
using etc. etc. etc.

Arrrrgh.
Okay, I am officially going back to C#.
(of course, I will still help ER with his code :) as the languages are pretty close syntacticaly. )

# endregion
 

FordGT90Concept

"I go fast!1!11!1!"
Joined
Oct 13, 2008
Messages
26,259 (4.63/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.
That's why C# is murdering Java for Windows developement. JRT was just thrown together with little consideration given to common sense (you don't friggen "swing" to make a "window" or "awt" to handle the "console").

We need not mention how much faster and thorough .NET is as well.
 
Last edited:

Easy Rhino

Linux Advocate
Staff member
Joined
Nov 13, 2006
Messages
15,449 (2.42/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 The Matrix
Just do:

byte[] secret = inputString.getBytes();


You need to do the same with the user text:
byte[] user = keyboard.readLine().getBytes();


Compare the two:

Code:
private static byte[] CompareSecret(byte[] secret, byte[] user)
{
  if (secret.getLength() == user.getLength())
  {
    byte[] output = new byte[secret.getLength()];
    for (int i = 0; i < secret.getLength(); i++)
    {
      if (secret[i] == user[i])
        output[i] = secret[i];
      else
        output[i] = 42; // *
    }
    return output;
  }
  else
  {
    return null; // invalid
  }
}


Turn the result back into a string:

txtDisplay.setText(new String(CompareSecret(secret, user)));

thanks. 2 things. with the data type byte you use .length not getLength()

also, im adding that method to the main method for execution and i am getting...

the method CompareSecret(byte[] byte[]) in the type TimeBomb is not applicable for the argument (byte, byte)

i think it may be how i have my code arranged because it also says that for the settext method.

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.JPanel;
import javax.swing.JTextField;

import java.io.BufferedReader;
import java.io.InputStreamReader;
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;
	
	BufferedReader keyboard = new BufferedReader(new InputStreamReader(System.in));
	
	JTextField tfInput = new JTextField(2);
	JLabel lblMessage = new JLabel("Enter A Letter: ");
	JLabel guessLetter = new JLabel();
	JButton btnOK = new JButton("OK");
	JLabel timer = new JLabel("*-----");
	
	public TimeBomb() throws IOException//constructor for GUI
	{
		super("Time Bomb Game");
		
		//secret word
		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
			guessLetter = new JLabel(temp);

		byte[] secret = inputString.getBytes();
		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);
		guessLetter.setFont(f1);
		btnOK.setFont(f1);
		timer.setFont(f2);
		
		//gui properties
		getContentPane().setLayout(new BorderLayout(1,1));

		JPanel northPanel = new JPanel();
		northPanel.add(lblMessage);
		getContentPane().add(northPanel, "North");
		
		JPanel westPanel = new JPanel();
		westPanel.add(timer);
		getContentPane().add(westPanel, "West");
		
		JPanel centerPanel = new JPanel();
		centerPanel.add(tfInput);
		getContentPane().add(centerPanel, "Center");
		
		JPanel eastPanel = new JPanel();
		eastPanel.add(btnOK);
		getContentPane().add(eastPanel, "East");
		btnOK.addActionListener(this);
		
		JPanel southPanel = new JPanel();
		southPanel.add(guessLetter);
		getContentPane().add(southPanel, "South");
		
		setSize(310, 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
		{
				guessLetter.setText(tfInput.getText());
		}
	}
	
	public static void main(String[] args) throws IOException
	{
		TimeBomb x = new TimeBomb();
		x.CompareSecret(secret, user);
	}
}//end TimeBomb class
 

FordGT90Concept

"I go fast!1!11!1!"
Joined
Oct 13, 2008
Messages
26,259 (4.63/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.
thanks. 2 things. with the data type byte you use .length not getLength()
It's been a long time since I coded Java so take everything I say as pseudocode. XD


the method CompareSecret(byte[] byte[]) in the type TimeBomb is not applicable for the argument (byte, byte)
Make sure you are handing CompareSecret arrays of bytes not just a single byte as defined here:
Code:
	public static byte user, secret;
 

Easy Rhino

Linux Advocate
Staff member
Joined
Nov 13, 2006
Messages
15,449 (2.42/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 The Matrix
It's been a long time since I coded Java so take everything I say as pseudocode. XD

:laugh:
you --> :slap: <-- me

Make sure you are handing CompareSecret arrays of bytes not just a single byte as defined here:
Code:
	public static byte user, secret;

yea that fixed it. when i run the program i enter the word and i have to hit enter twice in the console. im guessing it is because im simply not done tweaking the program. however, the user and secret byte arrays say they are never read locally. i have them in the TimeBomb constructor. also, im a little confused with how to properly get the user guessed letter from the GUI onto the GUI if it is correct, and if it isnt to not display but instead take a tick off the timebomb counter.



for the first, i think i need to rework my ActionPerformed(ActionEvent e) method. with this line...

Code:
txtDisplay.setText(new String(CompareSecret(secret, user)));

would i make txtDisplay a JFrame? obviously the compareSecret method is doing all the actual work in comparing the arrays but outputting the data has me perplexed. i know the simple setText and getText methods work in this instance but im not sure how. i cant add a new JPanel to this GUI setup so I have to somehow replace guessLetter with txtDisplay.
 

FordGT90Concept

"I go fast!1!11!1!"
Joined
Oct 13, 2008
Messages
26,259 (4.63/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.
JLabel or a readonly JTextField work well for displaying strings. The way you have it written (using new String) will work to turn the bytes back into text.
 

Easy Rhino

Linux Advocate
Staff member
Joined
Nov 13, 2006
Messages
15,449 (2.42/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 The Matrix
JLabel or a readonly JTextField work well for displaying strings. The way you have it written (using new String) will work to turn the bytes back into text.

yes, but get the problem is you cant create any more JPanels. you can only have a north, south, east, center and west. i may have to look into this.
 

FordGT90Concept

"I go fast!1!11!1!"
Joined
Oct 13, 2008
Messages
26,259 (4.63/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.
There's different kinds of Layout types that can put in a JFrame allowing you to add multiple controls to it. I used GridBagLayout a lot, for example.
 

Easy Rhino

Linux Advocate
Staff member
Joined
Nov 13, 2006
Messages
15,449 (2.42/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 The Matrix
There's different kinds of Layout types that can put in a JFrame allowing you to add multiple controls to it. I used GridBagLayout a lot, for example.

yup i gave that a try. you think it will be easier to work with?
 

FordGT90Concept

"I go fast!1!11!1!"
Joined
Oct 13, 2008
Messages
26,259 (4.63/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.

Easy Rhino

Linux Advocate
Staff member
Joined
Nov 13, 2006
Messages
15,449 (2.42/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 The Matrix
hrm, my prof was able to run the program using BorderLayout. when he guessed a letter correctly that letter replaces the mask * so he didnt need to use any other layout. hrm...
 

FordGT90Concept

"I go fast!1!11!1!"
Joined
Oct 13, 2008
Messages
26,259 (4.63/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.
Then just replace the text in the field that is currently displaying the mask.
 

Easy Rhino

Linux Advocate
Staff member
Joined
Nov 13, 2006
Messages
15,449 (2.42/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 The Matrix
Then just replace the text in the field that is currently displaying the mask.

i figured out that i could add as many things to panes as necessary so i wont have to change the entire gui layout. yay me.

so you are saying that i should be able to take the tfInput that the user enters into the textfield and if it is correct it should simply replace the mask of that letter?
 

FordGT90Concept

"I go fast!1!11!1!"
Joined
Oct 13, 2008
Messages
26,259 (4.63/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 can't advise without knowing what I'm looking at it. Screenshot, please?
 

Easy Rhino

Linux Advocate
Staff member
Joined
Nov 13, 2006
Messages
15,449 (2.42/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 The Matrix
I can't advise without knowing what I'm looking at it. Screenshot, please?

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.JPanel;
import javax.swing.JTextField;

import java.io.BufferedReader;
import java.io.InputStreamReader;
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;
	
	BufferedReader keyboard = new BufferedReader(new InputStreamReader(System.in));
	
	JTextField tfInput = new JTextField(2);
	JLabel lblMessage = new JLabel("Guess A Letter: ");
	JLabel guessLetter = new JLabel();
	JButton btnOK = new JButton("OK");
	JLabel timer = new JLabel("*-----");
	
	public TimeBomb() throws IOException//constructor for GUI
	{
		super("Time Bomb Game");
		
		//secret word
		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
			guessLetter = 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);
		guessLetter.setFont(f1);
		btnOK.setFont(f1);
		timer.setFont(f2);
		
		//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(guessLetter);
		getContentPane().add(centerPanel, "Center");
		
		JPanel southPanel = new JPanel();
		southPanel.add(timer);
		getContentPane().add(southPanel, "South");
		
		setSize(310, 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
		{
			guessLetter.setText(tfInput.getText());
		//	txtDisplay.setText(new String(CompareSecret(secret, user)));
		}
	}
	
	public static void main(String[] args) throws IOException
	{
		TimeBomb x = new TimeBomb();
		x.CompareSecret(secret, user);
	}
}//end TimeBomb class



 
Top