且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在Java中向后打印多个字符串

更新时间:2023-02-12 18:39:34

它看起来像你的变量 backwardsWord 总是追加一个字符而不是在字之间重置。最简单的解决方法是通过将 backwardsWord 设置为空字符串来清除 backwardsWord

  backwardsWord =; //清除任何现有的字符backwardsWord 

for(int i = upperCaseWord.length() - 1; i> = 0; i--)
{
backwardsWord + =(upperCaseWord.charAt(i)+);





如果你正在建立一个字符串一个字符一次您将使用大量的内存,因为 Java字符串是不可变的。 / p>

为了更有效地使用 StringBuilder 。这是为了建立像你在做什么字符。一旦你完成了,你可以使用 toString 方法来取出字符串。

  StringBuilder builder = new StringBuilder(); //创建存储字符的字符串生成器
for(int i = upperCaseWord.length() - 1; i> = 0; i--)
{
builder.append( upperCaseWord.charAt(I)); //追加一个字符

backwardsWord = builder.toString(); //将已完成的字符串存储到现有变量中

每次重置backwardsword 。


最后,因为你的目标是把字符串反过来,所以我们实际上可以在没有循环的情况下完成它。在这个答案中显示

  backwardsWord = new StringBuilder(upperCaseWord).reverse()。toString()

这会创建一个新的 StringBuilder ,其中 upperCaseWord 中的字符反转字符,然后将最终字符串存储在 backwardsWord


I am trying to take a file full of strings, read it, then print out a few things:

  • The string
  • The string backwards AND uppercase
  • The string length

There are a few more things, however I haven't even gotten to that point and do not want to ask anyone to write the code entirely for me. After messing around with it for a while, I have it almost completed (I believe, save for a few areas).

The piece that is tripping me up is the backwards word. We are required to put our output neatly into columns using prinf, but I cannot do this if I read each char at a time. So I tried setting a String backwardsWord = ""; and adding each character.

This is the piece that is tripping me up:

for(int i = upperCaseWord.length() - 1; i >= 0; i--)
{
    backwardsWord += (upperCaseWord.charAt(i) + "");
}   

My issue is that when I print it, the first word works properly. However, each word after that is added to the previous word.

For example: if I am printing cat, dog, and rat backwards, it shows

TAC

TACGOD

TACGODTAR

I obviously want it to read

TAC

GOD

TAR

Any help would be appreciated.

It looks like your variable backwardsWord is always appending a character without being reset between words. The simplest fix is to clear the backwardsWord just before your loop by setting it to empty string.

backwardsWord = ""; //Clear any existing characters from backwardsWord

for(int i = upperCaseWord.length() - 1; i >= 0; i--)
{
    backwardsWord += (upperCaseWord.charAt(i) + "");
}


If you are building up a String one character at a time you will be using a lot of memory because Java Strings are immutable.

To do this more efficiently use a StringBuilder instead. This is made for building up characters like what you are doing. Once you have finished you can use the toString method to get the String out.

StringBuilder builder = new StringBuilder(); //Creates the String builder for storing the characters
for(int i = upperCaseWord.length() - 1; i >= 0; i--)
{
    builder.append(upperCaseWord.charAt(i)); //Append the characters one at a time 
}
backwardsWord = builder.toString(); //Store the finished string in your existing variable

This has the added benefit of resetting the backwardsWord each time.


Finally, since your goal is to get the String in reverse we can actually do it without a loop at all as shown in this answer

backwardsWord = new StringBuilder(upperCaseWord).reverse().toString()

This creates a new StringBuilder with the characters from upperCaseWord, reverses the characters then stores the final string in backwardsWord