Page 1 of 2 12 LastLast
Results 1 to 20 of 23
Like Tree3Likes

Thread: Towers of hanoi

  1. #1
    faraa is offline Member
    Join Date
    Mar 2013
    Posts
    70
    Rep Power
    0

    Default Towers of hanoi

    hi.
    Can you help me?
    Why an error occur in this line:
    Java Code:
    n = Integer.parseInt(st);
    Java Code:
        
    package methodes;
    
    import java.awt.Font;
    import java.awt.Graphics;
    import javax.swing.JApplet;
    import javax.swing.JTextArea;
    
    public class siohaft extends JApplet {
    
        public void init() {
            int n;
            String st = new String();
            setSize(200, 200);
            setLayout(null);
            JTextArea jta = new JTextArea();
            jta.setBounds(getWidth() / 2 - 25, getHeight() / 2 - 10, 100, 30);
            add(jta);
            st = jta.getText();
            n = Integer.parseInt(st);
            hanoi('1','2','3',n);
    
    
        }
    public void hanoi(char s , char te , char ta , int n ){
        if (n==1){
            System.out.println(""+ n + ">>>>>" + ta);
            return ;
        }
        
        hanoi(s,ta,ta,n-1);
        return;
    }
    
        public void paint(Graphics g) {
            super.paint(g);
            g.setFont(new Font("Aria1", Font.BOLD, 15));
            g.drawString("Enter The Number Of Discs:", 25, 75);
    
    
        }
    }

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,240
    Rep Power
    19

    Default Re: Towers of hanoi

    Quote Originally Posted by faraa View Post
    Why an error occur in this line:
    Java Code:
    n = Integer.parseInt(st);
    When seeking help with an error, always post the entire stack trace. Without that, we might be merely guessing, and that isn't fun.

    Some comments, in no particular order:
    -- it's rarely, if ever, necessary or even desirable to initialize a String variable to new String(). You can declare the variable where it's assigned:
    Java Code:
    String st = jta.getText();
    -- an explicit return isn't needed at the end of a method that doesn't return a value (i.e. 'void')
    -- have you heard of JLabel? overriding a painting method to display a single line of text isn't a standard approach to creating a GUI.
    -- learn to use layout managers in favor of a null layout and setBounds(...)
    -- what text does jta contain at the point in the code where you extract it to the variable st?
    -- indent code consistently. Your entire hanoi method is too far left. See Code Conventions for the Java Programming Language: Contents
    -- don't override paint(...) of a top level window. See this tutorial for the correct approach: Lesson: Performing Custom Painting (The Java™ Tutorials > Creating a GUI With JFC/Swing)
    -- wherever possible, avoid constructing objects in a painting method override. Since only one Font is ever used, it can be declared and initialized as a static final variable.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    faraa is offline Member
    Join Date
    Mar 2013
    Posts
    70
    Rep Power
    0

    Default Re: Towers of hanoi

    Thank you so much
    Errors:
    java.lang.NumberFormatException: For input string: ""
    at java.lang.NumberFormatException.forInputString(Num berFormatException.java:65)
    at java.lang.Integer.parseInt(Integer.java:504)
    at java.lang.Integer.parseInt(Integer.java:527)
    at methodes.siohaft.init(siohaft.java:19)
    at sun.applet.AppletPanel.run(AppletPanel.java:435)
    at java.lang.Thread.run(Thread.java:722)

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,525
    Blog Entries
    7
    Rep Power
    20

    Default Re: Towers of hanoi

    It isn't your problem (yet) but that method that is trying to solve the Hanoi puzzle isn't correct. w.r.t. your current problem: your program is trying to fetch text before the user could type anything (so the text String is empty).

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    faraa is offline Member
    Join Date
    Mar 2013
    Posts
    70
    Rep Power
    0

    Default Re: Towers of hanoi

    Thank you so much
    Can you guide me...?How can I fetch text after user typed the text?

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,525
    Blog Entries
    7
    Rep Power
    20

    Default Re: Towers of hanoi

    Quote Originally Posted by faraa View Post
    Thank you so much
    Can you guide me...?How can I fetch text after user typed the text?
    Think about it: you can't know when a user has stopped typing text. You need a user gesture telling your program that it is ok to fetch the text; maybe a button can do the job: when the user presses the button, s/he indicates that the text is ready to be processed; read the API documentation for the JButton class for details.

    kind regards,

    Jos
    faraa likes this.
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    faraa is offline Member
    Join Date
    Mar 2013
    Posts
    70
    Rep Power
    0

    Default Re: Towers of hanoi

    Thank you so much again.
    I think understood...

  8. #8
    faraa is offline Member
    Join Date
    Mar 2013
    Posts
    70
    Rep Power
    0

    Default Re: Towers of hanoi

    Can you help me again?
    1-Why don't send n to hanoi function ?
    2-Which function can I use instead of JOptinPane?


    Java Code:
    public class siohaft extends JApplet implements ActionListener {
    
        JButton jb;
        JTextArea jta;
    
        public void init() {
            int n ;
            setSize(200, 200);
            setLayout(null);
            jta = new JTextArea();
            jta.setBounds(getWidth() / 2 - 25, getHeight() / 2 - 10, 100, 30);
            add(jta);
            jb = new JButton("OK");
            jb.setBounds(getWidth() / 2 - 5, getHeight() / 2 + 30, 60, 30);
            add(jb);
           
        }
    
        public void hanoi(int n, char s, char t, char f) {
            if (n == 1) {
                JOptionPane.showMessageDialog(null, "" + s + ">>>>>" + f);
            } 
            else {
                hanoi(n - 1, s, f, t);
                JOptionPane.showMessageDialog(null, "" + s + ">>>>>" + f);
                hanoi(n - 1, t, s, f);
    
            }
        }
    
        @Override
        public void paint(Graphics g) {
            super.paint(g);
            g.setFont(new Font("Aria1", Font.BOLD, 15));
            g.drawString("Enter The Number Of Discs:", 25, 75);
        }
    
        @Override
        public void actionPerformed(ActionEvent ae) {
            int n;
            String st = jta.getText();
            n = Integer.parseInt(st);
           hanoi(n,'1','2','3');
    
            
        }
    }

  9. #9
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,525
    Blog Entries
    7
    Rep Power
    20

    Default Re: Towers of hanoi

    Your 'siohaft' class implements the ActionListener interface but you don't register it at your 'jb' JButton; add one line near the end of your init() method:

    Java Code:
    jb.addActionListener(this);
    kind regards,

    Jos
    faraa likes this.
    cenosillicaphobia: the fear for an empty beer glass

  10. #10
    faraa is offline Member
    Join Date
    Mar 2013
    Posts
    70
    Rep Power
    0

    Default Re: Towers of hanoi

    Quote Originally Posted by JosAH View Post
    Your 'siohaft' class implements the ActionListener interface but you don't register it at your 'jb' JButton; add one line near the end of your init() method:

    Java Code:
    jb.addActionListener(this);
    kind regards,

    Jos




    I do that.
    But it has problem yet in this line:
    Java Code:
     n = Integer.parseInt(st);
    I can't really understand.


    Java Code:
    package methodes;
    
    import java.awt.Font;
    import java.awt.Graphics;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.JApplet;
    import javax.swing.JButton;
    import javax.swing.JOptionPane;
    import javax.swing.JTextArea;
    
    public class siohaft extends JApplet implements ActionListener {
    int n ;
        JButton jb;
        JTextArea jta;
    
        public void init() {
            
            setSize(200, 200);
            setLayout(null);
            jta = new JTextArea();
            jta.setBounds(getWidth() / 2 - 25, getHeight() / 2 - 10, 100, 30);
            add(jta);
            jb = new JButton("OK");
            jb.setBounds(getWidth() / 2 - 5, getHeight() / 2 + 30, 60, 30);
            jb.addActionListener(this);
            add(jb);
            
      
    
        }
    
        public void hanoi(int n, char s, char t, char f) {
            if (n == 1) {
                JOptionPane.showMessageDialog(null, "" + s + ">>>>>" + f);
            } 
            else {
                hanoi(n - 1, s, f, t);
                JOptionPane.showMessageDialog(null, "" + s + ">>>>>" + f);
                hanoi(n - 1, t, s, f);
    
            }
        }
    
        @Override
        public void paint(Graphics g) {
            super.paint(g);
            g.setFont(new Font("Aria1", Font.BOLD, 15));
            g.drawString("Enter The Number Of Discs:", 25, 75);
        }
    
        @Override
        public void actionPerformed(ActionEvent ae) {
           
            String st = jta.getText();
            n = Integer.parseInt(st);
            hanoi(n,'1','2','3');      
        }
    }

  11. #11
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,525
    Blog Entries
    7
    Rep Power
    20

    Default Re: Towers of hanoi

    Quote Originally Posted by faraa View Post
    But it has problem yet in this line:
    Java Code:
     n = Integer.parseInt(st);
    And what might this problem be?

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  12. #12
    faraa is offline Member
    Join Date
    Mar 2013
    Posts
    70
    Rep Power
    0

    Default Re: Towers of hanoi

    an error:
    Exception in thread "AWT-EventQueue-1" java.lang.NumberFormatException: For input string: "5
    "
    at java.lang.NumberFormatException.forInputString(Num berFormatException.java:65)
    at java.lang.Integer.parseInt(Integer.java:492)
    at java.lang.Integer.parseInt(Integer.java:527)
    at methodes.siohaft.actionPerformed(siohaft.java:56)
    at javax.swing.AbstractButton.fireActionPerformed(Abs tractButton.java:2018)
    at javax.swing.AbstractButton$Handler.actionPerformed (AbstractButton.java:2341)
    at javax.swing.DefaultButtonModel.fireActionPerformed (DefaultButtonModel.java:402)
    at javax.swing.DefaultButtonModel.setPressed(DefaultB uttonModel.java:259)
    at javax.swing.plaf.basic.BasicButtonListener.mouseRe leased(BasicButtonListener.java:252)
    at java.awt.Component.processMouseEvent(Component.jav a:6505)
    at javax.swing.JComponent.processMouseEvent(JComponen t.java:3321)
    at java.awt.Component.processEvent(Component.java:627 0)
    at java.awt.Container.processEvent(Container.java:222 9)
    at java.awt.Component.dispatchEventImpl(Component.jav a:4861)
    at java.awt.Container.dispatchEventImpl(Container.jav a:2287)
    at java.awt.Component.dispatchEvent(Component.java:46 87)
    at java.awt.LightweightDispatcher.retargetMouseEvent( Container.java:4832)
    at java.awt.LightweightDispatcher.processMouseEvent(C ontainer.java:4492)
    at java.awt.LightweightDispatcher.dispatchEvent(Conta iner.java:4422)
    at java.awt.Container.dispatchEventImpl(Container.jav a:2273)
    at java.awt.Component.dispatchEvent(Component.java:46 87)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.j ava:729)
    at java.awt.EventQueue.access$200(EventQueue.java:103 )
    at java.awt.EventQueue$3.run(EventQueue.java:688)
    at java.awt.EventQueue$3.run(EventQueue.java:686)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPri vilege(ProtectionDomain.java:76)
    at java.security.ProtectionDomain$1.doIntersectionPri vilege(ProtectionDomain.java:87)
    at java.awt.EventQueue$4.run(EventQueue.java:702)
    at java.awt.EventQueue$4.run(EventQueue.java:700)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPri vilege(ProtectionDomain.java:76)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java: 699)
    at java.awt.EventDispatchThread.pumpOneEventForFilter s(EventDispatchThread.java:242)
    at java.awt.EventDispatchThread.pumpEventsForFilter(E ventDispatchThread.java:161)
    at java.awt.EventDispatchThread.pumpEventsForHierarch y(EventDispatchThread.java:150)
    at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:146)
    at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:138)
    at java.awt.EventDispatchThread.run(EventDispatchThre ad.java:91)

    after clicking on jbutton these errors occurd

  13. #13
    faraa is offline Member
    Join Date
    Mar 2013
    Posts
    70
    Rep Power
    0

    Default Re: Towers of hanoi

    Sorry
    No error occurd
    Thank you

  14. #14
    faraa is offline Member
    Join Date
    Mar 2013
    Posts
    70
    Rep Power
    0

    Default Re: Towers of hanoi

    Thank you so much again
    And Can you guide me what can I use instead of JOptionPane?

  15. #15
    faraa is offline Member
    Join Date
    Mar 2013
    Posts
    70
    Rep Power
    0

    Default Re: Towers of hanoi

    I mean that for example can I use Drawstring ?
    How ?
    because if I use repaint we can see just the latest answer

  16. #16
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,525
    Blog Entries
    7
    Rep Power
    20

    Default Re: Towers of hanoi

    Quote Originally Posted by faraa View Post
    Thank you so much again
    And Can you guide me what can I use instead of JOptionPane?
    You can use any of the Swing JComponents (e.g. have a look at the JTextField instead of the JTextArea). It's all up to your imagination and creativity.

    kind regards,

    Jos
    faraa likes this.
    cenosillicaphobia: the fear for an empty beer glass

  17. #17
    faraa is offline Member
    Join Date
    Mar 2013
    Posts
    70
    Rep Power
    0

    Default Re: Towers of hanoi

    Thank's a lot
    I used JTextArea instead of JOptionPane.But I can't add scrollPane to that .
    Can you help me?

  18. #18
    faraa is offline Member
    Join Date
    Mar 2013
    Posts
    70
    Rep Power
    0

    Default Re: Towers of hanoi

    Java Code:
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    package methodes;
    
    import java.awt.Container;
    import java.awt.FlowLayout;
    import java.awt.Font;
    import java.awt.Graphics;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.JApplet;
    import javax.swing.JButton;
    import javax.swing.JScrollPane;
    import javax.swing.JTextArea;
    
    public class SioHaftb extends JApplet implements ActionListener{
    
      int n;
        JButton jb;
        JTextArea jta;
        JTextArea jta2;
        
        public void init() {
            
            setSize(200, 200);
           setLayout(null);
         
           
            jta = new JTextArea();
            jta.setBounds(getWidth() / 2 - 25, getHeight() / 2 - 10, 100, 20);
           
            
            jb = new JButton("OK");
            jb.setBounds(getWidth() / 2 - 5, getHeight() / 2 + 30, 60, 30);
            jb.addActionListener(this);
            
            Container c = getContentPane();
           
            c.add(jta);
            String st = "";
            jta2 = new JTextArea(st,getWidth() / 2 - 25, getHeight() / 2 + 70);
            c.add(new JScrollPane(jta2));
            
            c.add(jb);
           // c.add(jsp);
            
            
            
        }
        
        public void hanoi(int n, char s, char t, char f) {
            if (n == 1) {
                //JOptionPane.showMessageDialog(null, "" + s + ">>>>>" + f);
                // jta2.setText("" + s + ">>>>>" + f);
                jta2.append("" + s + ">>>>>" + f + "\n");
            } else {
                hanoi(n - 1, s, f, t);
                // JOptionPane.showMessageDialog(null, "" + s + ">>>>>" + f);
                //jta2.setText("" + s + ">>>>>" + f);
                jta2.append("" + s + ">>>>>" + f + "\n");
                hanoi(n - 1, t, s, f);
                
            }
        }
        
        @Override
        public void paint(Graphics g) {
            super.paint(g);
            g.setFont(new Font("Aria1", Font.BOLD, 15));
            g.drawString("Enter The Number Of Discs:", 25, 75);
        }
        
        @Override
        public void actionPerformed(ActionEvent ae) {
            
            String st = jta.getText();
            
            n = Integer.parseInt(st);
            hanoi(n, '1', '2', '3');
        }
    }
    I wrote this code.
    But second JTextArea and it scroll don't show.
    Can you help me?

  19. #19
    faraa is offline Member
    Join Date
    Mar 2013
    Posts
    70
    Rep Power
    0

    Default Re: Towers of hanoi

    Where are you my friends?
    Could you help me?

  20. #20
    faraa is offline Member
    Join Date
    Mar 2013
    Posts
    70
    Rep Power
    0

    Default Re: Towers of hanoi

    OoooOOuUUuUUhhhhh!!!!!!!!!!!!!!!
    I understood that it depends on kind of setLayout
    I tried many of Setlayouts.But just in gridlayout scrollpane can show.
    But It is not beautiful.
    Please help me .....
    pliiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiz....

Page 1 of 2 12 LastLast

Similar Threads

  1. Hanoi- HELP
    By xajaxworldx in forum Advanced Java
    Replies: 2
    Last Post: 05-06-2011, 05:22 AM
  2. Towers of hanoi Jpanels not displaying during solution
    By wassat676 in forum AWT / Swing
    Replies: 3
    Last Post: 05-06-2011, 04:11 AM
  3. Towers of Hanoi
    By myst in forum New To Java
    Replies: 1
    Last Post: 07-11-2010, 06:24 PM
  4. Replies: 5
    Last Post: 04-28-2010, 04:42 PM
  5. Towers of Hanoi using Producer/Consumer Pattern
    By dooey in forum New To Java
    Replies: 0
    Last Post: 09-08-2009, 12:45 PM

Posting Permissions

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