hi ^_^
I want to write a code to set an (error) text in the Jtextfiled if the number contains more than 1 dot (eventHandling)! but my code actually didn't concatinate with the next digits I've enterded ,for example:(2. ) and stop !
so what is the right solution ?!!

this is the part of (eventHandling for the numbers and the dot )

else if (".0123456789".contains(event)) {
numberButton(event, out);

and this is the part of code for (the conditions of the dot and the numbers )

  private void numberButton(String number, String out) {
        String s = T.getText();
        int count=0;

        if (!s.isEmpty()) {
        char c[] = new char[10];
        c = s.toCharArray();
        boolean a = true;

        for (int m = 0; m < c.length; m++) {
        if (!Character.isDigit(c[m])) {
        a = false;
        break;

        } 

        }
        if (!a) {           
            for (int n=0 ;n<s.length();n++){
                if (s.indexOf(n)=='.'){
                    count++;
                }
                if (count > 1){
                    T.setText("Error");
                  break;
                }
                else if(count==1) { 
                    T.setText(T.getText() + number);

                }
                }//for


        }
        else {
        int num = Integer.parseInt(out);
        String result_string = Integer.toString(num);
        T.setText(result_string + number);
        }
        }
        else {
        T.setText(number);
        }
        }

Recommended Answers

All 6 Replies

from what I remember of your code, the . had a separate part in the actionPerformed, which you should keep.

for instance, if you want to block adding the second '.'

if ( ".".equals(event)){
  if ( T.getText().contains(".")){
    // there's already a . in the text, so end
    return;
  }
  // rest of your code
}

if you want to show an error message:

if ( ".".equals(event)){
  if ( T.getText().contains(".")){
    // there's already a . in the text, so show an error message and end
    JOptionPane.showMessageDialog(null, "You already have a . in your input", "ERROR", JOptionPane.ERROR_MESSAGE);
    return;
  }
  // rest of your code
}

@stultuske :)
it works in writing a number >>but actaully it gives me an exception when I add 2 numbers >>
when I enter the first number it works, for example (2.1) >>but for the seconde number it gives me an exception if I click on the dot (.) button !!(java.lang.NumberFormatException)

dot code
   else if (event.equals(".")) {
   String S=T.getText();
   if (!S.isEmpty()){
  if ( T.getText().contains(".")){
        // there's already a . in the text, so end
      T.setText("Error");
        return;
   }
  else {
        T.setText(Integer.toString(Integer.parseInt(out)) + ".");
  }

  }
   else {

    T.setText("0");
    T.setText(Integer.toString(Integer.parseInt(out)) + ".");


   }
} //dot 

-------------------------------------

add func. code
 else if (event.equals("+")) {
        String B_text=B_add.getText();
        Math_Operator(B_text);

    } 

---------------------------------------

for the equal button
    else if (event.equals("=")) {
    switch(operator){
    case'+':tot2=tot1+Double.parseDouble(T.getText());

        break;
    case'-':tot2=tot1-Double.parseDouble(T.getText());

    break;
    case'*':tot2=tot1*Double.parseDouble(T.getText());

    break;
    case'/':tot2=tot1/Double.parseDouble(T.getText());

    break;
    case '^':tot2=Math.pow(tot1,Double.parseDouble(T.getText()));

    }
    T.setText(Double.toString(tot2));
    tot1=0.0;
}
for the Mathoperator func
   private void Math_Operator(String button){
    operator =button.charAt(0);
    String t=T.getText();
    tot1=tot1+Double.parseDouble(T.getText());
    T.setText(" ");

}

and what does the stacktrace say? normally, it should tell you what is being converted to double and fails.

Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: For input string: " 2"
at java.lang.NumberFormatException.forInputString(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at calculator.actionPerformed(calculator.java:299)

-------
it gives me the exception at this line (calculator.java:299)

else {
 T.setText(Integer.toString(Integer.parseInt(out)) + ".");
 }

just after entering 2 and then enter (dot) it gives me that exception (and this is only when I try to enter the seconde number to be added to the first number (for the first number it works but for the seconde number it will not !! ) and that thing confuses me alot ! )
Do U have any idea what is happening here ?!!

You have a single blank character at the start of the string.
Ie it's " 2", but it should be "2". The blank character is not valid when parsing an integer.

Maybe you are initialising out to " " instead of "" somewhere?

@JamesCherrill thank U ^____^ that's right :))
finally >> :)))

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.