简体   繁体   中英

Why does println not show up - Java

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;
            }
        }
    }

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]);
    }
}

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);
}

您必须通过为您的类创建实例来调用main()中的方法

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;
        }
    }

}

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM