Solved

How do I make a text field only to accept mathematical operators as first character of input.

Posted on 2009-04-07
3
249 Views
Last Modified: 2013-12-14
How do I make the text field to accept mathematical operators (+, -, *, /) only at char(0). nowhere else. My code lets the program accept the mathematical operators everywhere. Please check my code. I am using netbeans
private void txtNumberActionPerformed(java.awt.event.ActionEvent evt) {
        // Action Listner handling code here:
           if(txtNumber.getText().charAt(0) == '+' ||
              txtNumber.getText().charAt(0) == '-' ||
              txtNumber.getText().charAt(0) == '*' ||
              txtNumber.getText().charAt(0) == '/')
         {
     dataDisplay.append(txtNumber.getText()+ '\n');
     txtNumber.setText(null);
        }
         else
         {
      JOptionPane.showMessageDialog(null,"Sorry, " + txtNumber.getText() +
              " is not a valid entry" + "\n" + "Examples of valid Entry are:"
              + "\n" + "+23.5  " + "-1  " + "/6  " + " *12");
            }
    }

Open in new window

0
Comment
Question by:chandanchoubey
[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
  • 2
3 Comments
 
LVL 12

Accepted Solution

by:
basav_com earned 250 total points
ID: 24087112
Check this:

Use like this:
            if(txtNumber.getText().indexOf("+")> 0 || txtNumber.getText().indexOf("-")> 0 || txtNumber.getText().indexOf("*")> 0 || txtNumber.getText().indexOf("/")> 0 ){
/**
 * @(#)AcceptChars.java Created on Apr 7, 2009
 *
 * Copyright (c) 2007-2008 Subex, Ltd.
 * All Rights Reserved.
 *
 * This software is the confidential and proprietary information of
 * Subex Ltd ("Confidential Information"). You shall not disclose
 * such Confidential Information and shall use it only in accordance
 * with the terms of the license agreement you entered into
 * with Subex Ltd.
 */
 
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
 
/**
 * @author basaveswar.kureti
 *
 */
public class AcceptChars extends Frame implements TextListener {
	TextField txtNumber;
	AcceptChars(){
		txtNumber = new TextField(20);
		setLayout(new FlowLayout());
		add(txtNumber);
		setSize(500,400);
		setVisible(true);
		txtNumber.addTextListener(this);
		
	}
	
	public void textValueChanged(TextEvent  evt) {
        // Action Listner handling code here:
		if(txtNumber.getText().indexOf("+")> 0 || txtNumber.getText().indexOf("-")> 0 || txtNumber.getText().indexOf("*")> 0 || txtNumber.getText().indexOf("/")> 0 ){
			
   	      JOptionPane.showMessageDialog(null,"Sorry, " + txtNumber.getText() +
	              " is not a valid entry" + "\n" + "Examples of valid Entry are:"
	              + "\n" + "+23.5  " + "-1  " + "/6  " + " *12");
   	      String txt = txtNumber.getText();
   	       txtNumber.setText(txt.substring(0,txt.length()-1));
           }
		
 
	}
	
	public static void main(String args[]){
		new AcceptChars();
	}
 
}

Open in new window

0
 

Author Comment

by:chandanchoubey
ID: 24093241
Thanks basav_com. I appreciate your help. But if I input ++40 or --40. It accepts. I want only +40 or -40 to accept. If you can help me with this that will be great!
0
 

Author Closing Comment

by:chandanchoubey
ID: 31567454
Thanks basav_com. I appreciate your help. But if I input ++40 or --40. It accepts. I want only +40 or -40 to accept. If you can help me with this that will be great!
0

Featured Post

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
use lov values 2 72
Error in @AspectJ Based AOP with Spring 2 23
Is there a simpler dropbox system? 10 44
dao vs facade design patterns 2 35
Jaspersoft Studio is a plugin for Eclipse that lets you create reports from a datasource.  In this article, we'll go over creating a report from a default template and setting up a datasource that connects to your database.
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

733 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