JTextArea Storing Each Line - java

I'm making an Address Book application, and I have the basic fields (name, age, phone numbers, etc.) and I have a JTextArea for placing miscellaneous comments, since I won't know how much info I need to add. It could be one line or hundreds.
I want to know if there is a way to store this data and place it back into the field when the contacts information is loaded. I could store the entire JTextArea as a String, but would that keep the correct formatting? Or save many Strings into an ArrayList or something similar.
Additionally, is there a method to save blocks of text as opposed to single lines? For example. I might want to store an URL, or I might want to store a 5-line poem the contact wrote. Obviously, storing the URL could take one line, but the poem would take 5 lines and therefore 5 Strings.
What seems like a decant storage solution? If my concept doesn't make sense, please comment and I'll try to make it more clear.

Saving it as a single String should fine, it will, normally, preserve newlines as well.
This will of course, depend on where you are storing it

It sounds to me like your address book app is asking a user to input simple values as you suggested and I'm assuming it is saving these values into a "Contact" object or something similar.
If this is the case, you can simply add a String comments; to the Contacts class and implement a getter and setter method accordingly.
Just get the data from the JTextField and store it into that Contact's comments variable.
Contact person = new Contact();
person.setComments(textArea.getText());
Something like that. Then you can reference the person.getComments() later on and print that back out to the TextField if thats what you want to do with it.

Related

how to manipulate linkedlist and also accepts user input of different data type?

it is a java code.i have to created a link list but i am having issues in manipulating it. in terms of how to accept any user input in terms of data type be it integer or string. and also how to save several information in one node. and also how to display whats saved in the linked list.
sorry for my English.

How/where can I "stash"/keep data in my application?

I display dates to the user in a layout that are let's say are textual.
When the user presses a button I want to get the information in those fields that represent these "dates" but if I get the text in them is not of value to me.
I would need to store somewhere the original dates that created these "textual" elements and fetch them from there.
Is there a specific construct in android that one can use as a stash area or should I just use a static class with variable to hold them?
In your case, you should use SharedPreferences to store the data by converting it into a String (text) or int/long first.
This will allow you to easily write and retrieve data, and you should use this.
You can also use the file system to save almost any Java object using serializable, on Internal Storage.
Either way, the data will stay there even if your app is closed or the device is turned off.

Text file to string matrix java

