Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: incompatible types: void cannot be converted to int

  1. #1
    Junior Member
    Join Date
    Jul 2014
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default incompatible types: void cannot be converted to int

    package input;
    import javax.swing.JOptionPane;
    public class Input {


    public static void main(String[] args) {
    int user;
    user = JOptionPane.showMessageDialog(null, "Enter Your Age""); ERROR IS HERE

    if(user <= 18) {
    JOptionPane.showMessageDialog(null, "User is legit");
    }
    else {
    JOptionPane.showMessageDialog(null, "User is not legit");
    }
    }
    }

    Hello, can you please help me about this code, I'm new to Java, I'm getting this error message :

    incompatible types: void cannot be converted to int

    unclosed string literal

    Thanks a lot!!!


  2. #2
    Junior Member
    Join Date
    Jul 2014
    Posts
    15
    My Mood
    Cold
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: incompatible types: void cannot be converted to int

    are you getting an error with "int user;" because I'm fairly certain you should be... Try a variable, for instance:
    String x = JOptionPane.showMessageDialog(null, "Enter Your Age"");
    JOptionPane.showMessageDialog(null, x); <----------- this will bounce back whatever you typed in

    as far as your: "if(user <= 18) {" line goes, you need to use something that will match whatever number you typed in literally, its not a 1 or 0 or true or false, there are infinitely many answers rather than two if that makes sense.
    Last edited by Joe4296; July 29th, 2014 at 08:35 PM.

  3. #3
    Junior Member
    Join Date
    Jul 2014
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: incompatible types: void cannot be converted to int

    Hey Joe, thanks for answer, I'm not getting error with int user; line , only at user = JOptionPane.showMessageDialog(null, "Enter Your Age""); and it says incompatible types: void cannot be converted to int. I tried your method with variable String and still won't work. Says void cannot be converted to String.

    --- Update ---

    Update ;

    Actually I'm not getting error on that line no more, I'm getting error on if(user <= 18) { now. It says : bad operand types for binary operator <=

    first type : String
    second type : int

  4. #4
    Junior Member
    Join Date
    Jul 2014
    Posts
    15
    My Mood
    Cold
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: incompatible types: void cannot be converted to int

    One of the problems is the actual line JOptionPane.showMessageDialog(null, "Enter Your Age""); you close it with a double "", one should be removed, secondly, if you want someone to type in their answer, the line you want is going to be JOptionPane.showInputDialog(null, "Enter Your Age"); Also, the "Enter Your Age" is going to come out as a String if I cam correct, which means it it cannot be, or easily, converted to int for your (user <= 18) statement.

    Here is what I came up with, it still doesn't literally match age, but it "should" tell you if you are legit or not, and what you input... its up to you to fix it

    Update: I figured it out, only took a little googling, I will share if you like, but its up too you...

     package Writer;
    import javax.swing.JOptionPane;
    public class Sample {
     
     
    public static void main(String[] args) {
    	String z = JOptionPane.showInputDialog(null, "Enter Your Age");
    	{
    		if(z == "18")
    		{
    		JOptionPane.showMessageDialog(null, "User is legit, user's age is" + z);
    		}
    		else if(z != "18")
    		{
    		JOptionPane.showMessageDialog(null, "User is not legit, user's age is " + z);
    		}
    	}
       }
    }
    Last edited by Joe4296; July 29th, 2014 at 09:48 PM.

  5. #5
    Junior Member
    Join Date
    Jul 2014
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: incompatible types: void cannot be converted to int

    Update ;

    user = Integer.parseInt(JOptionPane.showInputDialog("Ente r Your Age")); this let me execute program now, thanks!

  6. #6
    Junior Member
    Join Date
    Jul 2014
    Posts
    15
    My Mood
    Cold
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: incompatible types: void cannot be converted to int

    Quote Originally Posted by Denirulz View Post
    Update ;

    user = Integer.parseInt(JOptionPane.showInputDialog("Ente r Your Age")); this let me execute program now, thanks!
    It works? Nice, what was this program for might I ask?

  7. #7
    Junior Member
    Join Date
    Jul 2014
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: incompatible types: void cannot be converted to int

    Quote Originally Posted by Joe4296 View Post
    It works? Nice, what was this program for might I ask?
    Just a simple one, I've been learning Java for like 3 hours now lol so i tried some simple thing.
    First it pops up dialog and ask for your age, if you're under 18 it shows another dialog with text :User is legit and if you're 18 it shows you are not legit etc, the basic one mate, ty for help anyway gl !

Similar Threads

  1. incompatible types: void cannot be converted to String
    By Nomi in forum What's Wrong With My Code?
    Replies: 5
    Last Post: July 28th, 2014, 04:43 AM
  2. incompatible types
    By stylelink in forum What's Wrong With My Code?
    Replies: 4
    Last Post: July 10th, 2014, 08:10 AM
  3. [SOLVED] incompatible types
    By muhammad93 in forum What's Wrong With My Code?
    Replies: 8
    Last Post: March 19th, 2013, 02:20 PM
  4. Incompatible types
    By jadeclan in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 26th, 2012, 06:09 AM
  5. Incompatible types
    By jadeclan in forum What's Wrong With My Code?
    Replies: 5
    Last Post: November 25th, 2012, 03:44 PM