Results 1 to 5 of 5
Thread: parseInt
- 09-08-2009, 11:01 PM #1
Member
- Join Date
- Sep 2009
- Posts
- 3
- Rep Power
- 0
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
-
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.
- 09-09-2009, 10:29 AM #3
Member
- Join Date
- Sep 2009
- Posts
- 3
- Rep Power
- 0
oh, this is just a switch case tutorial to do.
is there a shorter way to this?
- 09-09-2009, 06:12 PM #4
Member
- Join Date
- Sep 2009
- Posts
- 3
- Rep Power
- 0
dear debuggers, i have managed to solve this by declaring strings and integer in the class instead in graphics object.
thanks for viewing.
- 09-09-2009, 07:12 PM #5
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
Similar Threads
-
ParseInt question
By McChill in forum New To JavaReplies: 1Last Post: 03-09-2009, 09:34 PM -
[SOLVED] Integer.parseInt() problem
By eggmanpete in forum New To JavaReplies: 18Last Post: 02-15-2009, 04:25 PM -
parseInt() vs. intValue()
By JavaPilot in forum New To JavaReplies: 5Last Post: 02-04-2009, 08:39 AM -
Integer.parseInt?
By Exhonour in forum New To JavaReplies: 4Last Post: 01-20-2009, 02:31 AM -
Problem with Integer.parseInt()
By Hevonen in forum New To JavaReplies: 2Last Post: 12-14-2008, 03:41 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks