I am trying to make a cheat code button for my game, and the displaying of it is all fine but I cannot correctly get the contents of this text field. This is the code for it:
cheats.addMouseListener(new MouseListener() {
#Override
public void mouseClicked(MouseEvent e) {
Class.console("DISPLAYED OPTIONPANE");
JOptionPane.showInputDialog(cheatCode, "Enter Code Here");
Class.console("GOT STRING" + cheatCode.getText());
if(cheatCode.getText().equals("testin")) {
Class.console("testout");
}
}
I'm pretty much a beginner at this, so help? I can post everything else if needed.
P.S. Class.console() is a thing in my driver class. It's basically a shortened version of System.out.println()
The showInputDialog() method returns the value that was entered by the user. You should capture it into a variable. For example, do this:
String userInputString = JOptionPane.showInputDialog("Enter Code Here");
The variable userInputString will be a string containing the value that you're looking for.
Related
basically its a wordle type game where we take input and if the first letter of the input is the same as the first letter of our word "sc" it should make the textView background color to green or yellow if not in the right position
String str=editText.getText().toString();
String first=str.substring(0,1);
String sec=str.substring(1,2);
String third=str.substring(2,3);
String fourth=str.substring(3,4);
if(count==0)
{
textView1.setText(first);
textView2.setText(sec);
textView3.setText(third);
textView4.setText(fourth);
if(first==sc.substring(0,1))
{
textView1.setBackgroundColor(Color.GREEN);
}
else if(first==sc.substring(1,2)|| first==sc.substring(2,3) || first==sc.substring(3,4))
{
textView1.setBackgroundColor(Color.YELLOW);
}
i have tried this but it doesn't seem to be working it does no change
i am new to android studio code so please excuse if this has a very easy solution and i have also tried searching for similar questions online but none of them seemed to be working
In your code, you try for execute your if statement inside if(count==0). I assume you try execute your code if wordle is not fill yet since you dont explain clearly about count.
Based on your question here my answer, first you need initialize input and sc value
String sc = "WORD" \*just example value\*
String str=editText.getText().toString();
String first=str.substring(0,1);
String sec=str.substring(1,2);
String third=str.substring(2,3);
String fourth=str.substring(3,4);
Second, use if statement for check first letter is already filled
if(!first.equals(""))
Inside first if, create if statement for compare first letter with sc value
textView1.setText(first);
if(first==sc.substring(0,1))
{
textView1.setBackgroundColor(Color.GREEN);
}
else
{
textView1.setBackgroundColor(Color.YELLOW);
}
This simple way for reach your goal
== use when we have to compare integers. But you are using == to compare strings. Which is wrong. For comparing Strings you have to use string.matches() or string.equals().
Make changes in your code as below
if(count==0)
{
textView1.setText(first);
textView2.setText(sec);
textView3.setText(third);
textView4.setText(fourth);
if(first.matches(sc.substring(0,1))) // if "sc.substring(0,1)" throwing any error then parse it into "String.valueOf(sc.substring(0,1));"
{
textView1.setBackgroundColor(Color.GREEN);
}
else if(first.matches(sc.substring(1,2))|| first.matches(sc.substring(2,3)) || first.matches(sc.substring(3,4)))
{
textView1.setBackgroundColor(Color.YELLOW);
}
Let me know if the problem has not been solved yet.
I'm fairly new to programming, and I can't seem to figure out how to initialize a String on JFrame Form. I do not know what code to put in to initialize the String if the contents of the string is entered later, by the user. This basically means that the String (stringone) is currently a blank text field on my form. The user enters a sentence or string and the label (one) tells how many characters are in the string they just entered. Here is my code so far:
{String stringone = new String ();
int one;
one = Integer.parseInt(txtstringone.getText());
one = (stringone.length());
lblone.setText(String.valueOf(one));}
Currently there is a yellow line under the third line of code, saying it may not have been initialized. It also does not work when I run it. Hope this clears it up!
Thanks so much in advance!
Try this:
String stringone;
Remove new String()
If you want to initialize it, tou must pass it a String value. For example:
String stringone = "value";
The string's length (stringone.length()), will be 5.
So, in order to take the length of a String you must first initialize it. Otherwise, it will be null, and null does not have length.
The user enters a sentence or string and the label (one) tells how many characters are in the string they just entered.
What you need to do is add an event handler for when the user has entered the string. There is no "user entered string" before the user has given the string.
An event handler is code that runs when some "event" occurs. A user writing in a field is a possible event.
Here's how you can add an event listener on your txtstringone text field component. It will be triggered when the user presses the Enter key.
txtstringone = new JTextField();
txtstringone.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent evt) {
String text = txtstringone.getText();
int one = text.length();
lblone.setText(String.valueOf(one));
}
});
You can find a longer tutorial at https://docs.oracle.com/javase/tutorial/uiswing/components/textfield.html and a short working program at https://docs.oracle.com/javase/tutorial/uiswing/examples/components/TextDemoProject/src/components/TextDemo.java
I'm trying to check if a user's input into a text field contains certain letters and have it increment a counter if it does.
//this is the code for the button
//tfYourName is the name of the text field
//below is what I've tried already
private void btnResultsMouseClicked(java.awt.event.MouseEvent evt) {
if (tfYourName.getSelectedItems.toString.toUpperCase().contains("T"))
}
if (tfYourName.getText().toUpperCase().contains("T"))
counter++;
I'm writing a program where at one point, I need to print a String on a window using JOptionPane. The code for the line looks something like this:
JOptionPane.showMessageDialog(null, "Name: " + a.getName());
The getName function refers to the object a that i created that has a method that returns a String. However, when my code reaches this point, the program appears to enter some kind of infinite loop as the window never pops up and when using debug, it appears never-ending.
The main thing is, when I use getName, I am allowing the user the set this name with a different function earlier in the main driver.
getName() is basically one line, return name;
The code for my setName() function is basically:
Scanner a = new Scanner(System.in);
System.out.print("Pick a name: ");
name = in.nextLine();
a.close();
Name is a private variable in the class. The close() isn't necessary but I tried it to see if it had any effect.
What I did notice is that, if I use the above code, the window never pops up, and I get stuck in an infinite loop. However, if I simply change the name = line to anything, such as:
name = "foo";
The code runs smoothly, the window pops up, and I do not get stuck in a loop. Even if I do not input a name when the program prompts me to, resulting in a null String, the window still doesn't pop up. Can anyone help and advise me on why this is happening? Thanks.
Using Scanner operations creates a block in the WaitDispatchSuport class used by JOptionPane which checks non-dispatch threads are free from blocking IO. Calling Scanner.close() will not un-block the thread.
One solution is to call showMessageDialog from the EDT :
Scanner a = new Scanner(System.in);
System.out.print("Pick a name: ");
final String name = a.nextLine();
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
JOptionPane.showMessageDialog(null, "Name: " + name);
}
});
This code snippet can help you
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
final String s = scanner.nextLine();
SwingUtilities.invokeLater(() -> {
JDialog dialog = new JDialog();
dialog.setAlwaysOnTop(true);
JOptionPane.showMessageDialog(dialog, s);
});
}
I want name textfield validation and I am using this code, what is not working perfectly.
Name_text.addKeyListener(new KeyAdapter() {
#Override
public void keyPressed(KeyEvent EVT){
if(EVT.getKeyChar()>='a'&& EVT.getKeyChar()<='z'|| EVT.getKeyChar()>='A'&& EVT.getKeyChar()<='Z'|| EVT.getKeyChar()==8|| EVT.getKeyChar()==26){
Name_text.setEditable(true);
}
else{
JOptionPane.showMessageDialog(null, "enter characters only");
}
You really should perform this type of validation using the DocumentFilter class, this is exactly what it's designed for. If the user pastes text into the field or you use setText, you will not be notified of the change via the KeyListener
You could also check out this for a number of examples
I guess you want to validate with this code :
if (Character.isDigit(EVT.getKeyChar())) {
Name_text.setEditable(true);
Name_text.setText("");
} else {
JOptionPane.showMessageDialog(null, "enter characters only");
}
Edited by your comment.