0

I want to know how to make my program only accept (1101101) as the only correct input on JTextField txtCode. At the moment, I can enter any code in the textfield and it will display the same output.

Here's the code:

import java.awt.FlowLayout;
import java.awt.event.ActionListener;

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

@SuppressWarnings("serial")
public class GUI extends JFrame {

JLabel coffeeProgram;
JButton button;
JTextField txtCode;
JTextField price;
JTextField description;
JLabel outputPrice;

public GUI() {
     setLayout(new FlowLayout());
    coffeeProgram = new JLabel("Enter Code Here:");
    add(coffeeProgram);
    txtCode = new JTextField(15);
    add(txtCode);  
    button = new JButton("Submit Code");
    add(button);

       outputPrice = new JLabel(" Price:  ");
       add(outputPrice);

      JTextField price;

      CoffeeReturn objCoffee = new CoffeeReturn();

      double myPrice = objCoffee.CoffeeCode(txtCode.getText());

      price = new JTextField(15);
      add(price);

      price.setText("Price is"+myPrice);

 }
}

I'm sorry if this code seems a little inexperienced. I'm 15 and started to try and learn java 2 days ago. Here's a screenshot of my program: http://gyazo.com/afe2abf5fcffa3822bd41eaea8581597

Andy
  • 3
  • 2

2 Answers2

0

Take a look at http://docs.oracle.com/javase/tutorial/uiswing/components/formattedtextfield.html

It explains how you can use a format to allow only specific characters.

Davio
  • 4,609
  • 2
  • 31
  • 58
0

I guess you need to change the data type to succeed what you are trying to do.

Refer: Why can't we use '==' to compare two float or double numbers

You could probably use an Integer or a String value to do the comparison in the way in which you want.

Community
  • 1
  • 1
Bey0ndZ
  • 114
  • 3