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 4 of 4

Thread: Incompatible Types

  1. #1
    Junior Member
    Join Date
    Jun 2010
    Posts
    28
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Incompatible Types

    Hello! I'm creating something sort of like a turn-based battle system to sharpen my Java skills, but I'm getting this error on lines 40 and 44, and I'm not exactly sure why. Here's my code:

    import javax.swing.*;
    import java.io.*;
    import java.util.Random;
    class TBBS
    {
    	public static void main(String[]args)
    	{
    	    int attackbonus, defensebonus, hpbonus, enemyhp, enemyattack, enemydefense, hp, attack, defense, level, s1;
    		String select, s;
    		enemyhp=5;
    		enemyattack=1;
    		enemydefense=0;
    		hp=10;
    		attack=2;
    		defense=0;
    		level=1;
     
    		JOptionPane.showMessageDialog(null, "Welcome to the battle system!", "Welcome!", JOptionPane.INFORMATION_MESSAGE);
    		Options(s);
     
    	}	
     
    	static void Options(String s)
    	{
    	  Object[] possibility = {"Attack", "Defend"};
    		s = (String)JOptionPane.showInputDialog(null, "What do you want to do?", "Action Commands", JOptionPane.PLAIN_MESSAGE,
    		null, possibility, "");
     
    	}
       static void levelup(String b, int level)
    	{
    		int attack, defense, hp, s1;
    		level=(level+1);
    		Object[] levelup = {"Attack", "Defense", "HP"};
    		b = (String)JOptionPane.showInputDialog(null, "You've grown to level " + level + "! Which stat do you want to increase?", "Level Up!", JOptionPane.PLAIN_MESSAGE,
    		null, levelup, "");
     
    		s1=Integer.parseInt(b);
     
    		if (s1=1)
    		{
    			attack=(attack+1);
    		}	
    		else if (s1=2)
    		{
    			defense=(defense+1);
    		}
    		else
    		{
    			hp=(hp+5);
    		}		
    	}
    }

    Any help would be greatly appreciated!


  2. #2
    Member Charlie's Avatar
    Join Date
    Jun 2010
    Location
    Sweden
    Posts
    41
    Thanks
    1
    Thanked 5 Times in 5 Posts

    Default Re: Incompatible Types

    A typical single equals-sign (=) is used for declaring variables. The return-statement of inx x = y (which is basically what s1 = 1 means) isnt a boolean value which is needed for the if-statement.

    Try s1 == 1 instead, just add another equals sign and give it a go.

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Incompatible Types

    I'm getting this error
    Please post full text of the error message here. It helps solving the problem

  4. #4
    Junior Member
    Join Date
    Jun 2010
    Posts
    28
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: Incompatible Types

    Quote Originally Posted by Charlie View Post
    A typical single equals-sign (=) is used for declaring variables. The return-statement of inx x = y (which is basically what s1 = 1 means) isnt a boolean value which is needed for the if-statement.

    Try s1 == 1 instead, just add another equals sign and give it a go.
    Oh, wow. I can't believe I forgot that. Thanks!

    Also, I'm sorry I didn't put the error message in the post; I put it in the title instead, but I will change this when posting future errors.

Similar Threads

  1. Storing in different types of variables
    By giorgos in forum Collections and Generics
    Replies: 0
    Last Post: November 22nd, 2009, 02:02 PM
  2. Java chatting program implementing socket logic
    By sivakrishna.g in forum Java Networking
    Replies: 2
    Last Post: October 20th, 2009, 12:47 AM