I am making an auto chat client like Cleverbot for school. I have everything working, but I need a way to make a knowledge base of responses. I was going to make a matrix with all the responses that I need the bot to say, but I think it would be hard to edit the code every time I want to add a responses to the bot. This is the code that I have for the knowledge base matrix:
`String[][] Database={
{"hi","hello","howdy","hey"},//possible user input
{"hi","hello","hey"},//response
{"how are you", "how r u", "how r you", "how are u"},
{"good","doing well"}`
How would I make a matrix like this from a text file? Is there a better way than reading from a text file to deal with this?
You could...
Use a properties file
The properties file is something that can easily be read into (and stored from, but you're not interested in that) Java. The class java.util.Properties can make that easier, but it's fairly simple to load it and then you access it like a Map.
hello.input=hi,hello,howdy,hey
hello.output=hi,hello,hey
Note the matching formats there. This has its own set of problems and challenges to work with, but it lets you easily pull things in to and out of properties files.
Store it in JSON
Lots of things use JSON for a serialization format. And thus, there are lots of libraries that you can use to read and store from it. It would again make some things easier and have its own set of challenges.
{
"greeting":{
"input":["hi","hello","howdy","hey"],
"output":["hi","hello","hey"]
}
}
Something like that. And then again, you read this and store it into your data structures. You could store JSON in a number of places such as document databases (like couch) which would make for easy updates, changes, and access... given you're running that database.
Which brings us to...
Embedded databases
There are lots of databases that you can stick right in your application and access it like a database. Nice queries, proper relationships between objects. There are lots of advantages to using a database when you actually want a database rather than hobbling strings together and doing all the work yourself.
Custom serialization
You could create a class (instead of a 2d array) and then store the data in a class (in which it might be a 2d array, but that's an implementation detail). At this point, you could implement Serializable and write the writeObject and readObject methods and store the data somehow in a file which you could then read back into the object directly. If you have the administration ability of adding new things as part of this application (or another that uses the same class) you could forgo the human readable aspect of it and use the admin tool (that you write) to update the object.
Lots of others
This is just the tip of the iceberg. There are lots of ways to go about this.
P.S.
Please change the name of the variable from Database to something in lower case that better describes it such as input2output or the like. Upper case names are typically reserved for class names (unless its all upper case, in which case it's a final static field)
A common solution would be to dump the data in to a properties file, and then load it with the standard Properties.load(...) method.
Once you have your data like that, you can then access the data by a map-like interface.
You could find different ways of storing the data in the file like:
userinput=hi,hello,howdy,hey
response=hi,hello,hey
...
Then, when you read the file, you can split the values on the comma:
String[] expectHello = properties.getProperty("userinput").split(",");

Correct way to represent repeated data in stream

Any advice on how to support repeated messages? Specifically, if these message are all one type. In JSON, these would essentially be an array. In my case, I do not care about indexing however, but that is not saying that an array type would not be useful for protobuf. I have considered the below approaches, but I don' like the tradeoff's. It ins't clear from reading the Google documentation which approach is meant to be used for collections.
Use any existing message and just have a bunch of empty fields
You can use an existing type and just only include the desired collection of repeated messages. So if a user message type has repeated photo message type, send an empty user with nothing but the photo collection field.
Create a wrapper type
This is what #1 does but instead of using an existing type, create a new one. This is a little cleaner because it is explicit and doesn't use empty fields. Still has message typing too. In photo case, this would be an ArrayOfPhotos message w/ only repeated photo message field.
Use delimited stream
Not too sure about this method as I haven't tried it, but protobuf supports delimiting streams. This seems cool, but I would imagine it has downside of less strong typing. Streams could contain a grab bag of different message types.
Does seem beneficial though that this option requires no extra Message types.
In photo case, this would be delimited photo messages, but again, seems like you could throw user messages in as well.
It sounds like you're trying to ask what to do when your top-level data is an array rather than a record. (It isn't totally clear from your question whether you're asking about top-level, but otherwise I don't understand the problem.)
The questions to ask yourself are:
Is there any chance that some day you'll want to add some auxiliary data to this message which is not attached to any one of the objects? For instance, maybe your list of photos will some day have an album name attached. In this case, you certainly want to use your solution #2, since it gives you the flexibility to add other fields later without messing up some existing message type.
Will it be a problem for the client or the server to have to hold the entire data set in memory and parse/serialize it all at once? This is a requirement for a single message. For example, if you're sending 1GB of photos, you probably want each end to be able to handle one or just a few photos at a time. In this case you certainly want solution #3.
I would not advise using solution #1 in any case.

How to work with and how to parse a text file with mixed content in Java

I need some advice on how to go about processing a text file in Java. I have a file, where I have some lines on top with some data, and then a table. For example, in the beginning of the file I have totals like:
Cars purchased = 1890
Cars returned = 130
Then there is a table, which contains car ids:
id#1 =127974
id#2 =212445
And then another table:
table begin:
Customer ID | Price paid | Car brand#1 | Car brand#2 | Car brand#4
id#1
id#2
I have to
print out the cars purchased value, cars returned value, then an array with car ids and
create a tabular set based on the last table.
Can anyone please explain to me the logic on how to go about this in java? I am not asking for a code, but for some guidelines/steps/pseudocode. I can't understand how can I separate this text file into 3 chunks and have the input reader concentrate only on one of the three at a time. For example, the car ids can be similar to client ids in the table, so what can one do to not let the input reader read unnecessary information?
Another thing - if I read the file, where parts of it are tab-separated and other parts are not, how do I figure out where to begin reading the tab-separated part only?
If the beginning of the file has Cars purchased = 1890 and i have to return 1890 only, do i have to scan through the entire file? Do I search for the words "Cars purchased" and then somehow access the value?
Then, when I have to put all of this back together in a tabular set, how do I know that each entry will match its relevant entry from the original file?
Also, this is not for working with a single file, but with whatever file of the same type provided, assuming that the key names like Car id, Cars purchased, Cars returned are the same.
I really need help with understanding how one would process a file like this.
"I can't understand how can I separate this text file into 3 chunks and have the input reader concentrate only on one of the three at a time."
Because you don't know where your data starts and stops, you need to read the text file sequentially, and only start paying attention to your data when you get to the section you want. In pseudocode:
while (you haven't gotten to the text that marks the start of a section) {
read a line;
throw the line away;
}
while (you haven't reached the text that marks the end of a section) {
read a line;
do something with the line;
}
You'll notice that after we're done with our section, we don't keep reading -- there's no need, unless there's something you want further on down.
Should you read in the whole thing and store it, or read it multiple times as you deal with each section? That's a design decision, based on lots of factors -- speed of reading, how much memory you have vs how much the data will take up, etc.
How do you know each entry will match the tabular data? That depends on how you store it. Read up a bit on objects and data structures for some ideas. For beginners, arrays are often easy to handle.
From what it looks like: you are trying to parse data from a text document to a table of data. I recommend try using an XML format instead and use either the Java native XML parser (which sucks imo), or a third party one to get the information. By using XML tags you can easily search for the car by ID and get attributes such as price brand etcetc...
It will be much easier to edit and view info for your cars... Here is an example:
<cars_purchased='10485'>
<cars_returned='1945'>
<cars>
<id = '1000'>
<brand>ASDF</brand>
<car_model>SOMECARNAME</car_model>
<price>$10000</price_paid>
.......... And so on...
</id>
... More cars here....
</cars>
<client>
<clientID ='1000'>
<client_paid>18424</client_paid>
Whatever info u wanted on the client can go in tags here...
</clientID>
Any other clients....
</client>
From there using Java it is really simple to parse data from this xml, just go down by ID and you can get whatever info you want from the car to save to local variables...
So in order to find for example cars purchased, just look for that tag and it will return back with the value, you won't need to search through the entire file. Same thing would apply for anything else. Looking for a car? Just look under the cars chunk for the car_id... Looking for clients only? Look only at the clients chunk and search through for the correct ID...
For more on XML files look here http://en.m.wikipedia.org/wiki/XML...
Here to learn about how to use Javas native XML managing ... http://docs.oracle.com/javase/tutorial/jaxp/intro/index.html
Sorry for not using proper hyperlinking I'm on my phone and don't know how to do it manually...
Hopefully after reading the tutorials you can design a parser for yourself and edit/read the XML file with ease...

Categories