0

im am writing a java program of a frame that the user enters a list of numbers, in JTextFields and be stored in array elements, and when button is preseed its going to sort the arrae and set it in another JText fieldenter image description here

the problem is that when i click the button it gives an exception

thread "AWT-EventQueue-0" java.lang.NumberFormatException: For input string: ""
    at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
    at java.lang.Integer.parseInt(Integer.java:592)
    at java.lang.Integer.parseInt(Integer.java:615)
    at Array.actionPerformed(Array.java:92)

knowing that the actionPerformed method has the following code

public void actionPerformed(ActionEvent e){
        String s1= e.getActionCommand(); 
        if(s1.equals("Sort")){
            int[] a = new int[ 6]; 
            String ti1 = t1.getText();
            String ti2 = t2.getText();
            String ti3 = t3.getText();
            String ti4 = t4.getText();
            String ti5 = t5.getText();
            String ti6 = t6.getText();

            a[0] = Integer.parseInt(ti1); 
            a[1] = Integer.parseInt(ti2); 
            a[2] = Integer.parseInt(ti3); 
            a[3] = Integer.parseInt(ti4); 
            a[4] = Integer.parseInt(ti5); 
            a[5] = Integer.parseInt(ti6); 

            for(int i =0;i<a.length;i++){

            for(int j = 0; j<a.length-1;j++){ 
                if(a[j]>a[j+1]){
                    int temp = a[j];
                    a[j]=a[j+1];
                    a[j+1]=temp; 
                }

                }
            }
            t7.setText(Integer.toString(a[0]));
            t8.setText(Integer.toString(a[1]));
            t9.setText(Integer.toString(a[2]));
            t10.setText(Integer.toString(a[3]));
            t11.setText(Integer.toString(a[4]));
            t12.setText(Integer.toString(a[5]));
6
  • Java can't turn the empty string into a number, as the exception states. Commented Dec 3, 2019 at 15:33
  • the problem is that there is no empty string entered Commented Dec 3, 2019 at 15:40
  • Possible duplicate of stackoverflow.com/questions/8780962/… Commented Dec 3, 2019 at 15:42
  • the stacktrace tells you the line in which the error occurs (at Array.actionPerformed(Array.java:92)), which is likely one of the a[X] = Integer.parseInt(tiX); lines. Check why that tiX is empty. Commented Dec 3, 2019 at 15:42
  • 1
    What's the type of t<n> variables ? How are they initialized ? Commented Dec 3, 2019 at 15:44

1 Answer 1

0

I figured out the problem!! i aded the same JTextField object twice to the frame, rather than adding t1 and t10 ive added t1 twice thats why t10 was empty when i tried to use it

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.