程序从句子中删除单词

时间:2015-01-16 05:30:39

标签: java

如何在不使用字数选项的情况下修改程序? 请给我一个简化的程序。

如果没有字号,我该怎么做才能使这个程序工作?

import     java.io.*;

class Sentence
{
    public static void main(String ar[])throws IOException
    {
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        System.out.println("Enter the sentence");
        StringBuffer sentence=new StringBuffer(br.readLine()+" ");
        System.out.println("Enter the word to be deleted");
        String word=br.readLine();
        int n=0;
        int wordno;
        System.out.println("Enter the word number");
        wordno=Integer.parseInt(br.readLine());
        int count=0;
        for(int i=0;i<sentence.length();i++)
        {
            char ch=sentence.charAt(i);
            if(ch==' ')
            {
                String str=sentence.substring(n,i);
                count=count+1;
                if(str.equals(word)&&count==wordno)
                {
                    sentence.delete(n,i);
                }
                n=i+1;
            }
        }

        System.out.println("The new sentence is : "+sentence);
    }
}

3 个答案:

答案 0 :(得分:0)

这是工作程序

package com.nikhil.string;

import java.io.IOException;

import java.util.Scanner;

public class demo1 {

public static void main(String[] args) throws IOException {

    String[] s;
    String sentence, word;

    Scanner sc = new Scanner(System.in);

    System.out.println("Enter the sentence");

    sentence = sc.nextLine();

    System.out.println("Enter the word to be deleted");
    word = sc.nextLine();

    String finalSentence = "";

    s = sentence.split(" ");

    for (int i = 0; i < s.length; i++) {
        if (word.equals(s[i])) {
            continue;
        } else {
            finalSentence += s[i] + " ";
        }

    }

    System.out.println("final sentence is :: " + finalSentence);
    sc.close();

}

}

答案 1 :(得分:0)

据我所知,你想从句子中删除给定的单词。我的建议如下

  
      
  1. 阅读句子(说String str="hi, How are you"
  2.   
  3. 从句子中删除要删除的单词。                                (比如String strToRemove="are"
  4.   

然后你可以尝试以下内容。

    String str = "hi, How are you?. are you there?";
    String strToRemove = "are";
    StringBuilder stringBuilder = new StringBuilder();
    stringBuilder.append(str);
    boolean status=true;
    while (status){
        int index=stringBuilder.indexOf(strToRemove);
        if(index!=-1) {
            stringBuilder.delete(index, index + strToRemove.length()+1);
            // +1 will remove a space
        }else {
            status=false;
        }
    }
    System.out.println(stringBuilder.toString());

答案 2 :(得分:0)

public static void main(String ar[]) throws IOException {

    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

    System.out.println("Enter the sentence");

    StringBuffer sentence = new StringBuffer(br.readLine() + " ");

    System.out.println("Enter the word to be deleted");

    String word = br.readLine();

    String result = sentence.toString().replace(word, "");

    System.out.println("The new sentence is : " + result);

}