【问题标题】:Two problems, using charAt for undefined input and looping output两个问题,使用charAt进行未定义输入和循环输出
【发布时间】:2016-12-03 07:15:59
【问题描述】:

所以,我昨天发布了这个几乎相同的代码,询问如何在使用 .split 后将标点符号留在倒置句子的末尾。我仍在为此苦苦挣扎,但我也遇到了相同代码的另一个问题:这是我的屏幕截图http://i.stack.imgur.com/peiEA.png

import java.util.Scanner;
import java.util.StringTokenizer; // for splitting


public class MyTokenTester
{
    public static void main(String\[\] args)
    {
        Scanner enter = new Scanner(System.in);
        String sentinel = ""; // condition for do...while
        String backward = ""; // empty string 
        char lastChar = '\0';




        do
        {

        System.out.println("Please enter a sentence: ");
        String sentence = enter.nextLine();
        String\[\] words = sentence.split(" "); // array words gets tokens
    //  System.out.printf("The string is%s",sentence.substring(sentence.length()));


        for (int count = words.length -1; count>=0; count--) // reverse the order and assign backward each token
        {
            backward += words\[count\] + " ";


        }


        System.out.println(backward); // print original sentence in reverse order

        System.out.println("Hit any key to continue or type 'quit' to stop now: ");
            sentinel = enter.nextLine();
                sentinel = sentinel.toLowerCase(); // regardless of case






        } while (!sentinel.equals("quit")); // while the sentinel value does not equal quit, continue loop

        System.out.println("Programmed by ----");



    } // end main



} // end class MyTokenTester][1]][1]

正如你们可能从屏幕截图中看到的那样,当提示用户添加另一个句子时,前一个句子会被再次读回。

我的问题是:

  1. 如何使用charAt 识别未定义索引处的字符(用户输入的长度不同)
  2. 如何在用户决定继续阅读后停止我的句子。

再一次,正如我所说,我昨天发布了这段代码,但线程死了,我还有其他问题在原帖中没有提到。

【问题讨论】:

  • 我尝试在输出中添加屏幕截图。链接在这里。 [1]:i.stack.imgur.com/peiEA.png
  • 如果你在每个循环结束时都执行String sentence = null;String[ ] words = null; 以便它可以被垃圾回收呢?
  • 您能否澄清您在第 1 部分中的意思?具体来说,“未定义的索引”是什么意思
  • 我正在尝试做的部分事情是反转一句话:你好,我的名字是鲍勃!应该变成 Bob 是我的名字 你好!因此,整个句子颠倒过来,但标点符号保留在末尾。有人建议我使用 charAt() 来查找句子中的最后一个字符,然后将其放在末尾。我不知道如何将 charAt() 用于我不知道最后一个索引在哪里的字符串,因为它是确定长度的用户。

标签: java output charat


【解决方案1】:

为了解决第 2 部分,如果您想阻止句子回读先前的输入,则将 backward 重置为空字符串,因为就目前而言,您不断地向变量添加新单词。因此,要解决此问题,请在 do-while 循环结束之前添加这行代码,

backward = "";

要解决第 1 部分,如果您想检查字符串中的最后一个字符,那么首先您必须知道该字符串的最后一个索引是什么。好吧,一个字符串的索引从0str.length()-1。因此,如果您想访问用户输入中的最后一个字符,只需执行以下操作即可访问 words 数组(索引从 0words.length - 1)中的最后一个单词,

words[count].charAt(words[count].length() - 1);

请注意,count 只是 words.length - 1,因此可以根据自己的喜好进行更改。

【讨论】:

  • 谢谢,这回答了我问题的第 2 部分。我以前也遇到过同样的问题,循环后我不会重置东西。非常感谢。
  • @srmjr 检查我对第 1 部分的编辑。也很高兴我能提供帮助。如果您发现答案有帮助,请不要忘记将其标记为已接受。祝你编码好运!
  • 所以,我添加了这个:lastChar = words[count].charAt(words[count].length() - 1);出于调试目的,在循环内,与 System.out.println() 一起使用,在循环外但是,当我输入 Hello my name is Bob! 时,它给了我第一个单词的最后一个字母(我相信),它给了我'o'而不是感叹号
  • @srmjr 这是因为您在循环完成后放置该行,所以到那时 count 将是 0。要解决此问题,请将 count 替换为 words.length() - 1 并将其更改为 @ 987654335@
【解决方案2】:

1) 所以你有这个字符串数组words。在将每个单词添加到backward 字符串之前,您可以使用类似:words[count].chartAt(words[count].length() - 1)。它将返回该单词最后位置的字符。现在您可以检查它是字母还是任何特殊字符。

2) 问题不在于它再次读取上一行,问题在于backward 字符串仍然有之前的结果。当您使用+ 运算符来设置字符串的值时,它将继续将其与先前的结果相加。您应该在处理其他输入之前清理它以获得您想要的结果。

【讨论】:

  • @srmjr 刚刚添加了第 1 部分
  • 我可以设置两个答案来纠正吗?我有你们两个,都对我帮助很大。所以这行代码似乎给出了句子中第一个单词的最后一个字符,我猜是因为它们已经被标记了?我正在尝试查找总字符串中的最后一个字符。
  • @srmjr 你不能,但这没问题。我以为您想检查每个单词(也许句子中间有逗号或类似的东西)。因此,如果您只想检查输入字符串的结尾,可以使用sentence.chartAt(sentence.length() - 1)。很高兴为您提供帮助!
【解决方案3】:

这是你的代码:

import java.util.*;

public class main{

public static void main(String[] args){
    Scanner enter = new Scanner(System.in);
    String sentinel = ""; // condition for do...while
    String backward = ""; // empty string 
    char lastChar = '\0';

    do
    {
        System.out.println("Please enter a sentence: ");
        String sentence = enter.nextLine();
        String[] words = sentence.split(" "); // array words gets tokens
        //  System.out.printf("The string is%s",sentence.substring(sentence.length()));
        List<String> items = Arrays.asList(words);
        Collections.reverse(items);

        System.out.println(generateBackWardResult(items)); // print original sentence in reverse order
        System.out.println("Hit any key to continue or type 'quit' to stop now: ");
        sentinel = enter.nextLine();
        // i use quals ignore case, makes the code more readable
    } while (!sentinel.equalsIgnoreCase("quit")); // while the sentinel value does not equal quit, continue loop

    System.out.println("Programmed by ----");
} // end main

static String generateBackWardResult(List<String> input){
    String result="";
    for (String word:input){
        result =result +" "+word;
    }
    return result;
 }
} // end class MyTokenTester][1]][1]

还有一点要提: *永远不要再发明轮子了! (为了恢复数组,java util 包中有很多方法,请使用它们。) *编写干净的代码,完成每个功能,我是一个单独的方法。在您的情况下,您正在以一种方法进行还原并显示结果。

【讨论】:

    猜你喜欢
    • 2021-12-25
    • 2017-11-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多