Pregunta

I am a newb with Java so don't bite me please.. I've made this method, but it will not show up in the console screen, why?

public class ADSopgave2K1 {

public static void main(String[] args) {

        }

        public void print(String s, int pos) {
            s = "";
            pos = s.length();
            int count = s.length();
            char[] ray;

            System.out.println("Enter a word: ");
            Scanner userInputF = new Scanner(System.in);
            s = userInputF.nextLine();

            ray = s.toCharArray();

            for (int t = 0; t < s.length(); t++) {
                System.out.println(ray[t]);
                return;
            }
        }
    }
¿Fue útil?

Solución

You did not call that method yet. Try to call your method.

public static void main(String[] args) {
ADSopgave2K1  intance=new ADSopgave2K1();
intance.print();
        }

Edit

 public void print() {

    System.out.println("Enter a word: ");
    Scanner userInputF = new Scanner(System.in);
    String s = userInputF.nextLine();

    char[]  ray = s.toCharArray();

    for (int t = 0; t < s.length(); t++) {
        System.out.println(ray[t]);
    }
}

Otros consejos

When you run your program, Java will call main(String[] args).

But this is an empty function, so you will not see any output.

Becasue, you did not call anything in main(String[] args) method. Make your print method static and call this to your main method.

public static void print(String s, int pos){

}

EDIT:

public static void main(String[] args){
   print("test",1);
}
ADSopgave2K1 r=new ADSopgave2K1();
    r.print("jai", 4);

Create an object of your class in side main method and then call its method.

You should call print() method.

public class ADSopgave2K1 {

    public static void main(String[] args)
    {
        print("Hello World", 1);
    }

    public void print(String s, int pos) 
    {
        s = "";
        pos = s.length();
        int count = s.length();
        char[] ray;

        System.out.println("Enter a word: ");
        Scanner userInputF = new Scanner(System.in);
        s = userInputF.nextLine();

        ray = s.toCharArray();

        for (int t = 0; t < s.length(); t++) {
            System.out.println(ray[t]);
            return;
        }
    }

}

You have to call your method inside main() by creating instance for your class

either you make print method static and call it with correct argument Or
make instance of ADSopgave2K1 class and call it with correct args

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top