在Java中,从另一个类调用编辑字符串的方法时遇到了问题。

huangapple go评论67阅读模式
英文:

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

问题

我有一个简单的Java程序,它应该从字符串中提取指定的字符,并通过在第二个类的第二级方法中执行所有操作,将它们打印回用户,同时在第一个类中从用户接收输入。

我遇到的问题是,当我尝试在主方法中调用第二级方法时,我会收到一个错误,说:“在我的主方法中未找到'copy(String)' 方法。

我的主方法代码如下:

	System.out.println("复制 - 输入一个字符串");
	String currentString = input.next();
	System.out.println("当前字符串是:" + currentString);
	System.out.println("输入起始位置");
	int startPosition = input.nextInt();
	System.out.println("输入结束位置");
	int onePastLastPosition = input.nextInt();
	System.out.println("新字符串是:" + copy(currentString));

而copy方法的代码如下:

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

所以举个例子,代码应该做的是,如果我输入一个字符串“abcd”,它会返回给我“bc”,但系统卡在第一个方法的最后一行。
对于理解我哪里出错以及如何将来修复它,任何帮助都将不胜感激。

英文:

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.

答案1

得分: 3

你没有为该方法提供必要的参数,以使其能够正确执行。

方法 copy 接受三个参数:currentStringstartPositiononePastLastPosition,如下所示:

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

因此,你应该像这样调用该方法:

System.out.println("新字符串为:" + copy(currentString, startPosition, onePastLastPosition));

测试运行

复制 - 输入一个字符串
spectric
当前字符串为:spectric
输入起始位置
1
输入结束位置的下一个位置
2
新字符串为:p
英文:

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

答案2

得分: 2

你的复制(copy)方法位于不同的类中。你需要在第一个类中创建第二个类的实例,以调用第二个类的函数。

第一个类:

System.out.println("复制 - 输入一个字符串");
        ....   
System.out.println("新字符串为:" + 
SecondClass.copy(currentString));

附注:确保参数的数量是正确的。

英文:

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.

答案3

得分: 1

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

System.out.println("新字符串为:" + YourClassName::copy(currentString, startPosition, onePastLastPosition))
英文:

Your copy method is static, so you should call the method with class prefix, and pass additional arguments (startPosition, onePastLastPosition) you declared.

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

答案4

得分: 1

你有两个问题。

  • 你要复制的参数与方法签名显示的不同。
  • 你应该通过在包含类的名称前加上前缀来调用Copy作为静态方法。
英文:

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.

答案5

得分: 1

复制方法需要3个参数,但是您只提供了1个。请添加另外两个参数:

    copy(currentString, startPosition, onePastLastPosition)

---

为复制方法添加导入语句:

    import static SecondClass.copy;

或者限定方法所在的类:

    SecondClass.copy(currentString, startPosition, onePastLastPosition)
英文:

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)

huangapple
  • 本文由 发表于 2020年10月18日 23:34:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/64415103.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定