Java Can';几天后我似乎不明白这个错误
你好,我正在做以下练习:Java Can';几天后我似乎不明白这个错误,java,string,Java,String,你好,我正在做以下练习: 字数:此方法应接受对字符串对象的引用作为参数,并返回对象中包含的字数 arrayToString:此方法将字符数组作为参数,并将其转换为字符串对象。该方法应返回对字符串对象的引用 Mostfrequent:此方法接受对字符串对象的引用作为参数,并返回对象中出现频率最高的字符 replacusubstring:此方法接受对字符串对象的三个引用作为参数。让我们把它们叫做stringl、string2和string3。它在stringl中搜索string2的所有引用。当它找
- 字数:此方法应接受对字符串对象的引用作为参数,并返回对象中包含的字数
- arrayToString:此方法将字符数组作为参数,并将其转换为字符串对象。该方法应返回对字符串对象的引用
- Mostfrequent:此方法接受对字符串对象的引用作为参数,并返回对象中出现频率最高的字符
- replacusubstring:此方法接受对字符串对象的三个引用作为参数。让我们把它们叫做stringl、string2和string3。它在stringl中搜索string2的所有引用。当它找到string2的一个匹配项时,会将其替换为string3,例如,假设这三个参数具有以下值:
- String1:“狗跳过了栅栏”
- String2:“the”
- 第三条:“那”
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);
}
}
任何帮助都可以
main()
方法的类可以访问另一个类,并正确导入该类以使用另一个类的方法。例如,如果类Demo
尝试使用类MiscellaneousStringClass
的方法,请解决依赖关系,并确保该类及其方法在Demo
类中可见MiscellaneousStringClass
方法必须公开才能从其他类访问为了调用在另一个类中定义的静态方法,需要用类名作为前缀(并导入该类)。在您的案例中,在类
演示中
:
MiscellaneousStringClass.wordCount(string1);
等等
然后,需要在文件.java和遵循包层次结构的文件夹中声明一个公共类(在您的情况下,没有包)
如果您想简化,可以在Demo.java
文件中包含MiscellaneousStringClass
,而不使用公共
声明。导入java.util.Scanner;
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);
}
}
公开课演示{
公共静态void main(字符串[]args){
String string1=“狗跳过了栅栏”;
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’};
//字数法
int numberOfWords=MiscellaneousStringClass.wordCount(string1);
System.out.println(“计算字数:“+string1”);
System.out.println(“给定字符串的字数:“+numberOfWords”);
//数组字符串法
字符串字符=MiscellaneousStringClass.arrayToString(charArray);
System.out.print(“\n数组中的七个字符:”);
for(int i=0;i
您正在使用两个公共类。这就是为什么您应该在单独的文件中编写这些类。文件名应该是类的名称,因此您需要了解如何读取显示的错误消息。我将插入换行符,以便能够引用消息的每个组件
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\newfolder\mischellaneousstring.java
:1:表示编译器在该文件的第1行发现了错误
错误接着说,因为它包含一个名为MiscellaneousStringClass的公共类,Java坚持文件名(不包括.Java扩展名)和公共类使用相同的名称
所以你有两个选择
然后你会有新的错误,请参阅AbbéRésina关于如何调用类的静态成员的回答。消息非常明确,你可能应该读一个基本的Yea,这就是我所想的,简单地将两个类放在一个Java文件中。这对你来说只是一个简化,但是将实用程序类保留在自己的文件中是有意义的:只需通过删除尾随的
…class
重命名该类,它就可以编译了。因此,我将所有文件组合在一起,但出于某种原因,仍然获得了上面定义的4个错误。很抱歉,这让人讨厌,但是如果我所有的类都在一个java文件中,那么我到底做错了什么呢?是的,我看到我做了这件事很愚蠢,并很快改变了它。谢谢你的回复。如果你不满意的话,不要对自己太苛刻
MiscellaneousStringClass.wordCount(string1);
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);
}
}
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 {