Escape method call error and for statement from main

Asked 2 years ago, Updated 2 years ago, 23 views

public class Testtest{

  public int countCaps(String einString){
    String[] array_word;
    array_word = einString.split(""); //

    for(int i=0; i<array_word.length;i++){
      array_word[i]; // Error!!
    }
  }
  public static void main(String[] args){

    String word = Terminal.askString("String: ");

    countCaps(einString); //MethodCall

    System.out.println(array_word[i]);
  }
}

I am writing a code that receives the string input, cuts the string characters one by one, puts them in the array, and outputs them. I keep getting errors because there is no return value for the array_word[i] part and for statement. I want to call the main method and process it, but I don't know what's going on.

java

2022-09-21 23:17

1 Answers

Unable to invoke instance method from static method. The instance method is always conveyed that the first parameter is this. But the static method is not a method of instance, so there is no such thing as this.

Looking at the code a little more...That's a problem, but I think you should watch the Java tutorial first.

public class Testtest{

  public String[] countCaps(String einString){
    String[] array_word = einString.split("); // Separate sentences with spaces and save as an array.

    return array_word;
  }

  public static void main(String[] args){
    String word = Terminal.askString("String: ");

    Testtest instance = new Testtest();
    String[] results = instance.countCaps(word); //method call

    For(String results: results) // Output tokens (words) stored in the array
        System.out.println(result);
  }
}


2022-09-21 23:17

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.