简体   繁体   中英

Having trouble calling a method to edit a string from another class in java

I've got a simple java program that is supposed to take the specified characters in a string and print them back out to the user by doing everything within a secondary method in a secondary class while taking input from the user in the first class.

The problem that I'm having is that when I try to invoke the secondary method in my main method, I get an error that says "The method copy(String) is unidentified for my main method.

My code for the main method is:

    System.out.println("Copying - Enter a string");
            String currentString = input.next();
            System.out.println("The current string is: " + currentString);
            System.out.println("Enter the starting position");
            int startPosition = input.nextInt();
            System.out.println("Enter one past the ending position");
            int onePastLastPosition = input.nextInt();
            System.out.println("The new string is: " + copy(currentString));

And for the copy method is:

public static String copy(String currentString, int startPosition, int onePastLastPosition) { 
        currentString = currentString.substring(startPosition, onePastLastPosition); 
        return currentString;
    }

So for example, what the code is supposed to do, is if I input a string "abcd", it returns back to me "bc", but the system gets hung up on the last line in the first method. Any help with understanding where I went wrong, and how to fix it in the future would be much appreciated.

You didn't provide the method the necessary parameters for it to execute properly.

Method copy is accepting three parameters: currentString , startPosition , onePastLastPosition as per the parameters specified:

public static String copy(String currentString, int startPosition, int onePastLastPosition) {

So you should be calling the method like so:

System.out.println("The new string is: " + copy(currentString, startPosition, onePastLastPosition));

Test Run

Copying - Enter a string
spectric
The current string is: spectric
Enter the starting position
1
Enter one past the ending position
2
The new string is: p

Your copy method is in different class.You have to create the second class instance in first class to call the second class function.

First class

System.out.println("Copying - Enter a string");
        ....   
System.out.println("The new string is: " + 
SecondClass.copy(currentString));

P/S : Make sure the number of the parameters are correct.

您的copy方法是static ,因此您应该使用类前缀调用该方法,并传递您声明的其他参数( startPositiononePastLastPosition )。

System.out.println("The new string is: " + YourClassName::copy(currentString, startPosition, onePastLastPosition))

Two things.

  • your parameters to copy are different than the method signature shows.
  • you should be calling Copy as a static by prefixing the name of the containing class.

The copy method requires 3 parameters, but you have only provided 1. Add the other two:

copy(currentString, startPosition, onePastLastPosition)

Add an import statement for the copy method:

import static SecondClass.copy;

Or qualify the method:

SecondClass.copy(currentString, startPosition, onePastLastPosition)

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