Solved

textfield for integer or string

Posted on 2004-10-07
10
1,076 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
 
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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

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)
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

744 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now