java 查找单词位置_java – 在一个句子中查找任何单词

第一篇文章和所有这些.我是新手程序员.

无论如何,我的任务是创建一个Java程序,它将以句子和单词的形式接受用户输入.该程序从句子中删除空格,并检查该单词是否出现在“无空白”句子中.但是,该程序还会从单词的末尾删除一个字母,并检查该单词是否出现在无空白句子中.该程序继续删除该单词中的字母,直到没有更多的字母要删除.

此外,程序还应该说明单词的位置,但不能多次列出一个位置.如果程序找不到整个单词,则打印出“未找到’Word’.”如果是,则打印出”Word’在位置’x’处找到“

例如如果我的句子是“她在河边唱歌”并且字是“字节”,那么代码应该检查“shesingsbytheriver”中的“byte”,“byt”,“by”和“b”,但它找不到“byt”,“by”和“b”位于同一位置.

以下是我的代码.一切都很好,直到我的if语句.它没有找到空白句子中的单词,而是继续打印出“未找到’Word’.”

最后几点说明:我应该避免使用数组,我需要的大多数命令都是String类.

谢谢!

// The purpose of this program is to take in user input in the form

// of a sentence and a word. The program repeats the sentence and word

// back, removes the spaces, and checks if the word was present in the

// sentence. The program removes a letter from the word, checks if that

// "word" is present and continues until it cannot remove any more letters.

import java.util.*;

import javax.swing.JOptionPane;

public class Program1 {

public static void main(String[] args) {

String sentenceBlankless;

String sentence = JOptionPane.showInputDialog("Please enter a sentence: ");

String word = JOptionPane.showInputDialog("Please enter a word: ");

sentenceBlankless = sentence.replaceAll(" ", "");

JOptionPane.showMessageDialog(null, "The original imput is: " + sentence);

JOptionPane.showMessageDialog(null, "Removing blanks - " + sentenceBlankless);

JOptionPane.showMessageDialog(null, "Input word - " + word);

for (int x = 0; x < word.length(); x++) {

if (sentenceBlankless.toLowerCase().contains(word.toLowerCase())) {

int loc = sentence.toLowerCase().indexOf(word.toLowerCase());

JOptionPane.showMessageDialog(null, word.substring(0, word.length() - x) + " was found at location " + loc);

} else {

JOptionPane.showMessageDialog(null, word.substring(0, word.length() - x) + " was not found");

}

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在文件插入一个单词,可以按照以下步骤进行: 1. 创建一个 File 对象,指向要操作的文件。 2. 使用 FileReader 和 BufferedReader 读取文件内容。 3. 使用 StringBuilder 将读取的内容存储到内存。 4. 在 StringBuilder 查找要插入单词位置。 5. 使用 StringBuilder 的 insert() 方法在指定位置插入单词。 6. 使用 FileWriter 和 BufferedWriter 将修改后的内容写回到文件。 下面是一个示例代码: ```java import java.io.*; public class InsertWordToFile { public static void main(String[] args) { String fileName = "example.txt"; String wordToInsert = "hello"; try { // 创建 File 对象 File file = new File(fileName); // 读取文件内容 FileReader fileReader = new FileReader(file); BufferedReader bufferedReader = new BufferedReader(fileReader); StringBuilder stringBuilder = new StringBuilder(); String line; while ((line = bufferedReader.readLine()) != null) { stringBuilder.append(line).append("\n"); } bufferedReader.close(); // 在 StringBuilder 插入单词 int index = stringBuilder.indexOf("world"); stringBuilder.insert(index, wordToInsert + " "); // 将修改后的内容写回到文件 FileWriter fileWriter = new FileWriter(file); BufferedWriter bufferedWriter = new BufferedWriter(fileWriter); bufferedWriter.write(stringBuilder.toString()); bufferedWriter.close(); System.out.println("单词插入成功!"); } catch (IOException e) { e.printStackTrace(); } } } ``` 在上面的示例,我们将单词 "hello" 插入到了文件第一次出现单词 "world" 的位置。注意,这个示例没有处理异常情况,实际应用需要根据具体情况进行修改。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值