Win a copy of The Business Blockchain this week in the Cloud forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Text File To String, For Use With EULA Script

 
Jared Snyder
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am utilizing a pretty slick tutorial to include an EULA in my app...

Android: A Simple Eula for your Android Apps

It's slick in that it will appear for the user to re-accept each time the version number is increased in the app's manifest file.

The text for the EULA, as well as any relevant update information is being called from strings in strings.xml. Instead of that, due to the length of text for my EULA, I would like to pull it from a text file. How would I go about doing that?

The full code is available at the link above, but the snippet that deals with the aforementioned, specifically, is this:

 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 35266
383
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It looks like this does what you want.

The gist is that you store the file in res/raw folder and then call


From that point, it is just reading from an input stream in Java.
 
Jared Snyder
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Below is the code I am using, but my text is being truncated. I only see, like, the first paragraph in the dialog window.


 
Ulf Dittmer
Rancher
Posts: 42969
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Line 10 only reads a single line from the file - is all the content in that first line? If not, you should loop over all lines in that file.
 
Jared Snyder
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What would a loop for such a purpose even look like?
 
Ulf Dittmer
Rancher
Posts: 42969
73
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Something like this:


By the way, variable names in Java always start in lowercase. While uppercase is permissible, it will confuse people who read your code to no end, because identifiers starting with uppercase letters are customarily used for class names.
 
Jared Snyder
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

By the way, variable names in Java always start in lowercase. While uppercase is permissible, it will confuse people who read your code to no end, because identifiers starting with uppercase letters are customarily used for class names.


Good catch! I actually am aware of this convention, but I overlooked it in the aforementioned block of code. It is a block I borrowed from someone's class, so perhaps we should tell them of their error. ;)


 
Jared Snyder
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've opted to try something per what I've posted below. However, I think there is something wrong with my loop, because nothing is appearing in my alert dialog.



 
Jared Snyder
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeanne Boyarsky wrote:It looks like this does what you want.

The gist is that you store the file in res/raw folder and then call


From that point, it is just reading from an input stream in Java.



Thank you for your response! However, this won't work, because openRawResource doesn't work with Strings.
 
Irina Goble
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jared Snyder wrote:I've opted to try something per what I've posted below. However, I think there is something wrong with my loop, because nothing is appearing in my alert dialog.


If you look at your code, can you tell what it is doing? Just line by line. Did you get a stack trace? Your users might be not so lucky
 
Paul Clapham
Sheriff
Posts: 21542
33
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, there is something wrong with your loop. Why aren't you using the idiom suggested by Ulf Dittmer? Why aren't you accumulating the data into the EULA string? What's with that loop which goes 21 times for each line?

I'm assuming you just want to read all the data in that resource and store it into a String, though. But maybe I've mistaken the requirements. If so, could you please explain them?
 
Jared Snyder
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am totally lost!

I've been working on this same issue all day, and posting between here and Stack Overflow.

A couple things I've learned conclusively are that openRawResource doesn't work with Strings, and that my loop is pretty useless.
 
Ulf Dittmer
Rancher
Posts: 42969
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please post the link to the topic on StackOverflow: http://www.coderanch.com/how-to/java/BeForthrightWhenCrossPostingToOtherSites

Address the 3 points raised by Paul, and your loop should work fine:
Why aren't you using the idiom suggested by Ulf Dittmer? Why aren't you accumulating the data into the EULA string? What's with that loop which goes 21 times for each line?

If you don't understand one of them, just ask.
 
Jared Snyder
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Uh oh! I don't want to upset the Code Ranch crowd. I like the Code Ranch crowd!

Here is my post on Stack Overflow: http://stackoverflow.com/questions/22337892/loop-statement-for-bufferedreader-is-not-working
The suggestion I was getting there was completely different than what I was getting here. I would prefer to tweak my current loop.

1. I don't have a good reason. I'm also not sure what I should do to line. I tried to tinker with it a bit.

2. It didn't dawn on me that the data wasn't accumulating into the EULA string.

3. Since I am using readLine(), I thought I had to run that line 21 times to read all 21 lines of my text file.
 
Ulf Dittmer
Rancher
Posts: 42969
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The good reason would be that it works that way "line" is what you should be adding to your "eulaContent" string. The "while" does all the looping, and it's independent on how many lines there are.

Edit: Actually, the answer you got on SO is pretty much exactly what I suggested in my previous posts - except that it wouldn't work because it calls readLine twice in a row, and ignores every other row.
 
Jared Snyder
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ulf Dittmer wrote:Actually, the answer you got on SO is pretty much exactly what I suggested in my first post. In which way do you think it's different?


Are you referring to the initial answer? That didn't work. The Alert Dialog only showed "nullnull".

Let me look at your recommendation again, Ulf. I will update once I make heads or tails.
 
