Reading a new line in a string with \n - java

When building a string, you can create newlines like so:
"This is the first line \n\n And this is the second line";
So, when running this portion of code all works well on the Android Emulator:
TextView newsTextArea = (TextView) view.findViewById(R.id.newsTextView);
newsTextArea.setText("Hello \n\n Whats up");
However, I have downloaded and parsed JSON from a web service we have created, and I have stored what I want in a variable like so:
GlobalSettings globalSettings = new GlobalSettings();
String newsText = globalSettings.getNews();
So the variable newsText equals a string, which lets say for arguments sake here is "Hello, this has two lines. \n\n Welcome to the test".
When I run the above TextView code like this, it outputs it with the \n\n as literal characters.
newsTextArea.setText(newsText);
How can it be done so that the variable newsText keeps the formatting?

im a good guesser..lol well there may be soo many reasons that i think
1. newsTextArea.setSingleLine(false);
2. newsTextArea.setMinLines(2); or newsTextArea.setMaxLines(50);//any figure
3. String newsText = globalSettings.getNews().replace("\\\n", System.getProperty("line.separator"));
4. String newsText = globalSettings.getNews().toString();
play with these methods and see if one works for ya or two lol

However, I have downloaded and parsed JSON from a web service we have created ...
There are great chances that your JSON WS returns the literate sequence \n as its output. Not the line-feed character (<LF>) as you expected it.
At this point you probably have two options:
Fix the web service to return the proper value
Change your client program to "patch" the faulty data in order to obtain the desired behavior. As of myself, I won't push toward that direction as it will soon become a maintenance nightmare (as soon as legitimate \ will pop out in your data).
Maybe it is time to post an other question, this time about your JSON web service ?

It is likely that wherever that text comes from in the first place, the backslash is getting escaped. For example, it may be coming from a hard-coded constant in your service, entered into a form by a human and then validated, or etc.
Look at your JSON in its raw format before it is parsed. The JSON spec says that a single backslash followed by n means the newline character. If it looks like ["Hello, this has two lines. \n\n Welcome to the test"] then you should be good, because the JSON parser should interpret the newline characters correctly. However, if it looks like this: ["Hello, this has two lines. \\n\\n Welcome to the test"], then the backslash character is being escaped, not the n.

Related

Java print string as unicode

