-1

i have to use with a method (beginntMitA). somehow it doesn't work without main method. does anyone know how do i fix it?

public class StringTest{
    public static void beginntMitA(String args[]){
        String s = "java";
       char ch1 = s.charAt(0);
       char ch2 = s.charAt(1);
       char ch3 = s.charAt(2);
       char ch4 = s.charAt(3);
       char ch5 = s.charAt(4);
       
       System.out.println( ch1 );
       System.out.println( ch2 );
       System.out.println( ch3 );
       System.out.println( ch4 );
       System.out.println( ch5 );
    }

}
tonini
  • 13
  • 1
  • 4
  • `char ch5 = s.charAt(4);` is going to throw an exception. Your string `"java"` only has 4 characters, and they are indexed 0, 1, 2 and 3. – Stephen C Apr 25 '21 at 13:25

2 Answers2

0

You don't give much information about why it doesn't work, but try change to

public static void main(String args[]){
batman567
  • 826
  • 2
  • 12
  • 23
0

You have to use main method. As Java main method is the entry point of any java program. you can't run normal java program without it.

you can check this link Java main method

You can define your "beginntMitA" method and call it in the main method with the class name as it's static method like this

 public class StringTest{
    
    public static void main(String args[]){
          StringTest.beginntMitA();
    }

    public static void beginntMitA(){
        String s = "java";
       char ch1 = s.charAt(0);
       char ch2 = s.charAt(1);
       char ch3 = s.charAt(2);
       char ch4 = s.charAt(3);
       char ch5 = s.charAt(4);
       
       System.out.println( ch1 );
       System.out.println( ch2 );
       System.out.println( ch3 );
       System.out.println( ch4 );
       System.out.println( ch5 );
    }
}

And You can use loops to iterate on your string characters instead of using 0, 1, 2, 3, ..