Reversing a Sentence with Java - No Loops

In summary: I haven't done any Java programming for about 16 years, and don't have a compiler for it installed, so I can't give you any examples. Take a look at the documentation for the String class, http://download.oracle.com/javase/1.5.0/docs/api/java/lang/String.html. Some methods that might be of use are indexOf, for finding where the space characters are located, and substring. Split might be useful, also.
  • #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.
 
Technology news on Phys.org
  • #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. :yuck:
 
  • #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.
 
<h2>1. What is the purpose of reversing a sentence with Java?</h2><p>The purpose of reversing a sentence with Java is to change the order of the words in a sentence so that they are in reverse order. This can be useful for various applications such as creating anagrams or for data manipulation.</p><h2>2. Can you reverse a sentence in Java without using loops?</h2><p>Yes, it is possible to reverse a sentence in Java without using loops. This can be achieved by using the StringBuilder class and its reverse() method.</p><h2>3. How does the StringBuilder class reverse a sentence in Java?</h2><p>The StringBuilder class has a reverse() method that reverses the characters in a string. This method takes the original string and creates a new string with the characters in reverse order.</p><h2>4. Are there any limitations to reversing a sentence with Java?</h2><p>One limitation to reversing a sentence with Java is that it only works for single sentences. It cannot handle multiple sentences or complex punctuation.</p><h2>5. Is reversing a sentence with Java an efficient process?</h2><p>Yes, reversing a sentence with Java using the StringBuilder class is an efficient process. It has a time complexity of O(n) which means it can handle large sentences without significantly impacting performance.</p>

1. What is the purpose of reversing a sentence with Java?

The purpose of reversing a sentence with Java is to change the order of the words in a sentence so that they are in reverse order. This can be useful for various applications such as creating anagrams or for data manipulation.

2. Can you reverse a sentence in Java without using loops?

Yes, it is possible to reverse a sentence in Java without using loops. This can be achieved by using the StringBuilder class and its reverse() method.

3. How does the StringBuilder class reverse a sentence in Java?

The StringBuilder class has a reverse() method that reverses the characters in a string. This method takes the original string and creates a new string with the characters in reverse order.

4. Are there any limitations to reversing a sentence with Java?

One limitation to reversing a sentence with Java is that it only works for single sentences. It cannot handle multiple sentences or complex punctuation.

5. Is reversing a sentence with Java an efficient process?

Yes, reversing a sentence with Java using the StringBuilder class is an efficient process. It has a time complexity of O(n) which means it can handle large sentences without significantly impacting performance.

Similar threads

  • Programming and Computer Science
Replies
8
Views
1K
  • Programming and Computer Science
Replies
8
Views
1K
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
3
Views
744
  • Programming and Computer Science
Replies
1
Views
6K
  • Programming and Computer Science
Replies
1
Views
6K
  • Programming and Computer Science
Replies
14
Views
2K
  • Programming and Computer Science
Replies
1
Views
7K
  • Programming and Computer Science
Replies
3
Views
5K
  • Programming and Computer Science
Replies
2
Views
1K
Back
Top