Solved

textfield for integer or string

Posted on 2004-10-07
10
1,079 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
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 
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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
topping3 challenge 14 79
eclipse formatting 6 87
why cannot we forward request once the response is committed 2 42
Desingning Refactoring existing code 2 24
For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
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…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…

785 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