[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

textfield for integer or string

Posted on 2004-10-07
10
Medium Priority
?
1,091 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 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses
Course of the Month19 days, 20 hours left to enroll

872 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