Results 1 to 5 of 5

Thread: parseInt

  1. #1
    trefoil is offline Member
    Join Date
    Sep 2009
    Posts
    3
    Rep Power
    0

    Red face parseInt

    hi i need some guidance on this coding...

    Java Code:
    package q2;
    
    import java.applet.Applet;
    import java.awt.*;
    import javax.swing.JOptionPane;
    
    
    //import javax.swing.JFrame;
    
    
    public class DrawObjects extends Applet {
    
        public void init() {
          setSize(400,200);
          setBackground( Color.black );
        }
        public void paint(Graphics g) {
            g.setColor(Color.green);
                 //Object[] possibleValues = { "1", "2", "3" };
                //Object selectedValue = JOptionPane.showInputDialog(null, "Choose one", "Input", JOptionPane.INFORMATION_MESSAGE, null, possibleValues, possibleValues[0]);
        
    
    String inp = JOptionPane.showInputDialog(null, "Enter 1 to draw lines\nEnter 2 to draw rectangles\nEnter 3 to draw circles","Draw Objects", JOptionPane.CANCEL_OPTION);
    int num = Integer.parseInt(inp);
    
    if (num != int)
    {
        JOptionPane.showMessageDialog(null, "Please enter a valid number\nPlease try again", "Invalid number!", JOptionPane.ERROR_MESSAGE);
    }
    else
    {
    
    
               switch (num) {
                    
                    case 1: {
                        for(int i = 1; i <=20; i++){
                        g.drawLine(10, 10, 400, (10*i));
                        }
                    }break;
                    case 2: {
                        for(int i = 1; i <=20; i++){
                        g.drawRect(10*i, 10*i, 10*i+40, 10*i+40);
                        }
                    }break;
                        case 3: {
                        for(int i = 1; i <=20; i++){
                        g.drawOval(10*i, 10*i, 10*i+40, 10*i+40);
                        }
                    }break;
                        default: {
                        JOptionPane.showMessageDialog(null, "You have entered an Invalid number\nPlease try again", "Invalid number!", JOptionPane.ERROR_MESSAGE);
                          //JOptionPane.showMessageDialog(null, "Thank You");
                          //System.exit(0);
                        break;
                    //System.out.println("Invalid value.");
                    
                       
    
                        }
                } JOptionPane.showMessageDialog(null, "Thank You");
    
    }
        }
    
                }

    First of all, I am trying to dissappear the JOptionPane.showInputDialog but it keeps looping twice.

    2ndly, I am wondering how do i put if (inp != int) the int, is there other term to put it as numbers or anyother than numbers.

    Thanks and appreciate in advance.
    Last edited by Fubarable; 09-08-2009 at 11:14 PM. Reason: code tags added to aid readability

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

    Default

    Oh my. You appear to be calling your methods from within the applet's paint method which is a big no-no as the paint method is just for painting. I advise you to go through the Sun applet tutorial to learn how their supposed to be coded.

    Also, if you want the user to "go again" you'll need to use a loop of some sort, perhaps a while loop or a do/while loop.

  3. #3
    trefoil is offline Member
    Join Date
    Sep 2009
    Posts
    3
    Rep Power
    0

    Default

    oh, this is just a switch case tutorial to do.
    is there a shorter way to this?

  4. #4
    trefoil is offline Member
    Join Date
    Sep 2009
    Posts
    3
    Rep Power
    0

    Default

    dear debuggers, i have managed to solve this by declaring strings and integer in the class instead in graphics object.

    thanks for viewing.

  5. #5
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    7

Similar Threads

  1. ParseInt question
    By McChill in forum New To Java
    Replies: 1
    Last Post: 03-09-2009, 09:34 PM
  2. [SOLVED] Integer.parseInt() problem
    By eggmanpete in forum New To Java
    Replies: 18
    Last Post: 02-15-2009, 04:25 PM
  3. parseInt() vs. intValue()
    By JavaPilot in forum New To Java
    Replies: 5
    Last Post: 02-04-2009, 08:39 AM
  4. Integer.parseInt?
    By Exhonour in forum New To Java
    Replies: 4
    Last Post: 01-20-2009, 02:31 AM
  5. Problem with Integer.parseInt()
    By Hevonen in forum New To Java
    Replies: 2
    Last Post: 12-14-2008, 03:41 AM

Tags for this Thread

Posting Permissions

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