I was processing some data tweeter using java. I read them from the file, do some process and print to the stdout.
The text in file looks like this:
"RT #Bollogosta319a: #BuyBookSilentSinners \u262fGain Followers\n\u262fRT This\n\u262fMUST FOLLOW ME I FOLLOW BACK\n\u262fFollow everyone who rts\n\u262fGain\n #ANDROID \u2026"
I read it in, and print it out to stdout. The output is supposed to be:
"RT #Bollogosta319a: #BuyBookSilentSinners ☯Gain Followers\n☯RT This\n☯MUST FOLLOW ME I FOLLOW BACK\n☯Follow everyone who rts\n☯Gain\n #ANDROID …"
But my output is like this:
"RT #Bollogosta319a: #BuyBookSilentSinners ?Gain Followers
?RT This
?MUST FOLLOW ME I FOLLOW BACK
?Follow everyone who rts
?Gain
#ANDROID ?"
So, it seems that I have two problems to deal with:
1. print the exact Unicode character instead of Unicode string
2. keep "\n" as it is, instead of a newline in the output.
How can I do this? (I'm really crazy about dealing with different coding in Java)
I don't know how you are parsing the file, but the method you are using seems to be interpreting escape codes (like \n and \u262f). To leave instances of \n in the file literally, you could replace \n with \\n prior to using whatever means of interpreting the escape codes. The \\ will be converted to a single \, and the n will be left alone. Have you tried using a plain java.io.FileReader to read the file? That may be simpler.
The Unicode symbols may actually be read correctly; many terminals do not support the full range of Unicode characters and print some symbol in place of those it does not understand. Perhaps your program prints ☯ and the terminal simply doesn't know how to render it, so it prints a ? instead.

Java - Determine difference between user entering \n and pressing enter in html form

Searched the web for this but not sure I'm asking the question correctly. I have a web form with a textarea. Users can type what ever they want (can paste emails, etc). When they submit, I escape things like newline so that when I store in a PostgreSQL db (json column type) it saves correctly. That all works fine. However, if a user type something like c:\foo\bar\notworking.txt the \n is treated like a new line so I end up with
c:\foo\bar
otworking.txt
If I look at the string (user hits enter) coming into the controller (Spring based) I see \n.
Question is, how do I differentiate between someone typing \n and hitting the enter key?
easiest solution:
String s = ...;
s = s.replaceAll("\\","\\\\");
Then the opposite after you load it back in
In order to insert raw text into a JSON column, you need to encode the text as JSON, meaning:
The following fails «badly»:
"c:\foo\bar\notworking.txt"
encodes to (assuming non-ASCII needs encoding, which it does if the DB is not UTF-8):
"The following fails \u00ABbadly\u00BB:\r\n \"c:\\foo\\bar\\notworking.txt\""
Side note: As a Java String literal, that would be:
String json = "\"The following fails \\u00ABbadly\\u00BB:\\r\\n \\\"c:\\\\foo\\\\bar\\\\notworking.txt\\\"\"";
You will then of course use PreparedStatement or Spring, such that SQL escaping and SQL Injection issues are non-existent.

is it possible to use replaceAll() with wildcards

Good morning. I realize there are a ton of questions out there regarding replace and replaceAll() but i havnt seen this.
What im looking to do is parse a string (which contains valid html to a point) then after I see the second instance of <p> in the string i want to remove everything that starts with & and ends with ; until i see the next </p>
To do the second part I was hoping to use something along the lines of s.replaceAll("&*;","")
That doesnt work but hopefully it gets my point across that I am looking to replace anything that starts with & and ends with ;
You should probably leave the parsing to a DOM parser (see this question). I can almost guarantee you'll have to do this to find text within the <p> tags.
For the replacement logic, String.replaceAll uses regular expressions, which can do the matching you want.
The "wildcard" in regular expressions that you want is the .* expression. Using your example:
String ampStr = "This &escape;String";
String removed = ampStr.replaceAll("&.*;", "");
System.out.println(removed);
This outputs This String. This is because the . represents any character, and the * means "this character 0 or more times." So .* basically means "any number of characters." However, feeding it:
"This &escape;String &anotherescape;Extended"
will probably not do what you want, and it will output This Extended. To fix this, you specify exactly what you want to look for instead of the . character. This is done using [^;], which means "any character that's not a semicolon:
String removed = ampStr.replaceAll("&[^;]*;", "");
This has performance benefits over &.*?; for non-matching strings, so I highly recommend using this version, especially since not all HTML files will contain a &abc; token and the &.*?; version can have huge performance bottle-necks as a result.
The expression you want is:
s.replaceAll("&.*?;","");
But do you really want to be parsing HTML this way? You may be better off using an XML parser.

Input Sanitizing to not break JSON syntax

So, in a nutshell I'm trying to create a regex that I can use in a java program that is about to submit a JSON object to my php server.
myString.replaceAll(myRegexString,"");
My question is that I am absolutely no good with regex and to add onto that I need to escape the characters properly as its stored in a string, and then also escape the characters properly inside the regex. good lordy.
What I came up with was this:
String myRegexString = "[\"',{}[]:;]"
The first backslash was to escape outer quotes to get a " in there. And then it struck me that {} and [] are also regex commands. Would I escape those as well? Like:
String myRegexString = "[\"',\{\}\[\]:;]"
Thanks in advance. In case it wasnt clear from examples above the only characters I really care about at this moment in time is:
" { } [ ] , and also ; : ' for general sqlinj protection.
UPDATE:
This is the final regex:
[\\Q\"',{}[\]:;\\E] for anyone else curious. Thanks Amit!
Why don't you use an actual JSON encoding API/framework? What you're doing is not sanitizing. What you're doing is corrupting the data. If my name is O'Reilly, I want it to be spelled O'Reilly, not OReilly. If I send a message containing [ or {, I want these to be in the messages. Use a framework or API that escapes those characters when needed rather than removing them blindly.
Googling for JSON Java will lead you to many APIs and frameworks.
Try something like
String myRegexString = "[\\Q\"',{}[]:;\\E]";
now the characters between \Q and \E are now treated as normal characters.

Parsing of data structure in a plain text file

How would you parse in Java a structure, similar to this
\\Header (name)\\\
1JohnRide 2MarySwanson
1 password1
2 password2
\\\1 block of data name\\\
1.ABCD
2.FEGH
3.ZEY
\\\2-nd block of data name\\\
1. 123232aDDF dkfjd ksksd
2. dfdfsf dkfjd
....
etc
Suppose, it comes from a text buffer (plain file).
Each line of text is "\n" - limited. Space is used between the words.
The structure is more or less defined. Ambuguity may sometimes be, though, case
number of fields in each line of information may be different, sometimes there may not
be some block of data, and the number of lines in each block may vary as well.
The question is how to do it most effectively?
First solution that comes to my head is to use regular expressions.
But are there other solutions? Problem-oriented? Maybe some java library already written?
Check out UTAH: https://github.com/sonalake/utah-parser
It's a tool that's pretty good at parsing this kind of semi structured text
As no one recommended any library, my suggestion would be : use REGEX.
From what you have posted it looks like the data is delimited by whitespace. One idea is to use a Scanner or a StringTokenizer to get one token at a time. You can then check the first char of a token to see if it is a digit (in which case the part of the token after the digit(s) will be the data, if there is any).
This sounds like a homework problem so I'm going to try to answer it in such a way to help guide you (not give the final solution).
First, you need to consider each object of data you're reading. Is it a number then a text field? A number then 3 text fields? Variable numbers and text fields?
After that you need to determine what you're going to use to delimit each field and each object. For example, in many files you'll see something like a semi-colon between the fields and a new line for the end of the object. From what you said it sounds like yours is different.
If an object can go across multiple lines you'll need to bear that in mind (don't stop partway through an object).
Hopefully that helps. If you research this and you're still having problems post the code you've got so far and some sample data and I'll help you to solve your problems (I'll teach you to fish....not give you fish :-) ).
If the fields are fixed length, you could use a DataInputStream to read your file. Or, since your format is line-based, you could use a BufferedReader to read lines and write yourself a state machine which knows what kind of line to expect next, given what it's already seen. Once you have each line as a string, then you just need to split the data appropriately.
E.g., the password can be gotten from your password line like this:
final int pos = line.indexOf(' ');
String passwd = line.substring(pos+1, line.length());

Categories