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.

Page 1 of 2 12 LastLast
Results 1 to 25 of 26

Thread: Few errors on my code.

  1. #1
    Member
    Join Date
    Feb 2014
    Posts
    38
    Thanks
    17
    Thanked 0 Times in 0 Posts

    Default Few errors on my code.

    import java.util.Scanner;
     
    class Player {
    	private String name;
    	private String gender;
    	private String hcolor;
    	private String ecolor;
    	private String size;
    	private String height;
    	private String place;
     
    	public void setname(String name)
    	{
    		this.name = name;
    	}
    	public void setgender (String gender)
    	{
    		this.gender = gender;
    	}
    	public void sethcolor (String hcolor)
    	{
    		this.hcolor=hcolor;
    	}
    	public void setecolor (String ecolor)
    	{
    		this.ecolor=ecolor;
    	}
    	public void setsize (String size)
    	{
    		this.size = size;
    	}
    	public void setheight (String height)
    	{
    		this.height=height;
    	}
    	public void setplace (String place)
    	{
    		this.place=place; 
    	}
    	public String getname()
    	{
    		return name;
    	}
    	public String getgender()
    	{
    		return gender;
    	}
    	public String hcolor()
    	{
    		return hcolor;
    	}
    	public String getecolor()
    	{
    		return ecolor;
    	}
    	public String getsize()
    	{
    		return size;
    	}
    	public String getheight()
    	{
    		return height;
    	}
    	public String getplace()
    	{
    		return place;
    	}
     
     
    public class Roleplaygame {
     
    	public static void main(String[] args) {
    		Player player1 = new Player();
    		Scanner scanner = new Scanner (System.in);
    		System.out.print("Name: ");
    		String name = scanner.nextLine();
    		player1.setname(name);
    		System.out.print("Gender: ");
    		String gender = scanner.next();
            System.out.print("Hair Color: ");
            String hcolor = scanner.next();
            System.out.print("Eye Color: ");
            String ecolor = scanner.next();
            System.out.print("Fat or Skinny: ");
            String size = scanner.next();
            System.out.print("Tall or Short: ");
            String height = scanner.next();
            System.out.print("Name of the town you are going to: ");
            String place = scanner.next();
            System.out.println ("...");
            System.out.println ("...");
            System.out.println ("Welcome to " + player1.getplace());
            pmenu(place);
     
    	}
    	public void pmenu(String place)
    	{
    		System.out.println("Welcome to " + place);
    		System.out.println("What would you like to do?");
    		System.out.println("1. Inventory");
    		System.out.println("Talk to NPC: Bob");
    		System.out.println("Store");
    	}
     
    }
    }

    Error:
    The method main cannot be declared static; static methods can only be declared in a static or top level type
    on
    public static void main(String[] args)

    and

    Multiple markers at this line
    - Occurrence of 'pmenu'
    - Cannot make a static reference to the non-static method pmenu(String) from the type
    Player.Roleplaygame
    on
    pmenu(place);


    I see nothing wrong with my code. This is the start of my RPG game code project.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,940
    Thanks
    56
    Thanked 2,353 Times in 2,324 Posts

    Default Re: Few errors on my code.

    The formatting of the code makes it hard to see the nesting. There should not be two }s one above the other.

    It looks like there is a nested, inner class with a static method: main() which is not allowed.
    Check the locations of the class definition (should it be inside the other class?).
    Should Player be the inside class?
    If you don't understand my answer, don't ignore it, ask a question.

  3. The Following User Says Thank You to Norm For This Useful Post:

    hkfrenchtoast (February 22nd, 2014)

  4. #3
    Member
    Join Date
    Feb 2014
    Posts
    180
    Thanks
    0
    Thanked 48 Times in 45 Posts

    Default Re: Few errors on my code.

    If you're using Eclipse, expand the Source menu and select Format to format (and indent) your code. The little mistake should be quite obvious once you've done so. You'd see:

    	public String getplace() {
    		return place;
    	}
     
    	public class Roleplaygame {
     
    		public static void main(String[] args) {

    This is the nested, inner class that Norm referred to.

    The 2 }s are right at the bottom of your code listing, which when properly indented, becomes:

    			System.out.println("Store");
    		}
     
    	}
    }

    What you might want is to remove one of the 2 }s, and add one } right before public class Roleplaygame { so that you have matching curly braces the Player and Roleplaygame classes, taking RolePlaygame out of being a nested, inner class.

    Apart from that, the pmenu(place) call at the bottom of the main method (which is a static method) is not valid because the pmenu method is not static. This is what it meant by Cannot make a static reference to the non-static method pmenu(String) from the type Player.Roleplaygame. One way to rectify this is to make pmenu a static method, i.e., public static void pmenu(String place).

    Hth!

  5. The Following User Says Thank You to jashburn For This Useful Post:

    hkfrenchtoast (February 22nd, 2014)

  6. #4
    Member
    Join Date
    Feb 2014
    Posts
    38
    Thanks
    17
    Thanked 0 Times in 0 Posts

    Default Re: Few errors on my code.

    Okay, I fixed the problem. However, I added a new method for the Town Menu. I am getting errors

     import java.util.Scanner;
     
     
     
    class Player {
    	private String name;
    	private String gender;
    	private String hcolor;
    	private String ecolor;
    	private String size;
    	private String height;
    	private String place;
     
    	public void setname(String name)
    	{
    		this.name = name;
    	}
    	public void setgender (String gender)
    	{
    		this.gender = gender;
    	}
    	public void sethcolor (String hcolor)
    	{
    		this.hcolor=hcolor;
    	}
    	public void setecolor (String ecolor)
    	{
    		this.ecolor=ecolor;
    	}
    	public void setsize (String size)
    	{
    		this.size = size;
    	}
    	public void setheight (String height)
    	{
    		this.height=height;
    	}
    	public void setplace (String place)
    	{
    		this.place=place; 
    	}
    	public String getname()
    	{
    		return name;
    	}
    	public String getgender()
    	{
    		return gender;
    	}
    	public String gethcolor()
    	{
    		return hcolor;
    	}
    	public String getecolor()
    	{
    		return ecolor;
    	}
    	public String getsize()
    	{
    		return size;
    	}
    	public String getheight()
    	{
    		return height;
    	}
    	public String getplace()
    	{
    		return place;
    	}
    	public String geteverything()
    	{
    		System.out.println("Name: " + getname());
    		System.out.println("Gender: " + getgender());
    		System.out.println("Hair Color: " + gethcolor());
    		System.out.println("Eye Color: " + getecolor());
    		System.out.println("Size: " + getsize());
    		System.out.println("Height: " + getheight());
    		pmenu();
    	}
     
    }
     
     
    public class Roleplaygame {
     
    	public static void main(String[] args) {
    		Player player1 = new Player();
    		Scanner scanner = new Scanner (System.in);
    		System.out.print("Name: ");
    		String name = scanner.nextLine();
    		player1.setname(name);
    		System.out.print("Gender: ");
    		String gender = scanner.next();
            System.out.print("Hair Color: ");
            String hcolor = scanner.next();
            System.out.print("Eye Color: ");
            String ecolor = scanner.next();
            System.out.print("Fat or Skinny: ");
            String size = scanner.next();
            System.out.print("Tall or Short: ");
            String height = scanner.next();
            System.out.print("Name of the town you are going to: ");
            String place = scanner.next();
            System.out.println ("...");
            System.out.println ("...");
            System.out.println ("Welcome to " + player1.getplace());
            pmenu(place);
     
    	}
    	public static void pmenu(String place)
    	{
    		Scanner scanner2  = new Scanner (System.in);
    		System.out.println("--------------------------");
    		System.out.println("Welcome to " + place);
    		System.out.println("1. Status");
    		System.out.println("2. Inventory");
    		System.out.println("3. Talk to NPC: Bob");
    		System.out.println("4. Store");
    		System.out.println("What would you like to do?");
    		int pinput = scanner2.nextInt();
    		if (pinput==1)
    		{
    			player1.geteverything();
    		}
     
    	}	
    }

    Errors:
    The method pmenu() is undefined for the type Player
    on
    pmenu();
    on public String geteverything();
    and
    player1 cannot be resolved
    on
    player1.geteverything();

    How could I fix this? Create a new class? Thanks again.

  7. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,940
    Thanks
    56
    Thanked 2,353 Times in 2,324 Posts

    Default Re: Few errors on my code.

    method pmenu() is undefined
    player1 cannot be resolved
    The compiler can not find a definition for the method or variable in the error message. Check the spelling and the scope of the method's and variable's definition.
    If you don't understand my answer, don't ignore it, ask a question.

  8. #6
    Member
    Join Date
    Feb 2014
    Posts
    38
    Thanks
    17
    Thanked 0 Times in 0 Posts

    Default Re: Few errors on my code.

    No spelling mistakes... Not sure what is wrong? Is it because of the placement of the code?

  9. #7
    Member
    Join Date
    Dec 2013
    Posts
    40
    My Mood
    Amazed
    Thanks
    2
    Thanked 11 Times in 11 Posts

    Default Re: Few errors on my code.

    Observation: The method public String geteverything should be declared void as it does not return anything but just prints out.
    Who holds the KEY to all knowledge?

  10. #8
    Member
    Join Date
    Feb 2014
    Posts
    38
    Thanks
    17
    Thanked 0 Times in 0 Posts

    Default Re: Few errors on my code.

    Yeah, I put that after observing my code. But it did not fix anything.

  11. #9
    Member
    Join Date
    Dec 2013
    Posts
    40
    My Mood
    Amazed
    Thanks
    2
    Thanked 11 Times in 11 Posts

    Default Re: Few errors on my code.

    The solution should be to take the pmenu method to the Player class as the Roleplaygame cannot locate the pmenu method when the Player class was instantiated.
    Who holds the KEY to all knowledge?

  12. The Following User Says Thank You to Mugambbo For This Useful Post:

    hkfrenchtoast (February 23rd, 2014)

  13. #10
    Member
    Join Date
    Feb 2014
    Posts
    38
    Thanks
    17
    Thanked 0 Times in 0 Posts

    Default Re: Few errors on my code.

    Wow, that makes so much sense HAHA (EDIT: NVM. Look at bottom!). There was another solution that I found to this code too: (Turning everything into statics)
    import java.util.Scanner;
     
    class Player {
    	private static String name;
    	private static String gender;
    	private static String hcolor;
    	private static String ecolor;
    	private static String size;
    	private static String height;
    	private static String place;
     
    	public void setname(String name)
    	{
    		this.name = name;
    	}
    	public void setgender (String gender)
    	{
    		this.gender = gender;
    	}
    	public void sethcolor (String hcolor)
    	{
    		this.hcolor=hcolor;
    	}
    	public void setecolor (String ecolor)
    	{
    		this.ecolor=ecolor;
    	}
    	public void setsize (String size)
    	{
    		this.size = size;
    	}
    	public void setheight (String height)
    	{
    		this.height=height;
    	}
    	public void setplace (String place)
    	{
    		this.place=place; 
    	}
    	public String getname()
    	{
    		return name;
    	}
    	public String getgender()
    	{
    		return gender;
    	}
    	public String gethcolor()
    	{
    		return hcolor;
    	}
    	public String getecolor()
    	{
    		return ecolor;
    	}
    	public String getsize()
    	{
    		return size;
    	}
    	public String getheight()
    	{
    		return height;
    	}
    	public String getplace()
    	{
    		return place;
    	}
    	public static void geteverything()
    	{
    		System.out.println("Name: " + name);
    		System.out.println("Gender: " + gender);
    		System.out.println("Location: " + place);
    		System.out.println("Hair Color: " + hcolor);
    		System.out.println("Eye Color: " + ecolor);
    		System.out.println("Size: " + size);
    		System.out.println("Height: " + height);
    	}
     
    }
     
     
    public class Roleplaygame {
     
    	public static void main(String[] args) {
    		Player player1 = new Player();
    		Scanner scanner = new Scanner (System.in);
    		System.out.print("Name: ");
    		String name = scanner.nextLine();
    		player1.setname(name);
    		System.out.print("Gender: ");
    		String gender = scanner.next();
    		player1.setgender(gender);
            System.out.print("Hair Color: ");
            String hcolor = scanner.next();
            player1.sethcolor(hcolor);
            System.out.print("Eye Color: ");
            String ecolor = scanner.next();
            player1.setecolor(ecolor);
            System.out.print("Fat or Skinny: ");
            String size = scanner.next();
            player1.setsize(size);
            System.out.print("Tall or Short: ");
            String height = scanner.next();
            player1.setheight(height);
            System.out.print("Name of the town you are going to: ");
            String place = scanner.next();
            player1.setplace(place);
            System.out.println ("...");
            System.out.println ("...");
            System.out.println ("Welcome to " + player1.getplace());
            pmenu(place);
     
    	}
    	public static void pmenu(String place)
    	{
    		Scanner scanner2  = new Scanner (System.in);
    		System.out.println("--------------------------");
    		System.out.println("Welcome to " + place);
    		System.out.println("1. Status");
    		System.out.println("2. Inventory");
    		System.out.println("3. Talk to NPC: Bob");
    		System.out.println("4. Store");
    		System.out.println("What would you like to do?");
    		int pinput = scanner2.nextInt();
    		if (pinput==1)
    		{
    			Player.geteverything();
    			pmenu(place);
    		}
     
    	}	
    }

    This is so very very fascinating!

    --- Update ---

    Wait, nevermind. The more I look at the code on Post 4, the more I do not understand why it doesn't work... Aside from the small mistakes here and there. I don't see why the Roleplaygame can't locate the pmenu method

  14. #11
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    6,104
    My Mood
    Mellow
    Thanks
    244
    Thanked 755 Times in 741 Posts

    Default Re: Few errors on my code.

    I don't see why the Roleplaygame can't locate the pmenu method
    On what evidence is that statement made? Post the error.

    "Turning everything to statics" is not an appropriate OOP solution. It may serve the Java programmer who has not evolved past the "one class with all code in the main() method" design, but I think you are trying to move to the next level. Understanding the differences between static, non-static, and instance methods and variables is key to making that step successfully, so it's good that you're studying this code for understanding, asking great questions. I recommend that you don't give up until you do understand.

    The reason I asked my initial question is because I don't think it's accurate. Review the evidence that caused you to ask that question, post it if you can, and we can discuss it further.

    Good luck!

  15. #12
    Member
    Join Date
    Dec 2013
    Posts
    40
    My Mood
    Amazed
    Thanks
    2
    Thanked 11 Times in 11 Posts

    Default Re: Few errors on my code.

    Why I believe the Roleplaygame class couldn't locate the pmenu method was because the pmenu method was called even before the method was defined. And as java follows what is called the 'Stack Trace', your app was meant to start executing from top to bottom. I hope this helps.
    Who holds the KEY to all knowledge?

  16. #13
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    6,104
    My Mood
    Mellow
    Thanks
    244
    Thanked 755 Times in 741 Posts

    Default Re: Few errors on my code.

    Quote Originally Posted by Mugambbo View Post
    Why I believe the Roleplaygame class couldn't locate the pmenu method was because the pmenu method was called even before the method was defined. And as java follows what is called the 'Stack Trace', your app was meant to start executing from top to bottom. I hope this helps.
    It doesn't help, because your guess as to the cause and your explanation of your reasoning are not based on Java, its compiler, or the JVM. While some basic IDEs may enforce "source code order is important" rules because they don't know better, it's not a Java requirement.

    Please research your theories before posting them. Not only will you learn from the experience, but impressionable students will be spared the task of sorting fact from fiction and the confusion that will likely cause.

  17. #14
    Member
    Join Date
    Dec 2013
    Posts
    40
    My Mood
    Amazed
    Thanks
    2
    Thanked 11 Times in 11 Posts

    Default Re: Few errors on my code.

    I appreciate your impressive criticism Greg. Will do as you suggested. Thanks anyway.
    Who holds the KEY to all knowledge?

  18. #15
    Member
    Join Date
    Feb 2014
    Posts
    38
    Thanks
    17
    Thanked 0 Times in 0 Posts

    Default Re: Few errors on my code.

    Quote Originally Posted by GregBrannon View Post
    On what evidence is that statement made? Post the error.

    "Turning everything to statics" is not an appropriate OOP solution. It may serve the Java programmer who has not evolved past the "one class with all code in the main() method" design, but I think you are trying to move to the next level. Understanding the differences between static, non-static, and instance methods and variables is key to making that step successfully, so it's good that you're studying this code for understanding, asking great questions. I recommend that you don't give up until you do understand.

    The reason I asked my initial question is because I don't think it's accurate. Review the evidence that caused you to ask that question, post it if you can, and we can discuss it further.

    Good luck!
    Well I fixed the trivial mistakes from post number 4. I ended up with a single error:

     import java.util.Scanner;
     
    class Player {
    	private String name;
    	private String gender;
    	private String hcolor;
    	private String ecolor;
    	private String size;
    	private String height;
    	private String place;
     
    	public void setname(String name)
    	{
    		this.name = name;
    	}
    	public void setgender (String gender)
    	{
    		this.gender = gender;
    	}
    	public void sethcolor (String hcolor)
    	{
    		this.hcolor=hcolor;
    	}
    	public void setecolor (String ecolor)
    	{
    		this.ecolor=ecolor;
    	}
    	public void setsize (String size)
    	{
    		this.size = size;
    	}
    	public void setheight (String height)
    	{
    		this.height=height;
    	}
    	public void setplace (String place)
    	{
    		this.place=place; 
    	}
    	public String getname()
    	{
    		return name;
    	}
    	public String getgender()
    	{
    		return gender;
    	}
    	public String gethcolor()
    	{
    		return hcolor;
    	}
    	public String getecolor()
    	{
    		return ecolor;
    	}
    	public String getsize()
    	{
    		return size;
    	}
    	public String getheight()
    	{
    		return height;
    	}
    	public String getplace()
    	{
    		return place;
    	}
    	public void geteverything()
    	{
    		System.out.println("Name: " + getname());
    		System.out.println("Gender: " + getgender());
    		System.out.println("Hair Color: " + gethcolor());
    		System.out.println("Eye Color: " + getecolor());
    		System.out.println("Size: " + getsize());
    		System.out.println("Height: " + getheight());
    		pmenu(place);
    	}
     
    }
     
     
    public class Roleplaygame2 {
     
    	public static void main(String[] args) {
    		Player player1 = new Player();
    		Scanner scanner = new Scanner (System.in);
    		System.out.print("Name: ");
    		String name = scanner.nextLine();
    		player1.setname(name);
    		System.out.print("Gender: ");
    		String gender = scanner.next();
    		player1.setname(gender);
            System.out.print("Hair Color: ");
            String hcolor = scanner.next();
            player1.sethcolor(hcolor);
            System.out.print("Eye Color: ");
            String ecolor = scanner.next();
            player1.setecolor(ecolor);
            System.out.print("Fat or Skinny: ");
            String size = scanner.next();
            player1.setsize(size);
            System.out.print("Tall or Short: ");
            String height = scanner.next();
            player1.setheight(height);
            System.out.print("Name of the town you are going to: ");
            String place = scanner.next();
            player1.setplace(place);
            System.out.println ("...");
            System.out.println ("...");
            System.out.println ("Welcome to " + player1.getplace());
            pmenu(place);
     
    	}
    	public static void pmenu(String place)
    	{
    		Scanner scanner2  = new Scanner (System.in);
    		Player player1= new Player();
    		System.out.println("--------------------------");
    		System.out.println("Welcome to " + place);
    		System.out.println("1. Status");
    		System.out.println("2. Inventory");
    		System.out.println("3. Talk to NPC: Bob");
    		System.out.println("4. Store");
    		System.out.println("What would you like to do?");
    		int pinput = scanner2.nextInt();
    		if (pinput==1)
    		{
    			player1.geteverything();
    		}
     
    	}	
    }

    The error:
    The method pmenu(String) is undefined for the type Player
    on
    pmenu(place);
    in
    public void geteverything()

    I tried replacing (place) to (getplace()) but it did not work. Still do not understand why roleplaygame can't locate the pmenu. :/

  19. #16
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,940
    Thanks
    56
    Thanked 2,353 Times in 2,324 Posts

    Default Re: Few errors on my code.

    The method pmenu(String) is undefined for the type Player
    Where in the class: Player is the method: pmenu(String) defined?
    I see a method with the same name defined in another class: Roleplaygame2
    If you don't understand my answer, don't ignore it, ask a question.

  20. The Following User Says Thank You to Norm For This Useful Post:

    hkfrenchtoast (February 23rd, 2014)

  21. #17
    Member
    Join Date
    Feb 2014
    Posts
    38
    Thanks
    17
    Thanked 0 Times in 0 Posts

    Default Re: Few errors on my code.

    Quote Originally Posted by Norm View Post
    Where in the class: Player is the method: pmenu(String) defined?
    I see a method with the same name defined in another class: Roleplaygame2
    I changed the pmenu to
    Roleplaygame2.pmenu(place);

    But that just gave me another error:

    Multiple markers at this line
    - The type Player is already
    defined
    - Occurrence of 'Player'
    Looking at the code, Player type wasn't already defined...

  22. #18
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,940
    Thanks
    56
    Thanked 2,353 Times in 2,324 Posts

    Default Re: Few errors on my code.

    Can you post the new code and the full text of the compiler's error messages?
    If you don't understand my answer, don't ignore it, ask a question.

  23. #19
    Member
    Join Date
    Feb 2014
    Posts
    38
    Thanks
    17
    Thanked 0 Times in 0 Posts

    Default Re: Few errors on my code.

    Yes, of course!

    import java.util.Scanner;
     
    class Player {
    	private String name;
    	private String gender;
    	private String hcolor;
    	private String ecolor;
    	private String size;
    	private String height;
    	private String place;
     
    	public void setname(String name)
    	{
    		this.name = name;
    	}
    	public void setgender (String gender)
    	{
    		this.gender = gender;
    	}
    	public void sethcolor (String hcolor)
    	{
    		this.hcolor=hcolor;
    	}
    	public void setecolor (String ecolor)
    	{
    		this.ecolor=ecolor;
    	}
    	public void setsize (String size)
    	{
    		this.size = size;
    	}
    	public void setheight (String height)
    	{
    		this.height=height;
    	}
    	public void setplace (String place)
    	{
    		this.place=place; 
    	}
    	public String getname()
    	{
    		return name;
    	}
    	public String getgender()
    	{
    		return gender;
    	}
    	public String gethcolor()
    	{
    		return hcolor;
    	}
    	public String getecolor()
    	{
    		return ecolor;
    	}
    	public String getsize()
    	{
    		return size;
    	}
    	public String getheight()
    	{
    		return height;
    	}
    	public String getplace()
    	{
    		return place;
    	}
    	public void geteverything()
    	{
    		System.out.println("Name: " + getname());
    		System.out.println("Gender: " + getgender());
    		System.out.println("Hair Color: " + gethcolor());
    		System.out.println("Eye Color: " + getecolor());
    		System.out.println("Size: " + getsize());
    		System.out.println("Height: " + getheight());
    		Roleplaygame2.pmenu(place);
    	}
     
    }
     
     
    public class Roleplaygame2 {
     
    	public static void main(String[] args) {
    		Player player1 = new Player();
    		Scanner scanner = new Scanner (System.in);
    		System.out.print("Name: ");
    		String name = scanner.nextLine();
    		player1.setname(name);
    		System.out.print("Gender: ");
    		String gender = scanner.next();
    		player1.setname(gender);
            System.out.print("Hair Color: ");
            String hcolor = scanner.next();
            player1.sethcolor(hcolor);
            System.out.print("Eye Color: ");
            String ecolor = scanner.next();
            player1.setecolor(ecolor);
            System.out.print("Fat or Skinny: ");
            String size = scanner.next();
            player1.setsize(size);
            System.out.print("Tall or Short: ");
            String height = scanner.next();
            player1.setheight(height);
            System.out.print("Name of the town you are going to: ");
            String place = scanner.next();
            player1.setplace(place);
            System.out.println ("...");
            System.out.println ("...");
            System.out.println ("Welcome to " + player1.getplace());
            pmenu(place);
     
    	}
    	public static void pmenu(String place)
    	{
    		Scanner scanner2  = new Scanner (System.in);
    		Player player1= new Player();
    		System.out.println("--------------------------");
    		System.out.println("Welcome to " + place);
    		System.out.println("1. Status");
    		System.out.println("2. Inventory");
    		System.out.println("3. Talk to NPC: Bob");
    		System.out.println("4. Store");
    		System.out.println("What would you like to do?");
    		int pinput = scanner2.nextInt();
    		if (pinput==1)
    		{
    			player1.geteverything();
    		}
     
    	}

    Error:
    Multiple markers at this line
    - The type Player is already
    defined
    on
    class Player


    Maybe it has something to do with:
    Player player1= new Player();
    on
    public static void pmenu(String place)
    ?
    I feel like it should not be repeated, but putting the player player1 code fixes an error.

  24. #20
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,940
    Thanks
    56
    Thanked 2,353 Times in 2,324 Posts

    Default Re: Few errors on my code.

    The error message doesn't have the line number with them so its sort of floating around not connected to anything. Can you use the javac compiler to get some better error messages?

    The message should show the source with a ^ under the location of the error.
    Here is a sample from the javac compiler:
    TestSorts.java:138: cannot find symbol
    symbol  : variable var
    location: class TestSorts
             var = 2;
             ^
    If you don't understand my answer, don't ignore it, ask a question.

  25. The Following User Says Thank You to Norm For This Useful Post:

    hkfrenchtoast (February 23rd, 2014)

  26. #21
    Member
    Join Date
    Feb 2014
    Posts
    38
    Thanks
    17
    Thanked 0 Times in 0 Posts

    Default Re: Few errors on my code.

    The code does run on javac, however...When I go to the Status (Pmenu method) in my code, all the stuff are NULL. like
    Name= Null
    Gender = Null
    Hair Color= Null .....

    Compiler error is on Line 2 on Class Player.

    I think it has something to do with the double Player player1=new Player() code on both the main class and the pmenu method.

  27. #22
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,940
    Thanks
    56
    Thanked 2,353 Times in 2,324 Posts

    Default Re: Few errors on my code.

    Compiler error is on Line 2 on Class Player.
    Please copy the full text of the error message and paste it here. It has important info about the error.
    If you don't understand my answer, don't ignore it, ask a question.

  28. #23
    Member
    Join Date
    Feb 2014
    Posts
    38
    Thanks
    17
    Thanked 0 Times in 0 Posts

    Default Re: Few errors on my code.

    I did in an earlier post:

    Multiple markers at this line
    - The type Player is already
    defined
    - Occurrence of 'Player'
    That's all it says on the Compiler (Eclipse)

    The code runs, but it says there's an
    Error existing the project. Proceed with launch?

  29. #24
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,940
    Thanks
    56
    Thanked 2,353 Times in 2,324 Posts

    Default Re: Few errors on my code.

    That message is not associated with any source line. I don't know where it comes from. See the example in post#20 from the javac command. It has the source line number and contents.
    If you don't understand my answer, don't ignore it, ask a question.

  30. #25
    Member
    Join Date
    Feb 2014
    Posts
    38
    Thanks
    17
    Thanked 0 Times in 0 Posts

    Default Re: Few errors on my code.

    Javac does not recognize the Error. Perhaps it's just an Eclipse thing? I ran javac on console. No error from that. The code just runs.

    --- Update ---

    Okay, I managed to fix the NULL problem regarding the Status option in pmenu. Here is my code now:
    The error on class Player persist however.
    EDIT: NEVERMIND! The Class Player error was because I did not close the Roleplaygame code xD!

    However, can you look at my code and see if there is any logic error? Or something that I should not do?

    import java.util.Scanner;
     
    class Player {
    	private String name;
    	private String gender;
    	private String hcolor;
    	private String ecolor;
    	private String size;
    	private String height;
    	private String place;
     
    	public void setname(String name)
    	{
    		this.name = name;
    	}
    	public void setgender (String gender)
    	{
    		this.gender = gender;
    	}
    	public void sethcolor (String hcolor)
    	{
    		this.hcolor=hcolor;
    	}
    	public void setecolor (String ecolor)
    	{
    		this.ecolor=ecolor;
    	}
    	public void setsize (String size)
    	{
    		this.size = size;
    	}
    	public void setheight (String height)
    	{
    		this.height=height;
    	}
    	public void setplace (String place)
    	{
    		this.place=place; 
    	}
    	public String getname()
    	{
    		return name;
    	}
    	public String getgender()
    	{
    		return gender;
    	}
    	public String gethcolor()
    	{
    		return hcolor;
    	}
    	public String getecolor()
    	{
    		return ecolor;
    	}
    	public String getsize()
    	{
    		return size;
    	}
    	public String getheight()
    	{
    		return height;
    	}
    	public String getplace()
    	{
    		return place;
    	}
    	public void geteverything()
    	{
    		System.out.println("Name: " + getname());
    		System.out.println("Gender: " + getgender());
    		System.out.println("Hair Color: " + gethcolor());
    		System.out.println("Eye Color: " + getecolor());
    		System.out.println("Size: " + getsize());
    		System.out.println("Height: " + getheight());
    	}
     
    }
     
     
    public class Roleplaygame2 {
     
    	public static void main(String[] args) {
    		Player player1 = new Player();
    		Scanner scanner = new Scanner (System.in);
    		System.out.print("Name: ");
    		String name = scanner.nextLine();
    		player1.setname(name);
    		System.out.print("Gender: ");
    		String gender = scanner.next();
    		player1.setgender(gender);
            System.out.print("Hair Color: ");
            String hcolor = scanner.next();
            player1.sethcolor(hcolor);
            System.out.print("Eye Color: ");
            String ecolor = scanner.next();
            player1.setecolor(ecolor);
            System.out.print("Fat or Skinny: ");
            String size = scanner.next();
            player1.setsize(size);
            System.out.print("Tall or Short: ");
            String height = scanner.next();
            player1.setheight(height);
            System.out.print("Name of the town you are going to: ");
            String place = scanner.next();
            player1.setplace(place);
            System.out.println ("...");
            System.out.println ("...");
            System.out.println ("Welcome to " + player1.getplace());
            pmenu(player1);
     
    	}
     
    	public static void pmenu(Player player1)
    	{
    		Scanner scanner2  = new Scanner (System.in);
     
    		System.out.println("--------------------------");
    		System.out.println("Welcome to " + player1.getplace());
    		System.out.println("1. Status");
    		System.out.println("2. Inventory");
    		System.out.println("3. Talk to NPC: Bob");
    		System.out.println("4. Store");
    		System.out.println("What would you like to do?");
    		int pinput = scanner2.nextInt();
    		if (pinput==1)
    		{
    			player1.geteverything();
    			pmenu(player1);
    		}
     
    	}	
    }

Page 1 of 2 12 LastLast

Similar Threads

  1. Why am I getting so many errors in my code?
    By namenamename in forum What's Wrong With My Code?
    Replies: 1
    Last Post: December 3rd, 2013, 05:39 PM
  2. Replies: 3
    Last Post: April 27th, 2013, 08:19 AM
  3. Someone help me with my code it has some errors? Thanks!
    By skitheeast8 in forum What's Wrong With My Code?
    Replies: 7
    Last Post: November 1st, 2012, 07:37 PM
  4. Code is giving an error in console, but there are no errors apparent in the code!
    By JamEngulfer221 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 15th, 2011, 09:30 PM
  5. Please check the code for the errors
    By nrao in forum What's Wrong With My Code?
    Replies: 9
    Last Post: November 16th, 2010, 05:37 PM