简体   繁体   English

需要创建一个基于用户的输入文件

[英]Need to create a user based input file

I am a student trying to finish a lab assignment. 我是一名试图完成实验作业的学生。
I have two files, Sentence.java and Sentence.Tester.java but I'm confused about filling in the public String findWord() method and public String insertSecondWord() though I could have done both files completely wrong. 我有两个文件, Sentence.javaSentence.Tester.java但是我对填写公共String findWord()方法和public String insertSecondWord()感到困惑,尽管我可能完全错了两个文件。
The findWord method is supposed to returns the index location of a given word in the sentence, but wouldn't that be based on user input(Also the insertSecondWord() uses input) depends on the ? 应该使用findWord方法返回句子中给定单词的索引位置,但是这不是基于用户输入的(而且insertSecondWord()使用输入)取决于?

However the teacher said the scanner method is only in the tester and only use string methods in the sentence. 但是老师说,扫描仪方法仅在测试仪中,句子中仅使用字符串方法。 Could someone please explain how edit the findWord() and insertSecondWord() methods so they work correctly? 有人可以解释一下如何编辑findWord()insertSecondWord()方法以使其正常工作吗?

Thanks :) 谢谢 :)

Edit 1 : Put in the new code for the two methods I am having problems with but now I am getting 7 errors 编辑1 :输入我遇到问题的两种方法的新代码,但是现在出现7个错误

error: cannot find symbol on 45, 67, 76, and 85 as well as two errors on 56 as well as an error missing method body or declare abstract. 错误:在45、67、76和85上找不到符号,在56上找不到两个错误以及方法主体缺少错误或声明了抽象。

(These could be simply errors that I am not catching but I have worked on this and another lab all day and am java brain dead). (这些可能只是我没有发现的错误,但是我整天都在这个实验室和另一个实验室工作,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);
    }
}

I don't think it would be helpful to just write the solution for you, but here are the pieces you need. 我认为只为您编写解决方案不会有帮助,但这是您需要的部分。 :) :)

  1. This is the documentation for Java strings . 这是Java字符串的文档 It will be invaluable for any sentence parsing utilities you may need. 对于您可能需要的任何句子解析实用程序而言,它都是无价的。
  2. indexOf() can be used several difference ways, not just char finding! indexOf()可以使用几种不同的方式,而不仅仅是查找char You can search for the first index location of "these words" in a string with indexOf("these words") . 您可以使用indexOf("these words")在字符串中搜索“ these words”的第一个索引位置。
  3. replaceFirst() is quite powerful. replaceFirst()非常强大。 For example, you change the string "My dog loves treats" to "My "My cat loves treats" with replaceFirst(" dog", " cat") . 例如,使用replaceFirst(" dog", " cat")将字符串“ My dog喜欢治疗”更改为“ My” My cat喜欢治疗“。

Best of luck! 祝你好运!

EDIT: 编辑:

Oh, I might add that your methods have some odd return values and parameters! 哦,我可能会补充说您的方法有一些奇怪的返回值和参数! insertSecondWord() takes a string and returns nothing, so it should be public void insertSecondWord(String word) . insertSecondWord()接受字符串,不返回任何内容,因此应为public void insertSecondWord(String word) findWork() takes a string and returns and integer index, so it should be public int findWord(String word) . findWork()接受字符串并返回整数索引,因此应为public int findWord(String word)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM