我是一名学生,正在尝试完成实验室作业。
我有两个文件Sentence.java
和Sentence.Tester.java
,但我对填写公共String findWord()
方法感到困惑和public String insertSecondWord()
虽然我可以完全错误地完成这两个文件。
findWord 方法应该返回句子中给定单词的索引位置,但不会基于用户输入(insertSecondWord()使用输入)取决于?
findWord()和insertSecondWord()
方法以便它们正常工作吗?
谢谢:)
编辑1 :为我遇到问题的两种方法添加新代码,但现在我遇到了7次错误
错误:找不到45,67,76和85上的符号以及56上的两个错误以及错误的方法正文或声明摘要。
(这些可能只是我没有抓到的错误,但我已经在这个和另一个实验室工作了一整天,并且java脑死了)。
/**
*A Sentence provides information about the contents of a sentence.
*/
public class Sentence
{
// instance field (only one!)
private String phrase;
/**
* Constructs a Sentence object with a given String
* @param phrase the selected phrase
*/
public Sentence(String sentence)
{
phrase = sentence;
}
/**
* Returns the number of characters in the sentence
* @return the number of characters in the sentence
*/
// add the remaining methods as described
public int characterCount()
{
int count = phrase.length();
return count;
}
/**
* Returns the first word of the sentence
* @return the first word
*/
public String firstWord()
{
String first = phrase.substring(0, sentence.indexOf(" "));
}
/**
* Returns the last word of the sentence
* @return the the last word of the sentence
*/
public String lastWord()
{
String last = phrase.substring(sentence.lastIndexOf(" "), sentence.length());
}
/**
* find returns the index location of a given word in the sentence
* @param the word being searched for
* @return the index location
*/
public int findWord(String word);
{
int find = indexOf(word);
}
/**
* changes the sentence by inserting a given word after the existing first word
* @param second the word being added
*/
public void insertSecondWord(String second)
{
String replace = replace.phrase(" ", second);
}
/**
* returns the sentence
* @return the sentence
*/
public String getSentence()
{
return sentence;
}
}
// The Scanner is ONLY in the tester, not Sentence. Use string methods in Sentence
import javax.swing.JOptionPane;
/**
* A class to test the CoinCounter class
*/
public class CoinCounterTester
{
/**
* Tests methods of the CoinCounter class
* @param args not used
*/
public static void main(String[] args)
{
CoinCounter coinCounter = new CoinCounter(int quarters, int dimes, int nickels, int pennies);
String penny = JOptionPane.showInputDialog("Enter the quantity of pennies");
int pennies = Integer.parseInt(penny);
String nickel = JOptionPane.showInputDialog("Enter the quantity of nickels");
int nickels = Integer.parseInt(nickel);
String dime = JOptionPane.showInputDialog("Enter the quantity of dimes");
int dimes = Integer.parseInt(dime);
String quarter = JOptionPane.showInputDialog("Enter the quantity of quarters");
int quarters = Integer.parseInt(quarter);
System.exit(0);
}
}
答案 0 :(得分:4)
我不认为为您编写解决方案会有所帮助,但这里有您需要的部分。 :)
indexOf()
可以使用多种差异方式,而不仅仅是char
查找!您可以使用indexOf("these words")
在字符串中搜索“这些单词”的第一个索引位置。replaceFirst()
非常强大。例如,您将字符串“My dog loves treats”更改为“My”我的猫喜欢对待“replaceFirst(" dog", " cat")
。祝你好运!
哦,我可以补充说你的方法有一些奇怪的返回值和参数! insertSecondWord()
接受一个字符串并且不返回任何内容,因此它应该是public void insertSecondWord(String word)
。 findWork()
接受一个字符串并返回整数索引,因此它应该是public int findWord(String word)
。