0

i have got a problem while i tried to compile console game, Console shows me

Exception in thread "main" java.lang.Error: Unresolved compilation problem: at Pytania.main(Pytania.java:6)

Line 6 is public static void main(String[] args), and i sadly dont see fail :(

I use Eclipse, thanks for answer :)

import java.util.Scanner;

public class Pytania{


public static void main(String[] args){

    String Elf = ("Elf");
    String Kot = ("Kot");
    String Wojownik = ("Wojownik");
    String Lucznik = ("Lucznik");

    Scanner pisz = new Scanner(System.in);

    System.out.println("---------------------------------");
    System.out.println("----------RPG--TALES-------------");
    System.out.println("------------Part 1---------------");
    System.out.println("---------------------------------");
    System.out.println(" ");
    System.out.println("Pamietaj, pisz poprawnie, inaczej mutanty wysadza gre! :D ");
    System.out.println(" ");
    System.out.println("Podaj nazwe uzytkownika");
    String nazwa = pisz.nextLine();
    System.out.println("Witaj " + nazwa + " w nowym symulatorze RPG, nastepnym krokiem bedzie wybranie klasy, wybierz klase z dostepnych ponizej");
    System.out.println("Elf, Kot, Wojownik, Lucznik");
    String postac = pisz.nextLine();
    System.out.println("Gratulacje " + nazwa + ", Wybrales klase " + postac);





    if (postac=="Elf"){

        return postac = Elf;


    }


}

This'll help me a lot, thanks ! :)

zapole
  • 39
  • 4
  • 2
    What do you think `return postac = Elf;` should do (remember that you are still in `public static void main(String[] args)` method) – Pshemo Jul 17 '15 at 19:06
  • 3
    Also about your next question why `if (postac=="Elf")` doesn't work: [How do I compare strings in Java?](http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – Pshemo Jul 17 '15 at 19:07
  • 1
    When you just try to *compile* something, you don't get exceptions - you get compilation errors. Exceptions occur when you try to *run* code. Don't run it until it compiles - and while there are compilation errors, they should be a lot more descriptive than what you've provided... – Jon Skeet Jul 17 '15 at 19:08
  • Postac is in my Language "name", so i wanted that if user will write avaible class from list System.out.println("Elf, Kot, Wojownik, Lucznik"); as example Elf, it will return his class //return postac = Elf; and then when i write postac, it will automaticly change text to "Elf" in last line of println, sorry for my bad Englisch :/ – zapole Jul 17 '15 at 19:10
  • Actually "Postac" looks more like "Profession" (zajęcie/klasa), so you should probably name it that way :) Anyway `return` is not the action you want to use. You should probably create separate class like `MyCharacter` in which you will have field for race, name, HP, and some other statistics (you will need to add getters and setters too). Then you can create instance of such class like `MyCharacter mc = new MyCharacter();` and store data from user using something like `mc.setProfession(profesja)`. When you will need it use `mc.getProfession()`. – Pshemo Jul 17 '15 at 19:20
  • What is `Elf` in `return postac = Elf;`? Is it perhaps some `enum`? – Pshemo Jul 17 '15 at 20:11

2 Answers2

1

You can also change your equals method to something like this:

static String equals(String postac)
{


    switch(postac)
    {
        case "Elf": postac = "Elf"; 
            break;
        case "Kot": postac = "Kot";
            break; 
        case "Wojownik": postac = "Wojownik";
            break;
        case "Lucznik": postac = "Lucznik";
            break;

    }
         return postac;
}

And then you can just call it within your final statement just like:

 System.out.println("Gratulacje " + nazwa + ", Wybrales klase " + equals(postac));
SomeStudent
  • 2,856
  • 1
  • 22
  • 36
  • 1
    Sorry but what is the point of checking if `postac` has some value, and if it does assign same value which it already has? You probably mean `postac = Elf;` instead of `postac = "Elf"` (notice quotes). – Pshemo Jul 17 '15 at 20:11
  • It was more to just show an example of how to create a separate method. Your point is valid. – SomeStudent Jul 17 '15 at 22:22
0

You must add another } at the end to close the class definition.

Also, you cannot use a return statement in a void method.

JFPicard
  • 5,029
  • 3
  • 19
  • 43
  • You can accept the answer if it solved your problem. – JFPicard Jul 17 '15 at 19:22
  • 1
    Actually questions about typographical errors are off-topic on Stack Overflow and as such should be closed (and this case probably deleted). IMO best option to help OP and let others maintain high quality of SO is posting suggestion to such questions as comments rather than answers. Problem with answers is that if question will be closed it can't be automatically deleted if there is accepted answer or answer with positive score. Also don't worry about reputation, there are lot of interesting questions on SO, rep gained easy doesn't feel that good. – Pshemo Jul 17 '15 at 19:31
  • Thank you all, i am still new there, and this is my first day of using this site, so thanks for so much informations :) – zapole Jul 17 '15 at 19:36