K , im trying make a program were when u enter a number it draws a circle. The number entered is the radius (width) of the circle. "yes i know its stupid but im just messing around " > Now my problem is that if u enter a something either than a number its craps out , wondering if anyone can help nme out on this. Heres my code , its does not compile due to a few errors . Appreciate any help giving.

import java.applet.*;
import java.awt.*;

public class Circle_drawing_GUI extends Applet
{
    Label diameter;// , heightof ;
    TextField diameter_input;// , height_input   ;
    int x , y ; 
    String width ; //;, height;
    
   
    
    public void init ()
    {
        diameter = new Label ("Enter radius");
        diameter_input = new TextField (5);
       //heightof = new Label ("Enter height");  
       // height_input = new TextField (5);
        add (diameter);///aaa
        add (diameter_input);
       // add (heightof);
       // add (height_input);
    } 
    
    public boolean action (Event e , Object o)
    {
    Graphics g = getGraphics() ;
     
     width = diameter_input.getText();
     //height =Integer.parseInt (height_input.getText());
     x = Integer.parseInt (catch_method(""));
     g.drawOval (getSize ().width / 2 , getSize().height / 2 , x , 50);
     return true ;
     }
    
    static public String catch_method (String input) // reads string input that cannot be transferred to integer
    {
     int valid ;
     
        while (true)
        {
            try
            {
                valid = Integer.parseInt (width);
                break;
            }
            catch (NumberFormatException e)
            {
                
             showStatus ("invalid input") ;
            }
        } // while
        return valid.toString();
    } //closing catch_method

}

Recommended Answers

All 3 Replies

I kinda got it working . This is the way I have it now , when user inputs anything other than a # a new console appears saying invalid input (which is what i wanted it to do ) but when u close that window the program runnig is automatically shut down. So what i need know is just to make it say at the bottom of the applet "invalid input " using the showStatus ("invalid input"); .Heres my code

import java.applet.*;
import java.awt.*;
import java.io.*;

public class Circle_drawing_GUI extends Applet
{
    Label diameter;
    TextField diameter_input;
    int x , y ; 
    String width ; 
    
   
    
    public void init ()
    {
        diameter = new Label ("Enter radius");
        diameter_input = new TextField (5);
        add (diameter);
        add (diameter_input);
    } 
    
    public boolean action (Event e , Object o)
    {
    Graphics g = getGraphics() ;
     
     width = diameter_input.getText();
     x = Integer.parseInt (catch_method(width));
     g.drawOval (getSize ().width / 2 , getSize().height / 2 , x , 50);
     return true ;
     }
    
    static public String catch_method (String input) // reads string input that cannot be transferred to integer
    {
      DataInputStream stdin = new DataInputStream (System.in);
      int valid ;
     
        while (true)
        {
            try
            {
                valid = Integer.parseInt (input);
                break;
            }
            catch (NumberFormatException e)
            {
              System.out.println ("Invalid input"); // when go to close windows program shuts down (no good)
             // System.out.close ();         Computer freezes
              
             //showStatus ("invalid input") ; // can ' t get to work
            }
        } // while
        return input;
    } //closing catch_method
 
}

help me.....i am a second year college taken up computer science...my prob is that our topic is about java but i don't sometimes get it...plz help me!!!!!

Hi rheyang16 and welcome to daniweb,

There are a couple of things to consider when posting - first this thread is 18 months old and your statement has nothing to do with the topic. Second, you have to be more specific - what do you mean by "i don't sometimes get it"? If you have a specific question we can help you, otherwise it's a bit difficult sorry!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.