[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

textfield for integer or string

Posted on 2004-10-07
10
Medium Priority
?
1,088 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
  • 2
10 Comments
 
LVL 37

Accepted Solution

by:
zzynx earned 1200 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 1200 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 5

Assisted Solution

by:Naeemg
Naeemg earned 400 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

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

Question has a verified solution.

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

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
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…
Suggested Courses

650 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