Java Reversing a Sentence with Java - No Loops

  • Thread starter Thread starter J.live
  • Start date Start date
  • Tags Tags
    Java Reverse
Click For Summary
The discussion focuses on reversing a sentence in Java without using loops, with participants exploring various methods to achieve this. One suggested approach involves splitting the input string into words based on spaces, then reversing the order of these words using array indexing. Participants also discuss the use of Java's String methods like substring and charAt, and the importance of understanding how to manipulate strings effectively. There is a request for clarification on specific code snippets, indicating a need for simpler explanations of Java concepts. The conversation emphasizes the challenge of reversing sentences while maintaining word integrity without traditional looping constructs.
  • #31
"\n" is a control character, whereas "\\s" is a regular expression. They are two separate things.

what about spacing out "youarehowhello"? Do I use "\\s" to split that?
You can't. You have to put the spaces in while you are combining them into one string, or use the method I just posted.
 
Technology news on Phys.org
  • #32
J.live said:
Oh nice, got it. Thanks a lot Grep. I need to read up and practice what you have mentioned. I appreciate your help.
No problem. And you might want to make sure you look at TylerH's solution as well, just to understand it. It's probably the simplest way of all. Just go through the words in order, and keep adding each word to the start of some output string. Voila, reversed words. Simple and clear are winners in my book.

I have to admit, parsing strings "the hard way" might teach you more, but may be a little much to ask of you at this point.

Oh, and you might want to make sure everything is indented properly before submitting. It's a little off. And remove any extra blank lines that aren't needed. Keep ones that help make things clear and that separate groups of lines that belong together. It'll make it look nicer.
 
  • #33
This is how I would format your code.
EDIT: Add missing final brace.
Code:
import java.util.Scanner;


public class WordReverser
{
	
   public static void main(String[] args) 
   {
      String line, result[];
      Scanner keyboard = new Scanner (System.in);
      line=keyboard.nextLine();
		
      result = line.split("\\s");
      for (int i = result.length-1; i >= 0; i--)
      {
         System.out.print(result[i]);
      }
  }
}
 
Last edited:
  • #34
Mark44 said:
This is how I would format your code.
Except I'm guessing you wouldn't normally be missing a closing curly brace to close the function. :wink:

So you normally use 3 space indenting? You'd be one of the only people I've ever met that does that other than myself. I learned it when I was writing safety critical software where every little thing was scrutinized, and proper indentation (and thus clarity) was very important. I do it because it makes it obvious when someone has used tab characters for indenting since pretty much nobody has their tab stops set to 3. Same reason for you? Just curious.
 
  • #35
I prefer tabs to spaces because any good editor or IDE let's the user adjust tab length. Or... do you mean that you have you tab length set to 3?
 
  • #36
TylerH said:
I prefer tabs to spaces because any good editor or IDE let's the user adjust tab length. Or... do you mean that you have you tab length set to 3?
No, in most places I've worked, tabs were pretty much banned. I also don't like to use them, personally. Mostly, it's because not everyone tends to have their tabs set to the right length, and it becomes a mess when it's not set right. It seems like it shouldn't be a problem, but my experience has shown that it's a problem more often than I would have though. Basically, I don't want everyone who reads the code to have to mess with their tab stop settings. Spaces "just work".

Mind you, any editor I use is generally configured to give me 3 spaces (or whatever the standard is) when I press the tab key. So not much of a difference in practice.
 
  • #37
There's no difference in practice, *until someone wants something other than 3 spaces*. If everyone wants 3 space tabs, then all they have to do is set their tab stop settings to 3. It's when people differ in opinions, as they tend to, that the use of tabs becomes a godsend. In what case do tabs cause a mess?
 
  • #38
TylerH said:
There's no difference in practice, *until someone wants something other than 3 spaces*. If everyone wants 3 space tabs, then all they have to do is set their tab stop settings to 3. It's when people differ in opinions, as they tend to, that the use of tabs becomes a godsend. In what case do tabs cause a mess?
It's not so bad in a closed team, really.

