Results 1 to 18 of 18
Like Tree1Likes
  • 1 Post By wsaryada

Thread: Error trying to get data into array - .nextDouble()?

  1. #1
    neveser is offline Member
    Join Date
    Nov 2012
    Posts
    40
    Rep Power
    0

    Default Error trying to get data into array - .nextDouble()?

    Not sure why I'm getting this error - cannot find symbol, symbol: method nextDouble(), location: variable userInput of type double[][]

    What I'm trying to do is get the 2 user inputs (minutesIn and paymentIn) into 2 columns. Later after the data is entered, I want the user to be able to hit a "Report" button that sums each column so they can see the totals.

    Error occurs in line 23.

    Java Code:
    private void enterButtonActionPerformed(java.awt.event.ActionEvent evt) {                                            
           
            //Get input (minutes and payment) from user
    
            try {
                minutesIn = Double.parseDouble(minutesField.getText());
                paymentIn = Double.parseDouble(paymentField.getText());
                if (minutesIn >= 0); else {
                    outputTextArea.append("Please enter a vaild number of minutes\n");
                    return;
                }
                if (paymentIn >= 0); else {
                    outputTextArea.append("Please enter a vaild payment amount\n");
                    return;
                }
    
            } catch (Exception e) {
                outputTextArea.append("Please input a numerical value\n");
                return;
            }
            for (int row = 0; row < 50; row++) {
                for (int col = 0; col < 1; col++) {
                    userInput[row][col] = userInput.nextDouble();
                }
            }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Error trying to get data into array - .nextDouble()?

    This statement makes no sense:
    Java Code:
    userInput[row][col] = userInput.nextDouble();
    What is userInput? Is it an array as your left side suggests? Is it a Scanner object as the right side of the statement suggests?

  3. #3
    neveser is offline Member
    Join Date
    Nov 2012
    Posts
    40
    Rep Power
    0

    Default Re: Error trying to get data into array - .nextDouble()?

    userInput is the array.

    I'm not using using a scanner object because I'm parsing a jtextfield. I guess that's why the right side of the statement gives me the error.
    Not sure what to use on the right side.

  4. #4
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    760
    Rep Power
    8

    Default Re: Error trying to get data into array - .nextDouble()?

    What do you really want to do? the userInput is cleary an array and it doesn't have the nextDouble() method. If you are trying to read inputs from user then you need a Scanner object here as Fubarable said.

  5. #5
    neveser is offline Member
    Join Date
    Nov 2012
    Posts
    40
    Rep Power
    0

    Default Re: Error trying to get data into array - .nextDouble()?

    How do you use a scanner on a jtextfield? That's where the user input comes from.

  6. #6
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    760
    Rep Power
    8

    Default Re: Error trying to get data into array - .nextDouble()?

    If it a JTextField then you can use the getText() method to get what is input by user just like what you did in

    Java Code:
    minutesIn = Double.parseDouble(minutesField.getText());

  7. #7
    neveser is offline Member
    Join Date
    Nov 2012
    Posts
    40
    Rep Power
    0

    Default Re: Error trying to get data into array - .nextDouble()?

    So your saying the loop in unnecessary to increment through the array?

    In other words, after the user has clicked the enter button and entered the information, the next time they enter the information, the array will automatically increment?

    Example:
    So user enters 30 (minutes) and 100 (payment), clicks the enter button and these number are entered into [0][0], [0][1]
    Then the user enters 60(minutes) and 200(payment), clicks the enter button and the values are entered into [1][0], [1][1]

    Is that going to happen without a loop?

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Error trying to get data into array - .nextDouble()?

    Quote Originally Posted by neveser View Post
    userInput is the array.

    I'm not using using a scanner object because I'm parsing a jtextfield. I guess that's why the right side of the statement gives me the error.
    Not sure what to use on the right side.
    Well don't just guess by posting nonsense code and wondering why it isn't working. Always go to first principles. If you need to get two values, then perhaps you want to use two JTextFields. But it looks like you're doing just that, so I'm not sure why you even have that for loop.

  9. #9
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Error trying to get data into array - .nextDouble()?

    Quote Originally Posted by neveser View Post
    So your saying the loop in unnecessary to increment through the array?

    In other words, after the user has clicked the enter button and entered the information, the next time they enter the information, the array will automatically increment?
    Only if you code it to behave this way. You as the programmer are in complete control.

    Example:
    So user enters 30 (minutes) and 100 (payment), clicks the enter button and these number are entered into [0][0], [0][1]
    Then the user enters 60(minutes) and 200(payment), clicks the enter button and the values are entered into [1][0], [1][1]

    Is that going to happen without a loop?
    A loop would make no sense since you aren't in fact looping. You would increment a counter or index variable each time the button is pressed.

  10. #10
    neveser is offline Member
    Join Date
    Nov 2012
    Posts
    40
    Rep Power
    0

    Default Re: Error trying to get data into array - .nextDouble()?

    Quote Originally Posted by Fubarable View Post
    Well don't just guess by posting nonsense code and wondering why it isn't working. Always go to first principles. If you need to get two values, then perhaps you want to use two JTextFields. But it looks like you're doing just that, so I'm not sure why you even have that for loop.
    Sorry Fubarable. I just thought I had to use a loop to move through the array so the values got into the right places. That's why I started working on the loop today. I thought that was a missing piece of the puzzle. I didn't think it was exactly nonsense code, I just thought I missed something like having things in the right order or the having the right variables in that statement.

    I do indeed have 2 JTextfields for the user to use.

  11. #11
    neveser is offline Member
    Join Date
    Nov 2012
    Posts
    40
    Rep Power
    0

    Default Re: Error trying to get data into array - .nextDouble()?

    Quote Originally Posted by Fubarable View Post
    Only if you code it to behave this way. You as the programmer are in complete control.



    A loop would make no sense since you aren't in fact looping. You would increment a counter or index variable each time the button is pressed.
    Hmm... That's how I started this off then I switched to thinking the loop was needed.
    So I'd use an increment for the rows then? Just a simple row++ at the end of all that?
    Then I could use the row variable in the Report section to run through the array for the totalMinutes and totalPayments?

  12. #12
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Error trying to get data into array - .nextDouble()?

    Quote Originally Posted by neveser View Post
    Hmm... That's how I started this off then I switched to thinking the loop was needed.
    So I'd use an increment for the rows then? Just a simple row++ at the end of all that?
    Then I could use the row variable in the Report section to run through the array for the totalMinutes and totalPayments?
    Yes, use a row variable that is incremented each time the submit button is pressed. Then you could use that row value when adding information to your array, or perhaps better, ArrayList (in which case you wouldn't even need an index).

  13. #13
    neveser is offline Member
    Join Date
    Nov 2012
    Posts
    40
    Rep Power
    0

    Default Re: Error trying to get data into array - .nextDouble()?

    I've added a simple
    Java Code:
    row++
    into the code and the first set of values(row) is being over written.

    When I click the Enter button I have a line to watch the array
    Java Code:
    System.out.println(userInput[0][0] + "\t" + (userInput[0][1]));
    When I enter a second set of values and click the enter button, the new values overwrites the first pair. The code's not holding the first row and advancing to the second row. After I click the button for the second time, I should still see the first set of values because the statement doesn't change. It's still prints the values for [0][0],[0][1].

    I'm a bit amazed. I have 2 books from the library on my desk plus an eTextbook and I've read the array chapters from everything and I still can't wrap my head around this concept. This seems much worse than arrays in VB.

  14. #14
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    760
    Rep Power
    8

    Default Re: Error trying to get data into array - .nextDouble()?

    Can you post your latest code for us to see?

  15. #15
    neveser is offline Member
    Join Date
    Nov 2012
    Posts
    40
    Rep Power
    0

    Default Re: Error trying to get data into array - .nextDouble()?

    Yep. Here's the whole thing.

    There's a lot of remmed out stuff on there so it's a bit of a mess. Lot's of trial and error. Mostly error though.

    Java Code:
    package my.earnings;
    
    import java.util.*;
    import java.util.Arrays;
    
    /**
     *
     * @author Rusteater
     */
    public class Earnings extends javax.swing.JFrame {
        
        // vaiable for entering raw user data 
        //(printing after hitting enter)
        double minutesIn, paymentIn;
        //row for array count
        int row = 0;
        final int COLUMNS = 2;
        final int MAX_ROWS = 20;
        double[][] userInput = new double[MAX_ROWS][COLUMNS];
        //double[][] userInput = {{minutesIn, paymentIn}};
        //values for calculation
        double totalMinutes = 0;
        double totalPayment = 0;
        double averageWage = 0;
        final int HOUR = 60;
        double minWage = 10;
        int printHeader = 0;
        
        
    
        /**
         * Creates new form Earnings
         */
        public Earnings() {
            initComponents();
        }
    
        /**
         * This method is called from within the constructor to initialize the form.
         * WARNING: Do NOT modify this code. The content of this method is always
         * regenerated by the Form Editor.
         */
        @SuppressWarnings("unchecked")
        // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
        private void initComponents() {
    
            tutoringLabel = new javax.swing.JLabel();
            paymentLabel = new javax.swing.JLabel();
            minutesField = new javax.swing.JTextField();
            paymentField = new javax.swing.JTextField();
            jScrollPane1 = new javax.swing.JScrollPane();
            outputTextArea = new javax.swing.JTextArea();
            enterButton = new javax.swing.JButton();
            reportButton = new javax.swing.JButton();
            quitButton = new javax.swing.JButton();
    
            setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
            setTitle("KET1 Task 1 ");
    
            tutoringLabel.setText("Tutoring time in minutes");
    
            paymentLabel.setText("Payment for session in dollars and cents");
    
            outputTextArea.setColumns(20);
            outputTextArea.setRows(5);
            jScrollPane1.setViewportView(outputTextArea);
    
            enterButton.setText("Enter");
            enterButton.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    enterButtonActionPerformed(evt);
                }
            });
    
            reportButton.setText("Run Report");
            reportButton.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    reportButtonActionPerformed(evt);
                }
            });
    
            quitButton.setText("Quit");
            quitButton.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    quitButtonActionPerformed(evt);
                }
            });
    
            javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
            getContentPane().setLayout(layout);
            layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGap(37, 37, 37)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(paymentLabel)
                        .addComponent(tutoringLabel)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 337, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup()
                            .addGap(18, 18, 18)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                .addComponent(minutesField, javax.swing.GroupLayout.DEFAULT_SIZE, 68, Short.MAX_VALUE)
                                .addComponent(paymentField))
                            .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                            .addGap(41, 41, 41)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(enterButton)
                                .addComponent(reportButton)
                                .addComponent(quitButton))
                            .addGap(26, 26, 26))))
            );
            layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGap(34, 34, 34)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(tutoringLabel)
                        .addComponent(minutesField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGap(18, 18, 18)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(paymentLabel)
                        .addComponent(paymentField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGap(27, 27, 27)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup()
                            .addComponent(enterButton)
                            .addGap(18, 18, 18)
                            .addComponent(reportButton)
                            .addGap(18, 18, 18)
                            .addComponent(quitButton))
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 158, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addContainerGap(23, Short.MAX_VALUE))
            );
    
            pack();
        }// </editor-fold>                        
    
        private void reportButtonActionPerformed(java.awt.event.ActionEvent evt) {                                             
            // Run report (calcualtion and print to screen)
    
            outputTextArea.append("\n\n\n********************************\n");
            outputTextArea.append("Report of your wages to Date:\n\n");
            outputTextArea.append("Total minutes spent tutoring= " + totalMinutes + "\n");
            outputTextArea.append("Total Earnings= $" + totalPayment + "\n");
            outputTextArea.append(String.format("Average Per Hour Wage= $%.2f", averageWage));
            outputTextArea.append("\n\n");
            
            if (averageWage < minWage) {
                outputTextArea.append("Your average wages per hour are below average");
            } else if (averageWage >= minWage && averageWage <= minWage * 2) {
                outputTextArea.append("Your average wages per hour are average");
            } else if (averageWage > minWage * 2) {
                outputTextArea.append("Your average wages per hour are above average");
            }
    
        }                                            
    
        private void quitButtonActionPerformed(java.awt.event.ActionEvent evt) {                                           
            // Quit the program
            System.exit(0);
        }                                          
    
        private void enterButtonActionPerformed(java.awt.event.ActionEvent evt) {                                            
            //Get input (minutes and payment) from user
    
            try {
                minutesIn = Double.parseDouble(minutesField.getText());
                paymentIn = Double.parseDouble(paymentField.getText());
                if (minutesIn >= 0); else {
                    outputTextArea.append("Please enter a vaild number of minutes\n");
                    return;
                }
                if (paymentIn >= 0); else {
                    outputTextArea.append("Please enter a vaild payment amount\n");
                    return;
                }
    
            } catch (Exception e) {
                outputTextArea.append("Please input a numerical value\n");
                return;
            }
    
            // Store input into array
            // original declaration below
            //double[][] userInput = {{minutesIn, paymentIn}};
            //double[][] userInput = new double[20][2];
            //userInput [MAX_ROWS][COLUMNS] = minutesIn ;
            
            //watch array
            System.out.println(userInput[0][0] + "\t" + (userInput[0][1]));
    
            //array calcs
           
            
    
    
            //Print entries into textarea(outputTextArea)
    
            if (printHeader == 0) {
                outputTextArea.append("*****************************\n");
                outputTextArea.append("Raw Tutoring Earnings Data\n\n");
                outputTextArea.append("Minutes" + "\t" + "Earnings\n");
                outputTextArea.append(minutesIn + "\t" + paymentIn + "\n");
                printHeader++;
            } else {
                outputTextArea.append(minutesIn + "\t" + paymentIn + "\n");
            }
            totalMinutes += minutesIn;
            totalPayment += paymentIn;
            averageWage = (totalPayment / (totalMinutes / HOUR));
    
            //add one to array row counter
            row++;
    
            //clear jTextFields (minutesField & paymentField)
            minutesField.setText("");
            paymentField.setText("");
    
    
        }                                           
    
        /**
         * @param args the command line arguments
         */
        public static void main(String args[]) {
            /* Set the Nimbus look and feel */
            //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
            /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
             * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
             */
            try {
                for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                    if ("Nimbus".equals(info.getName())) {
                        javax.swing.UIManager.setLookAndFeel(info.getClassName());
                        break;
                    }
                }
            } catch (ClassNotFoundException ex) {
                java.util.logging.Logger.getLogger(Earnings.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            } catch (InstantiationException ex) {
                java.util.logging.Logger.getLogger(Earnings.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            } catch (IllegalAccessException ex) {
                java.util.logging.Logger.getLogger(Earnings.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            } catch (javax.swing.UnsupportedLookAndFeelException ex) {
                java.util.logging.Logger.getLogger(Earnings.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            }
            //</editor-fold>
    
            /* Create and display the form */
            java.awt.EventQueue.invokeLater(new Runnable() {
                public void run() {
                    new Earnings().setVisible(true);
                }
            });
        }
        // Variables declaration - do not modify                     
        private javax.swing.JButton enterButton;
        private javax.swing.JScrollPane jScrollPane1;
        private javax.swing.JTextField minutesField;
        private javax.swing.JTextArea outputTextArea;
        private javax.swing.JTextField paymentField;
        private javax.swing.JLabel paymentLabel;
        private javax.swing.JButton quitButton;
        private javax.swing.JButton reportButton;
        private javax.swing.JLabel tutoringLabel;
        // End of variables declaration                   
    }
    I know it's a mess. I like to keep my ideas there until I get it working. I'll clean it up afterwards.

    Line 189 is where I'm stuck. Trying to get the data into the array.
    The array read 0.0 0.0 when I print it out in the console.

    It's the enter button code that I'm working on. If you want I can remove the bulk of that code and leave only the enter button code. That's an awful lot of code to look at. I thought that maybe you wanted to see it all in case my problemexisted outside of that button code. Just let me know and I'll remove the extra code.
    Last edited by neveser; 12-02-2012 at 11:19 AM.

  16. #16
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    760
    Rep Power
    8

    Default Re: Error trying to get data into array - .nextDouble()?

    Your original code:

    Java Code:
    // Store input into array
    // original declaration below
    //double[][] userInput = {{minutesIn, paymentIn}};
    //double[][] userInput = new double[20][2];
    //userInput [MAX_ROWS][COLUMNS] = minutesIn ;

    From my understanding I believe you want something like this:

    Java Code:
    // Store input into array
    // original declaration below
    userInput[row][0] = minutesIn;
    userInput[row][1] = paymentIn;
    
    //watch array
    System.out.println("----------------------------------------");
    System.out.println(userInput[row][0] + "\t" + (userInput[row][1]));
    System.out.println("----------------------------------------");
    Fubarable likes this.

  17. #17
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Error trying to get data into array - .nextDouble()?

    Exactly. As I've been suggesting you want to use the row variable as the row index in your array.

  18. #18
    neveser is offline Member
    Join Date
    Nov 2012
    Posts
    40
    Rep Power
    0

    Default Re: Error trying to get data into array - .nextDouble()?

    Thanks guys. It's working perfectly.
    I put a line in under my report button code that prints the array to the console to see it after 2 entries. The values are there and in the correct places. Now it seems that I can do the final calculations under the report button code, just have to figure out the for loop to add up the 2 columns.

Similar Threads

  1. NullPointerException when there is data in array
    By anoorally in forum New To Java
    Replies: 3
    Last Post: 02-09-2012, 12:33 AM
  2. Q about shifting data in an array
    By alihht in forum New To Java
    Replies: 7
    Last Post: 02-03-2010, 07:17 AM
  3. error while retrieving data from data base
    By kirtesh4u in forum New To Java
    Replies: 5
    Last Post: 11-15-2008, 05:10 PM
  4. add data into an array
    By mispeed in forum New To Java
    Replies: 9
    Last Post: 11-08-2007, 04:53 AM
  5. Add data to an array
    By adlb1300 in forum New To Java
    Replies: 8
    Last Post: 11-05-2007, 03:01 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •