Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    Banned
    Join Date
    Aug 2002
    Posts
    111
    Thanks
    0
    Thanked 0 Times in 0 Posts

    java codes problem

    Hi could you help me this scirpt.
    I got a error I don't know why.

    i can't run this code



    PHP Code:
    import javax.swing.JOptionPane;
    public class 
    ito
    {
     public static 
    void main(String[] args)
     {

      
    int number JOptionPane.showInputDialog("Enter number: ");
      switch(
    number)
      {
        case 
    0:
             
    System.out.println("you typed zero");
             break;
        case 
    1:
         
    System.out.println("you typed one");
             break;
        default:
              
    System.out.println("you didn't type zero or one");
             break;


         }

         
    System.exit(0);

         }} 

  • #2
    Supreme Overlord Spookster's Avatar
    Join Date
    May 2002
    Location
    Marion, IA USA
    Posts
    6,273
    Thanks
    4
    Thanked 83 Times in 82 Posts
    It might be nice to know what the error was that the compiler gave you.
    Spookster
    CodingForums Supreme Overlord
    All Hail Spookster

  • #3
    Banned
    Join Date
    Aug 2002
    Posts
    111
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Here is error I get it

    PHP Code:
    A:ito.java:7Incompatible type for declarationCant convert java.lang.String to int.
      
    int number JOptionPane.showInputDialog("Enter number: ");
          ^
    1 error

    Tool completed with 
    exit code 1 
    could you help me

    Thanks

  • #4
    Supreme Overlord Spookster's Avatar
    Join Date
    May 2002
    Location
    Marion, IA USA
    Posts
    6,273
    Thanks
    4
    Thanked 83 Times in 82 Posts
    The error message says it all. The showInputDialog method returns a string so you cannot assign it to an integer datatype. What you would need to do is convert it to an integer first.

    PHP Code:

    import javax
    .swing.JOptionPane;
    public class 
    ito
    {
     public static 
    void main(String[] args)
     {

    String input JOptionPane.showInputDialog("Enter number: ");

    int number Integer.parseInt(input); 

      switch(
    number)
      {
        case 
    0:
             
    System.out.println("you typed zero");
             break;
        case 
    1:
         
    System.out.println("you typed one");
             break;
        default:
              
    System.out.println("you didn't type zero or one");
             break;

         }

         
    System.exit(0);

         }} 
    Of course it would probably be a good idea to add in some code to validate the input to ensure it actually is a number that they entered and also the required number of digits.
    Spookster
    CodingForums Supreme Overlord
    All Hail Spookster

  • #5
    Regular Coder
    Join Date
    Sep 2002
    Location
    Surrey, UK
    Posts
    119
    Thanks
    0
    Thanked 0 Times in 0 Posts

    try/catch

    You are forced to check for Number by the compiler:
    try {
    int number=Integer.parseInt(input);
    } catch (NumberFormatException nfe) {
    //code to execute if not a number
    }

    You must use try/catch block or else declare your main method to throw an Exception

  • #6
    New to the CF scene
    Join Date
    Mar 2007
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I have a HTML page that I need to insert ads, but the ads are in javascript and not doing well , guess i need help


  •  

    Posting Permissions

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