Me again! This time its a loop problem


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 13 of 13

Thread: Me again! This time its a loop problem

  1. #1
    Join Date
    Oct 2005
    Posts
    15

    Me again! This time its a loop problem

    I am trying to get my head around loops now!
    Is there a way to count down from 60 seconds in a text box??
    Would I be able to use a normal loop code??

    i.e.

    int count = 10;
    System.out.println("Start!");
    while (count > 0) {
    System.out.println(count + "!");
    count = count - 1;
    }
    System.out.println("Finish!");
    }
    }

    Thanks!
    Steve

  2. #2
    Join Date
    Dec 2005
    Location
    New Jersey
    Posts
    290
    You mean like this?
    Code:
    Thread t = new Thread();
    t.start();
    
    for (int i = 60; i >= 0; i--) {
        System.out.println(i);
             try {
                 t.sleep(1000);
             } catch (InterruptedException e) {
                 e.printStackTrace();
             }
         }
    }
    Useful Links:
    Threads:
    The Java(tm) Tutorial: Threads - Doing Two or More Tasks At Once
    Java World: Introduction to Threads

    Loops:
    The Java(tm) Tutorial: The for Statement
    Java Glossary: for loop
    The Java(tm) Tutorial: The while and do-while Statements
    Java Tutorials: Looping
    The while and do..while Statements

  3. #3
    Join Date
    Oct 2005
    Posts
    15
    My program runs fine without that piece of code but when I put it in and try to run it I get a message saying I have no main class selected?!?!

  4. #4
    Join Date
    Dec 2005
    Location
    New Jersey
    Posts
    290
    Post your code.

  5. #5
    Join Date
    Dec 2004
    Location
    San Bernardino County, California
    Posts
    1,468
    Are you doing a simulation of this countdown or do you really want this to be "60 seconds".

    If this is a countdown, you can do this in a while loop, and it can be coded in a "for" loop:

    for( count = 60; count > 0; count--)
    {
    continue;
    }
    System.out.println( "Finish");

    If you are looking for real time, look at the compareTo() method of the Calendar class, or use the System.currentTimeMillis() static method.

  6. #6
    Join Date
    Oct 2005
    Posts
    15
    I took the code out and i've tinkered with something else that has stopped my program working. I am getting the following errors...

    init:
    deps-jar:
    Compiling 2 source files to Y:\CO2204\0402900\build\classes
    Y:\CO2204\0402900\src\Mars.java:27: invalid method declaration; return type required
    public Mars() {
    Y:\CO2204\0402900\src\Mars.java:31: illegal start of type
    try {
    Y:\CO2204\0402900\src\Mars.java:244: <identifier> expected
    }
    3 errors
    BUILD FAILED (total time: 0 seconds)


    I know they are quite unspecific but can anybody see a problem with this coding??
    I left out the variable declaration




    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import java.net.*;
    import java.util.*;
    import java.sql.*;

    public class Mars extends javax.swing.JFrame {

    public class Database extends javax.swing.JFrame {
    Statement statement = null;
    String database = "jdbc:mysql://devcgi.compsci.chester.ac.uk/0402900";
    //network version ******* = your student number
    // String database = "jdbc:mysql://localhost/test"; // syntax for own computer

    public Mars() {
    initComponents();
    }

    try {
    // Step 1: Load the JDBC driver.
    Class.forName("com.mysql.jdbc.Driver");
    // Step 2: Establish the connection to the database.
    Connection conn = DriverManager.getConnection(database,"0402900","0402900");
    // ******* is your student number
    System.out.println("Step 2 ... fine");
    statement = conn.createStatement();
    } catch (Exception e) {
    System.err.println("Got an exception at Step 1 or 2! ");
    System.err.println(e.getMessage());

    /** Creates new form Mars */

    // <editor-fold defaultstate="collapsed" desc=" Generated Code ">
    private void initComponents() {
    jDialog1 = new javax.swing.JDialog();
    jPanel1 = new javax.swing.JPanel();
    lblTitle = new javax.swing.JLabel();
    lblMission = new javax.swing.JLabel();
    lblJob = new javax.swing.JLabel();
    lblName = new javax.swing.JLabel();
    cmdCancel = new javax.swing.JButton();
    cmdOk = new javax.swing.JButton();
    txtMission = new javax.swing.JTextField();
    txtJob = new javax.swing.JTextField();
    txtName = new javax.swing.JTextField();
    lblEmblem = new javax.swing.JLabel();
    cmdCreate = new javax.swing.JButton();
    jPanel2 = new javax.swing.JPanel();
    cmdMission = new javax.swing.JButton();
    jPanel3 = new javax.swing.JPanel();
    txtCount = new javax.swing.JTextField();

    jDialog1.getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

    jPanel1.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

    lblTitle.setFont(new java.awt.Font("Century Gothic", 1, 14));
    lblTitle.setText("Mission Information");
    jPanel1.add(lblTitle, new org.netbeans.lib.awtextra.AbsoluteConstraints(60, 10, -1, -1));

    lblMission.setFont(new java.awt.Font("Century Gothic", 0, 12));
    lblMission.setText("Mission Number");
    jPanel1.add(lblMission, new org.netbeans.lib.awtextra.AbsoluteConstraints(40, 180, -1, -1));

    lblJob.setFont(new java.awt.Font("Century Gothic", 0, 12));
    lblJob.setText("Job Title");
    jPanel1.add(lblJob, new org.netbeans.lib.awtextra.AbsoluteConstraints(40, 150, -1, -1));

    lblName.setFont(new java.awt.Font("Century Gothic", 0, 12));
    lblName.setText("Name");
    jPanel1.add(lblName, new org.netbeans.lib.awtextra.AbsoluteConstraints(40, 120, -1, -1));

    cmdCancel.setText("Cancel");
    cmdCancel.addActionListener(new java.awt.event.ActionListener() {
    public void actionPerformed(java.awt.event.ActionEvent evt) {
    cmdCancelActionPerformed(evt);
    }
    });

    jPanel1.add(cmdCancel, new org.netbeans.lib.awtextra.AbsoluteConstraints(130, 220, 80, -1));

    cmdOk.setText("Ok");
    cmdOk.addActionListener(new java.awt.event.ActionListener() {
    public void actionPerformed(java.awt.event.ActionEvent evt) {
    cmdOkActionPerformed(evt);
    }
    });

    jPanel1.add(cmdOk, new org.netbeans.lib.awtextra.AbsoluteConstraints(40, 220, 80, -1));

    jPanel1.add(txtMission, new org.netbeans.lib.awtextra.AbsoluteConstraints(140, 180, 70, -1));

    jPanel1.add(txtJob, new org.netbeans.lib.awtextra.AbsoluteConstraints(140, 150, 70, -1));

    txtName.addActionListener(new java.awt.event.ActionListener() {
    public void actionPerformed(java.awt.event.ActionEvent evt) {
    txtNameActionPerformed(evt);
    }
    });

    jPanel1.add(txtName, new org.netbeans.lib.awtextra.AbsoluteConstraints(140, 120, 70, -1));

    jPanel1.add(lblEmblem, new org.netbeans.lib.awtextra.AbsoluteConstraints(90, 70, -1, -1));

    cmdCreate.setText("Create");
    cmdCreate.addActionListener(new java.awt.event.ActionListener() {
    public void actionPerformed(java.awt.event.ActionEvent evt) {
    cmdCreateActionPerformed(evt);
    }
    });

    jPanel1.add(cmdCreate, new org.netbeans.lib.awtextra.AbsoluteConstraints(90, 250, -1, -1));

    jDialog1.getContentPane().add(jPanel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 0, 240, 270));

    getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
    jPanel2.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

    cmdMission.setText("Mission Info");
    cmdMission.addActionListener(new java.awt.event.ActionListener() {
    public void actionPerformed(java.awt.event.ActionEvent evt) {
    cmdMissionActionPerformed(evt);
    }
    });

    jPanel2.add(cmdMission, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 340, 110, 20));

    jPanel3.setBorder(new javax.swing.border.LineBorder(new java.awt.Color(0, 0, 0)));
    jPanel2.add(jPanel3, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 10, 480, 280));

    txtCount.addActionListener(new java.awt.event.ActionListener() {
    public void actionPerformed(java.awt.event.ActionEvent evt) {
    txtCountActionPerformed(evt);
    }
    });

    jPanel2.add(txtCount, new org.netbeans.lib.awtextra.AbsoluteConstraints(130, 340, 30, -1));

    getContentPane().add(jPanel2, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 10, 490, 360));

    pack();
    }
    // </editor-fold>

    private void cmdCreateActionPerformed(java.awt.event.ActionEvent evt) {
    String str = "CREATE TABLE " + txtTableName.getText()+ " (Name VARCHAR(20), Job Title VARCHAR(20). Mission Number(20))";
    try{
    statement.executeUpdate(str);
    System.out.println("Table " + txtTableName.getText() + " Created !");
    } catch(Exception e){
    e.printStackTrace();
    }
    }

    private void txtCountActionPerformed(java.awt.event.ActionEvent evt) {

    }

    private void txtNameActionPerformed(java.awt.event.ActionEvent evt) {
    // TODO add your handling code here:
    }

    private void cmdOkActionPerformed(java.awt.event.ActionEvent evt) {
    String str = "INSERT INTO " + txtTableName.getText()+ " VALUES('" + txtName.getText() + "','" + txtJob.getText() + "','" + txtMission.getText() + "')";
    try{
    statement.executeUpdate(str);
    System.out.println("Insert Performed !");
    } catch(Exception e){
    e.printStackTrace();
    }
    }
    setVisible(false);
    confirm = true;

    }

    private void cmdCancelActionPerformed(java.awt.event.ActionEvent evt) {
    setVisible(false);
    }

    private void cmdMissionActionPerformed(java.awt.event.ActionEvent evt) {
    Dialog d = new Dialog(this,true); // the modal argument is set to true
    d.setVisible(true);
    txtName.setText(d.getName());
    }


    public void handleButton(ActionEvent evt) {
    Icon icon = new ImageIcon(getClass().getResource("PE7401.jpeg"));
    lblEmblem.setIcon(icon);



    }


    /**
    * @param args the command line arguments
    */{
    boolean confirm = false;
    public static void main(String args[]) {
    java.awt.EventQueue.invokeLater(new Runnable() {
    public void run() {
    new Mars().setVisible(true);
    }
    });
    }

  7. #7
    Join Date
    Dec 2005
    Location
    New Jersey
    Posts
    290
    Why do you have a Database class nested inside your Mars class.

    You have a lot of bracket errors. Go through all your brackets to make sure everything is in a method.

    Also, you have spaces in the word ActionEvent.. you might wanna get rid of them.

    One more thing.. I'm hoping you have more code than that because I think every single one of your variables has not been defined.
    Last edited by destin; 01-16-2006 at 10:10 AM.

  8. #8
    Join Date
    Oct 2005
    Posts
    15
    Because I have a form that is using SQL

    Yeah i didnt post my variables.

    Thanks!

  9. #9
    Join Date
    Oct 2005
    Posts
    15
    Ok I ahve sorted all of my problems out and Im back to the loop!
    When I put that code in for the text box and run the program I still have a blank text box ??

    Quote Originally Posted by destin
    You mean like this?
    Code:
    Thread t = new Thread();
    t.start();
    
    for (int i = 60; i >= 0; i--) {
        System.out.println(i);
             try {
                 t.sleep(1000);
             } catch (InterruptedException e) {
                 e.printStackTrace();
             }
         }
    }
    Useful Links:
    Threads:
    The Java(tm) Tutorial: Threads - Doing Two or More Tasks At Once
    Java World: Introduction to Threads

    Loops:
    The Java(tm) Tutorial: The for Statement
    Java Glossary: for loop
    The Java(tm) Tutorial: The while and do-while Statements
    Java Tutorials: Looping
    The while and do..while Statements

  10. #10
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560

    A basic ticker...

    Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    
    class CountDownTF extends JTextField implements ActionListener {
      Timer t=null;
      private int secondsLeft=-1;
    
      public CountDownTF () {
        t=new Timer(1000,this);
      }
      public void setCountdownTime(int secondsLeft) {
        setText(Integer.toString(secondsLeft));
        this.secondsLeft=secondsLeft;
      }
      public void start() {
        if (!t.isRunning())
          t.start();
      }
      public void stop() {
        if (t.isRunning()) t.stop();
      }
      public void actionPerformed(ActionEvent e) {
        secondsLeft--;
        if (secondsLeft == 0)
          stop();
    
        setText(Integer.toString(secondsLeft));
      }
    
    }
    eschew obfuscation

  11. #11
    Join Date
    Dec 2005
    Posts
    21
    Code:
    Thread t = new Thread();
    t.start();
    
    for (int i = 60; i >= 0; i--) {
        System.out.println(i);
             try {
                 t.sleep(1000);
             } catch (InterruptedException e) {
                 e.printStackTrace();
             }
         }
    }
    Does that code even work? Don't you need to assign the thread to the main class, or implement or extend Runnable? It looks like that would create a new thread, run stuff and tell that thread to sleep. However, the action is going on outside of the thread, so I would think it would continue while the thread sleeps, and then it will try to make an already sleeping thread sleep, probably throwing an exception...

    I'm pretty sure you can simply control the main thread by reffering to it as Thread... ie:
    Code:
    try {
       for (int i=0; i < 5; i++) {
          System.out.println(i);
          Thread.sleep(1000);
       }
    } catch (InterruptedException e) {
       System.out.println("Main thread interrupted");
    }
    Alternatively you could keep how you have t sleeping, but instead of creating a new thread, you can assign it to the main thread by:
    Code:
    Thread t = Thread.currentThread();

  12. #12
    Join Date
    Jan 2006
    Posts
    53
    Thread.sleep is static. You can create a bazillion instances of thread, and call any of their .sleep() methods, and you'll still be calling the Thread.sleep() method in the class, not in that instance.

    Calling Thread.sleep() tells the CURRENT thread to sleep. In the example of making a new Thread and putting it into t, then calling t.sleep(), this has no affect whatsoever on how t runs.
    Last edited by Laszlo; 01-25-2006 at 03:31 PM.

  13. #13
    Join Date
    Dec 2005
    Location
    New Jersey
    Posts
    290
    Quote Originally Posted by smartr
    Does that code even work?
    Well, there's an extra bracket, but w/o that, yeah it works.

    But yeah, it would be better to just do Thread.sleep; and get rid of Thread t.
    Last edited by destin; 01-25-2006 at 03:37 PM.

Similar Threads

  1. How do you add multiple time stings?
    By Code_Writer in forum VB Classic
    Replies: 11
    Last Post: 08-14-2005, 08:50 PM
  2. Reliability Problem
    By elise in forum Java
    Replies: 0
    Last Post: 10-30-2002, 04:40 AM
  3. Reliability Problem
    By elise in forum .NET
    Replies: 0
    Last Post: 10-30-2002, 04:39 AM
  4. OO4O - Needs help me with LONG RAW problem please?
    By Ofir in forum oracle.general
    Replies: 0
    Last Post: 06-24-2002, 05:28 AM
  5. Replies: 2
    Last Post: 11-30-2000, 03:23 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
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center