您好我正在做以下练习:
ReplacaSubstring:此方法接受对String对象的三个引用作为参数。 Lett将它们称为string1,string2和string3。它在stringl中搜索所有出现的string2。当它找到string2的出现时,它将其替换为string3,例如,假设这三个参数具有以下值:
使用这三个参数,该方法将返回对String对象的引用,其值为"该狗跳过该栅栏"。
我的以下代码(主要):
import java.util.Scanner;
public class Demo {
public static void main(String[] args) {
String string1 = "the dog jumped over the fence";
String string2 = "the";
String string3 = "that";
char[] charArray = { 'a', 'b', 'c', 'd', 'e', 'f', 'g',
'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p',
'q', 'r', 's', 't', 'u', 'v','w', 'x','y', 'z'};
// wordCount method
int numberOfWords = wordCount(string1);
System.out.println("Count the number of words: " + string1);
System.out.println("Number of words given string: " + numberOfWords);
// arrayToString method
String character = arrayToString(charArray);
System.out.print("\nGiven characters in the array:");
for(int i = 0; i < charArray.length; i++)
System.out.print(charArray[i] + " ");
System.out.print("\nString from given character array: " + character);
// mostFrequent method
char mostOccurrence = mostFrequent(string1);
System.out.println("\nGiven string to get the most occured characters: " + string1);
System.out.println("The most occured character in given string: " + mostOccurrence);
//replaceSustring method
String string = replaceSubstring(string1, string2, string3);
System.out.println("\nGiven string to modify: " + string1);
System.out.println("Replace for: " +string2);
System.out.println("After replacing, the new string: " + string);
}
}
此课程出现以下错误:
C:\Users\carlosm\Desktop\New folder\MiscellaneousString.java:1: error: class MiscellaneousStringClass is public, should be declared in a file named MiscellaneousStringClass.java
public class MiscellaneousStringClass {
^
1 error
常规班级档案:
public class MiscellaneousStringClass {
int numberOfWords;
/* method should accept a reference to a string object
* as an argument and return the number of words contain
* in the object
*/
private static int wordCount(String string1) {
int number = 0;
for(int i = 0; i < string1.length(); i++) {
char ch = string1.charAt(i);
if(Character.isWhitespace(ch)) {
number = number + 1;
}
}
return number + 1;
}
/* method should accept a char array as an argument and convert i to a string object.
* The method should return to the string object.
*/
private static String arrayToString(char[] charArray) {
return String.valueOf(charArray);
}
/* method accepts a reference to a string object as an argument
* and returns the character that occurs the most frequently
* in the object.
*/
private static char mostFrequent(String string1) {
char mostOccurrence = ' ';
int most = 0;
int j;
for(int i = 0; i < string1.length(); i++) {
int count = 0;
char ch = string1.charAt(i);
for(j = 0; j < string1.length(); j++) {
if(ch == string1.charAt(j))count = count + 1;
}
if(count >= most) {
most = count;
mostOccurrence = ch;
}
}
return mostOccurrence;
}
/* method accepts three references to string object as arguments.
* Call them string1, string2, and string3; when it finds an occurrence
* of string2, it replaces it with string 3.
*/
private static String replaceSubstring(String string1, String string2, String string3) {
return string1.replaceAll(string2, string3);
}
}
此课程出现以下错误:
C:\Users\carlosm\Desktop\New folder\Demo.java:13: error: cannot find symbol
int numberOfWords = wordCount(string1);
^
symbol: method wordCount(String)
location: class Demo
C:\Users\carlosm\Desktop\New folder\Demo.java:18: error: cannot find symbol
String character = arrayToString(charArray);
^
symbol: method arrayToString(char[])
location: class Demo
C:\Users\carlosm\Desktop\New folder\Demo.java:27: error: cannot find symbol
char mostOccurrence = mostFrequent(string1);
^
symbol: method mostFrequent(String)
location: class Demo
C:\Users\carlosm\Desktop\New folder\Demo.java:32: error: cannot find symbol
String string = replaceSubstring(string1, string2, string3);
^
symbol: method replaceSubstring(String,String,String)
location: class Demo
4 errors
任何帮助都可以。
答案 0 :(得分:2)
为了调用在另一个类中定义的静态方法,您需要在类名前面添加前缀(并向类import
添加前缀。在您的情况下,在课程Demo
中:
MiscellaneousStringClass.wordCount(string1);
等等......
然后,需要在文件.java中和在包层次结构后面的文件夹中声明公共类(在您的情况下没有包)。
如果您想简化,可以将MiscellaneousStringClass
添加到Demo.java
文件中,不包含 public
声明。
答案 1 :(得分:1)
首先确保任何Java公共类名称及其文件名匹配。
确保具有main()
方法的类可以访问其他类并正确导入以使用其他类的方法。例如,如果类Demo
正在尝试使用类MiscellaneousStringClass
的方法,请解决依赖关系并确保类&amp;它的方法在Demo
类中可见。
MiscellaneousStringClass
方法必须公开才能从其他类访问。
答案 2 :(得分:0)
import java.util.Scanner;
public class Demo {
public static void main(String[] args) {
String string1 = "the dog jumped over the fence";
String string2 = "the";
String string3 = "that";
char[] charArray = { 'a', 'b', 'c', 'd', 'e', 'f', 'g',
'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p',
'q', 'r', 's', 't', 'u', 'v','w', 'x','y', 'z'};
// wordCount method
int numberOfWords = MiscellaneousStringClass.wordCount(string1);
System.out.println("Count the number of words: " + string1);
System.out.println("Number of words given string: " + numberOfWords);
// arrayToString method
String character = MiscellaneousStringClass.arrayToString(charArray);
System.out.print("\nGiven characters in the array:");
for(int i = 0; i < charArray.length; i++)
System.out.print(charArray[i] + " ");
System.out.print("\nString from given character array: " + character);
// mostFrequent method
char mostOccurrence = MiscellaneousStringClass.mostFrequent(string1);
System.out.println("\nGiven string to get the most occured characters: " + string1);
System.out.println("The most occured character in given string: " + mostOccurrence);
//replaceSustring method
String string = MiscellaneousStringClass.replaceSubstring(string1, string2, string3);
System.out.println("\nGiven string to modify: " + string1);
System.out.println("Replace for: " +string2);
System.out.println("After replacing, the new string: " + string);
}
}
您正在使用两个公共课程。这就是为什么你应该在单独的文件中编写这些类。文件名应该是类的名称
答案 3 :(得分:0)
因此,您需要了解如何阅读所提供的错误消息。我将插入换行符,以便能够引用消息的每个组成部分......
C:\Users\carlosm\Desktop\New folder\MiscellaneousString.java:1:
error: class MiscellaneousStringClass is public,
should be declared in a file named MiscellaneousStringClass.java
public class MiscellaneousStringClass {
错误消息的第一个“行”命名发生编译错误的文件。
对于此错误,它是
C:\ Users \ carlosm \ Desktop \ New folder \ MiscellaneousString .java
:1:表示编译器在该文件的第1行发现了错误。
然后该错误继续说因为它包含一个名为 MiscellaneousStringClass 的 public 类,Java坚持认为文件名(不包括.java扩展名)和公共类使用一样的名字。
所以你有两个选择......
我倾向于选择第一个选项,因为在Java类中使用“Class”这个词有点过分而且有点奇怪。
然后您会遇到新的错误,请参阅AbbéRésina关于如何致电您班级静态成员的答案。