Help With Java

Discussion in 'Mac Programming' started by dergaderg, Mar 9, 2011.

  1. dergaderg, Mar 9, 2011
    Last edited by a moderator: Mar 9, 2011

    dergaderg macrumors member

    Joined:
    Feb 9, 2009
    #1
    im am trying to get a list to be displayed on a jFrame. here is the code....


    Code:
    import java.util.List;
    import java.util.ArrayList;
    
            
    public class Collatz extends javax.swing.JFrame {
    
        /** Creates new form Collatz */
        public Collatz() {
            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() {
    
            jLabel1 = new javax.swing.JLabel();
            jTextField1 = new javax.swing.JTextField();
            jButton1 = new javax.swing.JButton();
            jScrollPane1 = new javax.swing.JScrollPane();
            jList1 = new javax.swing.JList();
            jScrollPane3 = new javax.swing.JScrollPane();
            jScrollPane2 = new javax.swing.JScrollPane();
    
            setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
    
            jLabel1.setText("Loop (If Applicable)");
    
            jTextField1.setText("Intial Number");
            jTextField1.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    jTextField1ActionPerformed(evt);
                }
            });
    
            jButton1.setText("Go!");
            jButton1.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    jButton1ActionPerformed(evt);
                }
            });
    
            jList1.setModel(new javax.swing.AbstractListModel() {
                String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
                public int getSize() { return strings.length; }
                public Object getElementAt(int i) { return strings[i]; }
            });
            jScrollPane1.setViewportView(jList1);
    
            jScrollPane3.setViewportView(jScrollPane2);
    
            org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
            getContentPane().setLayout(layout);
            layout.setHorizontalGroup(
                layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                    .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                        .add(layout.createSequentialGroup()
                            .add(60, 60, 60)
                            .add(jTextField1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 128, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                        .add(layout.createSequentialGroup()
                            .add(108, 108, 108)
                            .add(jLabel1)))
                    .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 40, Short.MAX_VALUE)
                    .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                        .add(layout.createSequentialGroup()
                            .add(jScrollPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                            .add(jScrollPane3, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 14, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                            .addContainerGap())
                        .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                            .add(jButton1)
                            .add(70, 70, 70))))
            );
            layout.setVerticalGroup(
                layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                .add(layout.createSequentialGroup()
                    .add(60, 60, 60)
                    .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                        .add(jButton1)
                        .add(jTextField1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                    .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                        .add(layout.createSequentialGroup()
                            .add(76, 76, 76)
                            .add(jLabel1))
                        .add(layout.createSequentialGroup()
                            .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                            .add(jScrollPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                        .add(layout.createSequentialGroup()
                            .add(18, 18, 18)
                            .add(jScrollPane3, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 64, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))
                    .addContainerGap(62, Short.MAX_VALUE))
            );
    
            pack();
        }// </editor-fold>                        
    
        private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    
            double c;
           
             
             
          
                   c =  Double.parseDouble(jTextField1.getText());
                   
                    List<String> ls=new ArrayList<String>();
                    
                    ls.add(String.valueOf(c));
                    
                    
                   
              
                   do {
           if (c == -5) {jLabel1.setText("loop = −5 → −14 → −7 → −20 → −10 → −5");}
         if (c == -17) {jLabel1.setText("loop = −17 → −50 → −25 → −74 → −37 → −110 "
                    + "→ −55 → −164 → −82 → −41 → −122 → −61 → "
                    + "−182 → −91 → −272 → −136 → −68 → −34 → −17 …	");}
             if (c == -1) {jLabel1.setText("loop = -1 → -2 → -1 ");}
              if (c == 0) {jLabel1.setText("loop = 0 → 0");}
              if (c == 1) {jLabel1.setText("loop = 1 → 4 → 2 → 1");}
              if (c != -5) {jLabel1.setText("Loop (If Applicable)");}
                   /**if (c != -17) {jLabel1.setText("Loop (If Applicable)");}
                   if (c != -1) {jLabel1.setText("Loop (If Applicable)");}
                   if (c != 0) {jLabel1.setText("Loop (If Applicable)");}
                   if (c != 1) {jLabel1.setText("Loop (If Applicable)");}
                   **/ if (c != 1){
             
            double n;
            n = Double.parseDouble(jTextField1.getText());
           
            if (( n % 2 ) == 0) {
      
                      try{
    Thread.sleep(50); // Sleep for 4 sec
    }
    catch(InterruptedException e){} 
                      
            double x, r; 
            x = Double.parseDouble(jTextField1.getText());
            
            r = x/2; 
            ls.add(String.valueOf(r));
            jTextField1.setText(String.valueOf(r));
           
            
    
           
            
            } 
        
           
            if (( n % 2) != 0){  
                                  try{
    Thread.sleep(50); // Sleep for 4 sec
    }
    catch(InterruptedException e){} 
                      
                double x, r; 
            x = Double.parseDouble(jTextField1.getText());
            r = (0x3*x)+1; 
            ls.add(String.valueOf(r));
            jTextField1.setText(String.valueOf(r));
     
            } 
            
    
          } 
        }                                        
                    
                    while (c != 1);  }    
        private void jTextField1ActionPerformed(java.awt.event.ActionEvent evt) {                                            
            // TODO add your handling code here:
        }                                           
    
        /**
        * @param args the command line arguments
        */
        public static void main(String args[]) {
            java.awt.EventQueue.invokeLater(new Runnable() {
                public void run() {
                    new Collatz().setVisible(true);
                }
            });
        }

    any help is appreciated
     
  2. ulbador macrumors 68000

    ulbador

    Joined:
    Feb 11, 2010
    #2
    You probably want to wrap your code in
    Code:
     tags.
     
  3. dergaderg thread starter macrumors member

    Joined:
    Feb 9, 2009
  4. jiminaus macrumors 65816

    jiminaus

    Joined:
    Dec 16, 2010
    Location:
    Sydney
    #4
    Perhaps try a Java programming forum instead of a Mac programming forum.
     
  5. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #5
    Not really.

    Your posted code doesn't compile. The first few errors are:
    Code:
    Collatz.java:21: cannot find symbol
    symbol  : variable jLabel1
    location: class Collatz
            jLabel1 = new javax.swing.JLabel();
            ^
    Collatz.java:22: cannot find symbol
    symbol  : variable jTextField1
    location: class Collatz
            jTextField1 = new javax.swing.JTextField();
            ^
    Collatz.java:23: cannot find symbol
    symbol  : variable jButton1
    location: class Collatz
            jButton1 = new javax.swing.JButton();
            ^
    
    You've either left some parts out, specifically the parts that declare the variables jLabel1, jTextField1, jButton1, and so on, or you're using some unidentified tool that automatically inserts those variables.


    There's this comment in the code:
    Code:
        /** 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.
         */
    
    which suggests you're using a Form Editor, but that's not much to go on. Googling for Java Form Editor finds a zillion hits.


    Finally, you haven't identified what the problem is.
    1. Describe what you expected to happen.
    2. Describe what actually happened.
    When I tried to compile the posted code, like so:
    Code:
    javac Collatz.java
    the first error I got was a missing '}'. After I fixed that, I got a whole bunch of errors, the first few of which I've posted above. So as an example of identifying the problem:
    1. I expected the posted code to compile.
    2. The code did not compile, instead producing the errors shown above.​
     

Share This Page