开发者

Need to create a user based input file

开发者 https://www.devze.com 2023-04-13 04:50 出处:网络
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 inse

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.

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 ?

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?

Thanks :)

Edit 1: Put in the new code for the two methods I am having problems with but now I am getting 7 errors

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.

(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).

/**
 *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. It will be invaluable for any sentence parsing utilities you may need.
  2. indexOf() can be used several difference ways, not just char finding! You can search for the first index location of "these words" in a string with indexOf("these words").
  3. replaceFirst() is quite powerful. For example, you change the string "My dog loves treats" to "My "My cat loves treats" with replaceFirst(" dog", " 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). findWork() takes a string and returns and integer index, so it should be public int findWord(String word).

0

精彩评论

暂无评论...
验证码 换一张
取 消