2015-01-16 29 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); 

}