Solved

textfield for integer or string

Posted on 2004-10-07
10
1,080 Views
Last Modified: 2008-03-03
Hi experts,

I need code for the following:
I have a textfield and two buttons. One button should be expect an integer from the text field (between 1-99) and when pressed it changes an integer variable.
the other button should expect a String and then change the "name" variable

thanks


also if anyone can help me with this question I accidently forgot to place it in the java topic area:
http://www.experts-exchange.com/Programming/Q_21154053.html

thanks everyone
0
Comment
Question by:Samooramad
  • 5
  • 3
  • 2
10 Comments
 
LVL 37

Accepted Solution

by:
zzynx earned 300 total points
ID: 12246436
By heart:

button1.addActionListener( new ActionListener() {
   public void actionPerformed(ActionEvent evt) {
       integerVariable = Integer.parse(textField.getText());
   }
});

button2.addActionListener( new ActionListener() {
   public void actionPerformed(ActionEvent evt) {
       name = textField.getText();
   }
});
0
 
LVL 37

Expert Comment

by:zzynx
ID: 12246447
Sorry:

button1.addActionListener( new ActionListener() {
   public void actionPerformed(ActionEvent evt) {
       try {
          integerVariable = (Integer.valueOf(textField.getText())).intValue();
       } catch (NuberFormatException ex) { }
   }
});
0
 
LVL 37

Assisted Solution

by:zzynx
zzynx earned 300 total points
ID: 12246514
>> between 1-99
Of course you can check for that...

button1.addActionListener( new ActionListener() {
   public void actionPerformed(ActionEvent evt) {
       try {
          int tmp = (Integer.valueOf(textField.getText())).intValue();
          if (tmp>0 && tmp<100)
              integerVariable = tmp;
          else
              // You say it ;°)
       } catch (NuberFormatException ex) { }
   }
});
0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
LVL 5

Assisted Solution

by:Naeemg
Naeemg earned 100 total points
ID: 12246685
Try this code:


  void jButton4_actionPerformed(ActionEvent e)
  {
    int thisVal = Integer.parseInt(jTextField1.getText());
    int myValu = 0;
   
    if( thisVal >= 1 || thisVal <= 99)
    {
      myValu = thisVal;
    }
  }

  void jButton5_actionPerformed(ActionEvent e)
  {
    boolean isName = false;
    String name = "";
   
    //-- check whether this string contains A-Z, a-z and white space and dot.
    for(int i = 0; i < jTextField1.getText().length(); i++)
    {
      System.out.println("(int)jTextField1.getText().charAt(i) = " +
                       (int)jTextField1.getText().charAt(i));
     
   
      //** count number of Dots
       if(((int)jTextField1.getText().charAt(i) >= 65 && (int)jTextField1.getText().charAt(i) <= 90 ) //** A-Z
          ||((int)jTextField1.getText().charAt(i) >= 97 && (int)jTextField1.getText().charAt(i) <= 122 ) //** a-z
          ||((int)jTextField1.getText().charAt(i) == 46) //** equal to dot.
          ||((int)jTextField1.getText().charAt(i) == 32)) //** equal to white space
       {
         isName = true;
       }
       else
       {
           isName = false;
       }
    }

    if(isName)
      name = jTextField1.getText();
  }



bye.
Naeem Shehzad Ghuman
0
 
LVL 37

Expert Comment

by:zzynx
ID: 12246770
Allow me to correct:

1)
  void jButton4_actionPerformed(ActionEvent e)
  {
     try {                                                  // <<<<<<<< try/catch needed
         int thisVal = Integer.parseInt(jTextField1.getText());
         if( thisVal >= 1 && thisVal <= 99)    // <<<<<<  && instead of ||
            myValu = thisVal;
     } catch (NumberFormatException ex) {
     }
  }

  Remark: I don't see the added value compared with my previous comment


2)
If you want to check if the string only contains A-Z, a-z, spaces and dots, you can better do this:

void jButton5_actionPerformed(ActionEvent e) {

    if ( jTextField1.getText().matches("([A-Za-z \\.])+?") )
      name = jTextField1.getText();
}

That's the power of regular expressions!
0
 
LVL 5

Expert Comment

by:Naeemg
ID: 12246819
Thanks zzynx  for correcting my mistakes.
0
 

Author Comment

by:Samooramad
ID: 12250219
thanks both of you. I'm working on it.. will get back to you
0
 

Author Comment

by:Samooramad
ID: 12250916
excelent help as always zzynx :)
thanks Naeemg for your help also.. I forgot I need to check for letters and spaces :)
0
 

Author Comment

by:Samooramad
ID: 12250946
hey if you guys have a little time please help with the question I mentioned above.. Its driving me nutts :)
0
 
LVL 37

Expert Comment

by:zzynx
ID: 12251725
thanks
0

Featured Post

Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
tomcat administrtor 12 61
type mismatch (Object[] to double[] 4 33
eclipse buid path vs tomcat lib path 10 34
What browser will run Java? 7 72
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

820 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question