Jared Snyder
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm intrigued by why I am only getting the final line in the Alert Dialog with the code below. Well, for one, I imagine I did it wrong. However, does the txt file get read from bottom to top?

 
Jared Snyder
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Fixed it.




Why is the first word in the Alert Dialog, "null"?

Also, why am I getting a box character (like this: []) every so often?
 
Ulf Dittmer
Rancher
Posts: 42969
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why is the first word in the Alert Dialog, "null"?

Because you're accumulating to a "null" string, and that is the string representation of a null string. Initialize it to an empty string instead of a null string.

Also, why am I getting a box character (like this: []) every so often?

Not sure, do you maybe have some special (possibly invisible) characters in that file? Also, what encoding is the file in? You're not specifying an encoding when reading from the file, so Android's default encoding would be used, which is UTF-8.
 
Jared Snyder
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Ulf Dittmer wrote:Initialize it to an empty string instead of a null string.


If I don't use "null" on the string, I get, "The local variable eulaContent may not have been initialized".

Ulf Dittmer wrote:...do you maybe have some special (possibly invisible) characters in that file? Also, what encoding is the file in? You're not specifying an encoding when reading from the file, so Android's default encoding would be used, which is UTF-8.


I am not sure about the file, I created it using TextEdit in OS X Mavericks. Initially, that creates an rtf, and then you have to click Format > Make Plain Text. It's got a txt file extension, and that's all I know about it.
 
Ulf Dittmer
Rancher
Posts: 42969
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If I don't use "null" on the string, I get, "The local variable eulaContent may not have been initialized".


You probably used "String eulaContent;" - that is exactly the same as "String eulaContent = null;", because null is the default initial value for objects if you don't provide an initial value. Initializing with an empty string would be String eulaContent = "";.
 
Jared Snyder
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ulf Dittmer wrote:Initializing with an empty string would be String eulaContent = "";.


Touche, Ulf. Touche.

I think the [] is a BOM character, especially if it is UTF-8 encoding. What say you?
 
Ulf Dittmer
Rancher
Posts: 42969
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think the [] is a BOM character, especially if it is UTF-8 encoding. What say you?

Quite possibly. I just created a file in the way you said and it did indeed contain a BOM. But in that case there should be exactly one such mark at the beginning of the file - your Android code can easily remove it if it is present.
 
Jared Snyder
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ulf Dittmer wrote:...there should be exactly one such mark at the beginning of the file - your Android code can easily remove it if it is present.


I've gone through and counted. There are 15 in total. Oddly, there is not one preceding the text. By and large, it seems as if there is one at the end of each sub-headhing.


For example, the beginning of the EULA should appear something like this, and is printed as such in the text file:

...you agree to the following terms and conditions (the "Terms and Conditions").

1. USE OF APPLICATION

a. The Developer grants you the exclusive...



In the alert dialog it appears like this:

...you agree to the following terms and conditions (the "Terms and
Conditions"). 1. USE OF
APPLICATION[]a. The Developer grants you the exclusive...
 
Ulf Dittmer
Rancher
Posts: 42969
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maybe prepare the text using a text editor that doesn't force you through hoops. Even using a word processor and saving it as text may work.
 
Jared Snyder
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oddly enough, as you were posting your message, I was in Microsoft Word re-creating the text file. When you save as plain text in Word, it allows you set encoding options. So I was able to specify UTF-8 and also selected "Insert line breaks".

Voila! No more BOM.

Am I able to use \n for line breaks? Those inserted by Word don't appear to do the trick.
 
Paul Clapham
Sheriff
Posts: 21542
33
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It doesn't matter what kind of line breaks you're using in the EULA, because your code is throwing them away. (That's what BufferedReader's readLine() method does.) But yeah, you could add back a '\n' character after each line while you're building the EULA string in that code. I would expect that to work.

I know you've invested a lot in this code, but I might have taken an approach which just read all of the data from the EULA and copied it to a String. That would look a bit different from what you have there. However having code which works is often preferable to rewriting it for some fairly petty reason, and if I inherited that code I might gripe a bit about throwing away the line endings and then adding them back but I wouldn't rewrite it.
 
Jared Snyder
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So I know that step one is to add \n to the end of each line, and in the spaces between lines in my text file. Then, there is going to have to be something in my code that tells BufferedReader what to do when it reads said character (\n). What does such code look like?
 
Paul Clapham
Sheriff
Posts: 21542
33
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, that's not quite it. BufferedReader is going to discard the line-endings and there's nothing you can do about that. You just have to put them back into your EULA string. So instead of (pseudocode):



you need


 
Jared Snyder
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bingo! Bango! Bongo! That worked perfectly, and it was a much easier process than I had assumed. Thanks, Paul!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic