473,322 Members | 1,409 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

Comparing JTextfield ints

1
I am trying to compare a number inserted into a jtextfield with a number generated with the program. Unfortunately the numbers never evaluate as equal. Here is an example of the code I'm working with. In the example if the user inserts 10 is should come up as equal but it still comes up as false.
Expand|Select|Wrap|Line Numbers
  1. import java.awt.*;
  2. import java.awt.event.*;
  3. import javax.swing.*;
  4.  
  5.  
  6. public class GamePanel extends JPanel {
  7.     private JTextField input;
  8.     private int target, guess;
  9.  
  10.     public GamePanel(){
  11.         int target = 10;
  12.         System.out.println(target);
  13.  
  14.  
  15.         input = new JTextField (4);
  16.         input.addActionListener(new GuessNumber());
  17.  
  18.         add(input);
  19.  
  20.         setPreferredSize(new Dimension(033,75));
  21.         setBackground(Color.white);
  22.  
  23.     }
  24.  
  25.     private class GuessNumber implements ActionListener
  26.     {
  27.         public void actionPerformed (ActionEvent event){
  28.             String text= input.getText();
  29.             guess = Integer.parseInt(text);
  30.             System.out.println(guess==target);
  31.         }
  32.     }
  33.  
  34. }
Oct 25 '08 #1
1 2085
sukatoa
539 512MB
I am trying to compare a number inserted into a jtextfield with a number generated with the program. Unfortunately the numbers never evaluate as equal. Here is an example of the code I'm working with. In the example if the user inserts 10 is should come up as equal but it still comes up as false.
Expand|Select|Wrap|Line Numbers
  1. import java.awt.*;
  2. import java.awt.event.*;
  3. import javax.swing.*;
  4.  
  5.  
  6. public class GamePanel extends JPanel {
  7.     private JTextField input;
  8.     private int target, guess;
  9.  
  10.     public GamePanel(){
  11.         int target = 10;
  12.         System.out.println(target);
  13.  
  14.  
  15.         input = new JTextField (4);
  16.         input.addActionListener(new GuessNumber());
  17.  
  18.         add(input);
  19.  
  20.         setPreferredSize(new Dimension(033,75));
  21.         setBackground(Color.white);
  22.  
  23.     }
  24.  
  25.     private class GuessNumber implements ActionListener
  26.     {
  27.         public void actionPerformed (ActionEvent event){
  28.             String text= input.getText();
  29.             guess = Integer.parseInt(text);
  30.             System.out.println(guess==target);
  31.         }
  32.     }
  33.  
  34. }
You have declared another target variable inside the constructor which you believe that the value 10 was assigned in the global declaration private int target, guess; since you are accessing target as global in the eventhandler

Change int target = 10 to this.target = 10
Oct 26 '08 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Alexander Mazo | last post by:
Hi! I'm new in JAVA. I'm wont change "Home" and "End", but nothing receive. public class RLTextField { public static String s = new String(); public static void main(String args) { JFrame...
12
by: Elijah Bailey | last post by:
I have two char arrays of size k. I want to know which one is bigger (exactly like for instance I compare two ints/longs/etc.). What is the fastest way to do this? k <= 10 usually for my...
16
by: Kevin Goodsell | last post by:
What do you think is the best way to handle a compiler warning about comparing an unsigned value to a signed value? Cast to silence it? Disable that warning altogether? Or just live with it? On...
0
JoeMac3313
by: JoeMac3313 | last post by:
The question is how can I update the JTextField in Class CreditDisplayField when I hit the button in class CreditButton? I wrote in a test printing to console to make sure the "logic.addCredits();"...
2
by: all eyes | last post by:
hi all, i am using here JTextField to get sum of all previously pressed numbers in one button called (+) and so on. there is only one button take care of addition and displaying the result in the...
1
by: ahmed222too | last post by:
the jtextfield donot accept Arabic language font when i run this code jtextfield.settext="ذهب الولد إلى المدرسة"; the arabic font change into another font in the jtextfield
4
by: ahmed222too | last post by:
when i read data from file by code into jtextfield the lines of the file apprear in Concatenation in the jtextfield (without Enters between lines) i want to transfere the content of the file to the...
3
by: Kid Programmer | last post by:
Hello guys. I was wondering if it is possible to make a JTextField read only. In a program I am writing I don't want the user to be able to edit the text in a JTextField.
17
by: D'Arcy J.M. Cain | last post by:
I'm not sure I follow this logic. Can someone explain why float and integer can be compared with each other and decimal can be compared to integer but decimal can't be compared to float? True...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.