2015-01-16 27 views
0

如何在不使用單詞號碼選項的情況下修改程序? 請給我一個簡化的程序。從句子中刪除單詞的程序

我該做什麼才能使此程序在沒有字數的情況下工作?

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); 
    } 
} 
+0

你的意思是,刪除文字,他們的輸入,而不是讓他們給你這號,是什麼? –

+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(); 

} 

}

+0

nikhil你可以給程序而不使用字符串緩衝區類嗎? –

+0

nikhil你可以使用緩衝閱讀器,但不能與字符串緩衝區 –

+0

掃描儀尚未涵蓋多數民衆贊成在爲什麼 –

0

至於我可以看到你想從一個句子中刪除給定的詞。我的建議如下

  1. 讀句子(比如String str="hi, How are you"
  2. 帶你想從句子刪除單詞。 (說String strToRemove="are"

那麼你可以嘗試一些如下。

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

}