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
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments