Hello,

I am trying to build a gui with two textfields, a textarea, and a few buttons. The two textfields are for the user to enter in random numbers (one number in each textfield). I then want the person to be able to hit a button that will then take these two values, put them into a two-dimensional array, and then display those values inside the textarea. The person should then be able to enter in two more values, hit the button, those two new values go into the array with the others and the textarea should then display all four values. Here is the part of the code that I am having trouble with:

private void enterButtonActionPerformed(java.awt.event.ActionEvent evt) {                                            
        double number1 = 0;
        try {
             number1 = Double.parseDouble(
                this.minutesField.getText());
             if (number1 <= 0 || number1 > 240) {
                 throw new Exception();
             }

        }
        catch (Exception e) {
            JOptionPane.showMessageDialog(this, "Invalid input. Please try again.", "Error", JOptionPane.ERROR_MESSAGE);
            return;
        }
        double number2 = 0;
        try {
             number2 = Double.parseDouble(
                this.wagesField.getText());
             if (number2 <= 0) {
                 throw new Exception();
             }
        }
        catch (Exception e) {
            JOptionPane.showMessageDialog(this, "Invalid input. Please try again.", "Error", JOptionPane.ERROR_MESSAGE);
            return;
        }

        my[][] array = new my[10][2];

        for (int r=0; r<array.length; r++) {
            
            for (int c=0; c<array[r].length; c++) {
              
            }
        }
        resultsArea.setText("************************\nMinutes   Wages\n" + array);

    }

When the button is clicked, the textarea then displays the setText message, but the array looks like: '[[Lclass$class;@randomNumbersLetters' - where randomNumbersLetters is a bunch of numbers and letters that change when I enter different values into the textfields and press the button again. There is probably an easy solution to this but as a beginner at Java I am having trouble figuring this out.

Thanks in advance!

Recommended Answers

All 6 Replies

L34 add to check:

System.out.println(array[r][c]);

L30 fill array

Yes I tried that already and it gives me errors. I will try to simplify my problem a bit. This is the code that I am having problems with:

// Here is the two-dimensional array
        my[][] array = new my[10][2];

        for (int r=0; r<array.length; r++) {
          
           //variable number1 is of type double as seen in my first post
           array[r] = number1
            
             for (int c=0; c<array[r].length; c++) {
                
                //variable number1 is of type double as seen in my first post
                array[c] = number2
              
            }
        }
        /* resultsArea is the variable name for the JTextField that I want the following text and the array to be posted into when the button is clicked */
 
        resultsArea.setText("************************\nMinutes   Wages\n" + array);

The user will enter 2 numbers. One number into one textfield, and another number into another textfield. When the button is clicked, one number should be assigned to variable "number1" and the other number assigned to variable "number2". These two numbers should then enter the array and be displayed on the screen in the JTextArea "resultsArea". I just cannot figure out how to do it!

Declare array as

double[][] array = new double[10][2];

Ok, so I did that and now the lines 7 and 12 (array[r] = number1, and array[c] = number2) give an error saying:

"incompatible types; Required: double[]; Found: double"

So I am guessing that it is looking for an array but finding "double" because that is what the type is for the variables: number1, number2. Is there an easy way to fix this? Thanks!

array is two dimensional, adress of any element of array have two components r and c
two loops:

public static void main(String[] args) {
        double[][] array = new double[10][2];
        for (int r = 0; r < array.length; r++) {
            for (int c = 0; c < array[r].length; c++) {
                array[r][c]=2;// example
                System.out.println(array[r][c]);
            }
        }
    }

or one loop-for:

public static void main(String[] args) {
        double number1 = 111;//example
        double number2 = 22;//example
        double[][] array = new double[10][2];
        for (int r = 0; r < array.length; r++) {
            array[r][0] = number1;
            array[r][1] = number2;
            System.out.println(array[r][0]);
            System.out.println(array[r][1]);
            System.out.println();

        }
    }

//
array[r] -- at this adress is the second one-dimensional table: double[]
http://java.sun.com/docs/books/tutorial/java/nutsandbolts/arrays.html

Thanks Sooooo much quuba! Everything is fixed!

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.