-6

Is there is custom way of inserting punctuation marks in a java String?

Lets say i have this small program:

class Punctuation Marks {
public static void main (String[]args) {

String Greetings="Hello how are you";

System.out.println("Greetings");
}
}

Intended output: Hello,how are you.

3 Answers3

2

How about this:

String greetings = "Hello" + "," + " how are you";

Or this:

String greetings = "Hello how are you";
greetings = greetings.substring(0, 5) + "," + greetings.substring(5);

Or this:

String greetings = "Hello how are you";
greetings = new StringBuilder(greetings).insert(5, ",").toString();

Inserting a punctuation mark is trivial if you know where it should go. But if you don't know the exact place beforehand, it's impossible!

Óscar López
  • 232,561
  • 37
  • 312
  • 386
1

Inserting the punctuation at a given location is easy, just use StringBuilder.insert(int index, char toInsert); Programmaticly determining where the punctuation belongs, and what kind to use, is damn near impossible.

Mike
  • 2,434
  • 1
  • 16
  • 19
0

The gist of the answers is that automatic punctuation is nigh impossible.

Why?

Basically because there are a lot of sequences of words that could be punctuated in different ways to mean different things. For example.

  the cow jumped over the hill I saw another cow

can be punctuated as

  The cow jumped.  Over the hill I saw another cow. 

or

  The cow jumped over the hill.  I saw another cow.

which clearly mean different things. (Can you tell me which is the right one? Why? And are you still correct if there are moles in the field?)

Basically, deciding which of the possible alternatives is the "correct" one requires deep understanding of what the punctuated utterances mean ... in the context that they appear. This is most likely beyond the state of the art of natural language processing, and certainly not something that a run-of-the-mill application should attempt.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216