But let's say you have it done with tabs 4 spaces long, and you send me your code. I load it in vi (which I do use frequently) where my tabs are 8 characters long by default. First, I probably have to look up how to change the tab stop settings, because I don't usually have to do that. And it's not always immediately obvious what the proper setting is. With spaces, it never comes up. Things can end up wrapping in a messy way viewing it at 8. But mostly, it's code indented like this:
Code:
int function(int a, int b,
             int c, int d)
{
}
Now imagine that to indent the "int c" line and line it up with "int a" above (a common practice - I do that for example), you use a tab followed by some spaces. If the tab stops aren't the same, it won't line up properly anymore, making it look messy.

It's not a huge thing (and I'm certainly not suggesting you're doing it wrong), but it's been enough of an annoyance that a lot of places I've worked make spaces the standard. Of course, the most important thing is that everyone agrees to do it the same way. Resolving change control conflicts because people don't agree on indentation is rather annoying.
 
  • #39
I see. I've come across that once. My solution was to deal with the offset, like so:
Code:
int function(int a, int b,
    int c, int d) // 5 spaces being my normal tab stop
{
}
I'll admit that the offset is annoying, but better than having to go through and change spaces to tabs so I can use my tab stop settings. :P

As for not knowing how to set vi's tab stop, which I don't either, I would say it's the employee's job to know his tools.

But, you're a professional with (probably) years of experience of dealing with others code. And I'm a one-man programming-team, who happens to still be in high school, and has never had to deal with the annoyance of making my code readable to anyone but me(not that I'm overly sloppy). So I really can't argue from the perspective of someone who would know which is better for a [more-than-one-man] team wide coding standards.
 
  • #40
Grep said:
Except I'm guessing you wouldn't normally be missing a closing curly brace to close the function. :wink:
Right! I removed a pair of braces that served no purpose, and I guess that somehow screwed up my count, plus I was being sloppy. It's fixed now.
Grep said:
So you normally use 3 space indenting? You'd be one of the only people I've ever met that does that other than myself. I learned it when I was writing safety critical software where every little thing was scrutinized, and proper indentation (and thus clarity) was very important. I do it because it makes it obvious when someone has used tab characters for indenting since pretty much nobody has their tab stops set to 3. Same reason for you? Just curious.

I usually use only two spaces per level of indentation. The reason for this is that the code that I and others write appears in online documentation. If tabs are used and there are many levels of nesting, some lines of code make the page very wide so that you have to scroll sideways to see the long lines. With two spaces per indent level, the code hierarchy is evident, and you don't have to scroll to see it.
 
  • #42
Mark44 said:
I usually use only two spaces per level of indentation. The reason for this is that the code that I and others write appears in online documentation. If tabs are used and there are many levels of nesting, some lines of code make the page very wide so that you have to scroll sideways to see the long lines. With two spaces per indent level, the code hierarchy is evident, and you don't have to scroll to see it.
Ah, makes sense. Mind you, I usually consider that, by the time it gets to that, someone has too many levels of nesting in the first place. And I don't mean to imply that you nest code excessively. :smile:

TylerH said:
The solution to spaces v tabs: http://nickgravgaard.com/elastictabstops/
I admit I like that solution. Of course, the problems in getting this idea in general use are likely self-evident. He'd have to cover a whole lot of editors, and do some serious marketing to get this to see the light of day. So I'm not hopeful to see this in general use. But I, personally, think it's a great idea.
 

Similar threads

  • · Replies 8 ·
Replies
8
Views
2K
Replies
8
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 3 ·
Replies
3
Views
1K
  • · Replies 1 ·
Replies
1
Views
7K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 1 ·
Replies
1
Views
6K
Replies
1
Views
8K
  • · Replies 2 ·
Replies
2
Views
2K