Results 1 to 8 of 8
  1. #1
    root_demon's Avatar
    root_demon is offline Member
    Join Date
    Oct 2008
    Location
    Humboldt County
    Posts
    5
    Rep Power
    0

    Smile stumpt. converting an object to a long int.

    hi, I'm new to the Java language. I'm using the Netbeans IDE vesion 6.1.


    I'm trying to convert an Object variable from a jComboBox to a long integer. I know how to convert it to a String, and I've found the code to convert that String to a long, but that doesn't work. any ideas? thank you :-)

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    8

    Default

    Java Code:
    String s = "101";
    int n = Integer.parseInt(s);
    long k = Long.parseLong(s);
    System.out.printf("n = %d  k = %d%n", n, k);

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,275
    Rep Power
    25

    Default

    that doesn't work
    Post the error message and the source line where it occured. We can NOT tell what you did wrong without seeing the error message and the code that caused the error.

  4. #4
    root_demon's Avatar
    root_demon is offline Member
    Join Date
    Oct 2008
    Location
    Humboldt County
    Posts
    5
    Rep Power
    0

    Default got it

    actually, the code from hardwired worked. thank you much for that. I was trying to use Long.getLong() and netbeans was very unhappy about that.

    this unfortunately brings me to my next problem, though . :)

    basically, I'm trying to write an alarm clock program in java using swing. I find little self made projects like this are a fun way to learn a language. in any case, I need a way to check the system time once every minute and then sleep. this is my first OOP language, so sorry if this sounds like a horribly dumb question. I realize that I can't have a continuous loop and some sort of sleep/wait function and then poll for the time, because that makes the app unresponsive. is this something that I am supposed to make event driven? and if so, know of a good resource for learning about this?


    anyway, sorry and thank you in advance :D

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,275
    Rep Power
    25

    Default

    trying to write an alarm clock program
    DO you have a design for this or at least a list of things you want the program to do?
    I need a way to check the system time
    See the System.currentTimeMillis() method

  6. #6
    root_demon's Avatar
    root_demon is offline Member
    Join Date
    Oct 2008
    Location
    Humboldt County
    Posts
    5
    Rep Power
    0

    Default

    well, I have a rough design. it's really simple stuff. it's just a few jComboBoxs to select the preferred time, and a start button. I've found the System.currentTimeMillis() and was looking for something that didn't give me the number of milliseconds since 1970, but I can work with that if needed.

    I've spent the last few hours learning how threads work in java, and I think right now that's my biggest issue. if I create a thread, then shouldn't it execute simultaneously with the main thread? a thread as far as OS design is concerned works the same from a user level as a process. let me know what you guys think I'm doing wrong;



    /*** code snippet from alarmclock.java ***/
    private void jButton1MouseClicked(java.awt.event.MouseEvent evt) {

    long currenttime, selectedtime;
    Object selectedhour, selectedminute, selectedsuffix;
    String str;

    currenttime = System.currentTimeMillis();

    System.out.println(currenttime);

    selectedhour = jComboBox1.getSelectedItem();
    selectedminute = jComboBox2.getSelectedItem();
    selectedsuffix = jComboBox3.getSelectedItem();


    str = String.valueOf(selectedhour);
    selectedtime = Long.parseLong(str);
    selectedtime = ((((selectedtime*12)*60)*60)*60);

    System.out.println(selectedtime);


    /////// creating the thread right here //////

    alarmthread p = new alarmthread();
    try {
    p.run();
    } catch (InterruptedException ex) {
    Logger.getLogger(alarmclock.class.getName()).log(L evel.SEVERE, null, ex);
    }



    int k = 20;

    while(k>=1) {
    System.out.println("hello from jbutton");
    k--;
    }

    }

    }



    /*** code snippet from alarmthread.java ***/
    public class alarmthread {
    public void run() throws InterruptedException {
    int k = 20;

    while(k>=0) {
    System.out.println("hello from run");
    k--;
    }
    }

    }







    sorry, it's messy and only chunks of the project. now when I compile and run this, shouldn't I get interchanging "hello from run" / "hello from jbutton" messages? instead I get 20 runs and then 20 jbuttons. I've looked through a few different online tutorials tonight on thread creation, and even went to Sun's website, but they all have the same basic example.

  7. #7
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    8

    Default

    is this something that I am supposed to make event driven?
    Yes. You can use a Timer or a Swing Timer for Swing EventDispatchThread–savy applications/use. Or you can use Threads. I started with timers which seemed easier in the beginning. Then I saw code snippets of Thread use on the Sun forums and started experimenting. They're much easier to work with once you catch on.
    and if so, know of a good resource for learning about this?
    Difficult to say. It's a wild/fascinating subject area. Even the designers at Sun struggle with it, eg, the ongoing saga of SwingWorker and the recent (j2se 1.4) advice to start all apps with invokeLater.
    Try JavaWorld for articles such as
    Programming Java threads in the real world, Part 1
    and the tutorial basics
    Lesson: Concurrency
    and the Swing addition
    Lesson: Concurrency in Swing.
    Java Code:
    import java.awt.*;
    import javax.swing.*;
    
    public class ThreadCounting implements Runnable {
        Thread thread;
        long delay = 1000;
        boolean running = false;
        JLabel label;
        long maxCount = 25;
        int count;
    
        public void run() {
            long start = System.currentTimeMillis();
            while(running) {
                try {
                    Thread.sleep(delay);
                } catch(InterruptedException e) {
                    running = false;
                }
                count++;
                label.setText(String.valueOf(count));
                if(count >= maxCount) {
                    stop();
                }
            }
            long stop = System.currentTimeMillis();
            System.out.printf("elapsed time = %.2f%n",
                               (stop - start)/1000.0);
        }
    
        private void start() {
            if(!running) {
                count = 0;
                running = true;
                thread = new Thread(this);
                thread.setPriority(Thread.NORM_PRIORITY);
                thread.start();
            }
        }
    
        private void stop() {
            running = false;
            if(thread != null) {
                thread.interrupt();
            }
            thread = null;
        }
    
        private JLabel getLabel() {
            label = new JLabel();
            label.setHorizontalAlignment(JLabel.CENTER);
            label.setFont(label.getFont().deriveFont(24f));
            return label;
        }
    
        public static void  main(String[] args) {
            ThreadCounting test = new ThreadCounting();
            JFrame f = new JFrame();
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.add(test.getLabel());
            f.setSize(200,100);
            f.setLocationRelativeTo(null);
            f.setVisible(true);
            test.start();
        }
    }

  8. #8
    root_demon's Avatar
    root_demon is offline Member
    Join Date
    Oct 2008
    Location
    Humboldt County
    Posts
    5
    Rep Power
    0

    Default

    awesome. thank you again hardwired. that code is a great example, and the links look very promising.


    I have a night full of reading ahead of me. :)

Similar Threads

  1. Calculate sum of long integer!
    By Julingo in forum New To Java
    Replies: 2
    Last Post: 09-09-2008, 11:50 PM
  2. Converting object to string
    By Preethi in forum New To Java
    Replies: 4
    Last Post: 06-14-2008, 03:29 AM
  3. how to post a long text in an editor
    By bachtoutou in forum New To Java
    Replies: 3
    Last Post: 06-06-2008, 02:00 AM
  4. It takes very long time.....
    By iresha in forum Advanced Java
    Replies: 6
    Last Post: 05-11-2008, 02:31 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
  •