Results 1 to 3 of 3
  1. #1
    Albert is offline Senior Member
    Join Date
    Jun 2007
    Posts
    114
    Rep Power
    0

    Default Can't convert java.lang.String to int.

    Hi could you help me this script. I got a error I don't know why.
    I can't run this code

    Java 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);
    
         }}
    Here is error:

    Java Code:
    A:Ito.java:7: Incompatible type for declaration. Can't 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
    Albert:rolleyes:

  2. #2
    susan is offline Member
    Join Date
    Jul 2007
    Posts
    44
    Rep Power
    0

    Default

    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.
    Java 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.

  3. #3
    satya123 is offline Member
    Join Date
    Jul 2007
    Posts
    3
    Rep Power
    0

    Default

    Java Code:
    import javax.swing.JOptionPane;
    public class Ito {
     public static void main(String[] args)
     {
    
    [B]String pn = JOptionPane.showInputDialog("Enter number: ");
    int number=Integer.parseInt(pn);  [/B]
    
    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);
    
         }}

    Now this code won't give that error i think....just parsed the returned string uing Integer.parseInt() wrapper class function.....

Similar Threads

  1. how to convert String number to int
    By gabriel in forum New To Java
    Replies: 5
    Last Post: 08-02-2009, 03:46 PM
  2. Replies: 2
    Last Post: 09-18-2008, 07:30 AM
  3. Replies: 5
    Last Post: 01-13-2008, 10:36 PM
  4. Error: cannot be applied to (java.lang.String)
    By carl in forum New To Java
    Replies: 1
    Last Post: 08-05-2007, 06:33 AM
  5. Replies: 1
    Last Post: 08-02-2007, 04:07 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
  •