[Boards: 3 / a / aco / adv / an / asp / b / bant / biz / c / can / cgl / ck / cm / co / cock / d / diy / e / fa / fap / fit / fitlit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mlpol / mo / mtv / mu / n / news / o / out / outsoc / p / po / pol / qa / qst / r / r9k / s / s4s / sci / soc / sp / spa / t / tg / toy / trash / trv / tv / u / v / vg / vint / vip / vp / vr / w / wg / wsg / wsr / x / y ] [Search | Free Show | Home]

CSV in Java . . . what?

This is a blue board which means that it's for everybody (Safe For Work content only). If you see any adult content, please report it.

Thread replies: 15
Thread images: 2

File: meow.jpg (23KB, 424x481px) Image search: [Google]
meow.jpg
23KB, 424x481px
I know you guys aren't a personal tech support forum or anything, but I tried posting this in /wsr/ a few days ago and nobody had any idea what it was -- so I was hoping I could at least open the floor for discussion on it here.

Basically I'm trying to take input from the user and save it into a .txt file. Each item must be separated by a comma. The problem is that, since I'm allowing the user to input whatever they want, I can't rely on the old "just wrap your commas in quotes" alternative.

These two things alone basically invalidate most of everything I found online on how to address this unique issue. So I wound up doing some more digging and read about OpenCSV. The thing is that I'm not actually seeing any examples or instances of whether or not it actually even solves my problem, or if it's just going to lead to a different set of problems that I cannot solve.

Prior to this I've been writing files with FileWriter and BufferedWriter. I look on OpenCSV's website / Stack / tutorials like and I see solutions like this :

    FileOutputStream fos = new FileOutputStream("awesomefile.csv"); 
OutputStreamWriter osw = new OutputStreamWriter(fos, "UTF-8");
CSVWriter writer = new CSVWriter(osw);
...
String[] row = {
"123",
"John",
"Smith",
"39",
"I said \"Hey, I am 5'10\".\""
};
writer.writeNext(row);
...
writer.close();
osw.close();
os.close();
.

Once again, this doesn't answer my question because this is an example of the programmer just writing his/her (probably his) own input.

tl/dr - I can't figure out how to make it so a user can write commas and/or quotes and/or double quotes in their input. I think I'm supposed to use OpenCSV, but I've never used it before and I was hoping to be able to finish this today so I can show it to my boss tomorrow.

Hoping someone can give me a hand. :/
>>
poo in the loo
>>
>>56436962

I'm from the United States.
>>
>>56436962

Save that stupid shit for /b/. If you don't know the answer, just say so. At least OP isn't begging us to spoonfeed him information.

Yes OP, OpenCSV will resolve this problem, but I have no idea how or why it works.
>>
>>56436947
how many columns is going to have a row?

or its just a line per user input?
>>
>>56437412

It's just a line. The example I showed was someone answering a related question on Stack Overflow.

I really don't want this to turn into an all day affair. :( Right now I have the OpenCSV JAR downloaded and the dependency written into my pom.xml but I'm unsure of where to go from here - or if it's even in the right classpath.
>>
File: rotsnake.gif (303KB, 1024x768px) Image search: [Google]
rotsnake.gif
303KB, 1024x768px
import com.opencsv.*;

void main(String... a){
CSVWriter writer = new CSVWriter(new FileWriter("yofile.txt"));
List<String> line = new ArrayList();

do{
System.out.print("input>");
line.add(System.console().readLine());
}while(line.size() < 3); //user inputs per line

writer.writeNext(line.toArray(new String[line.size()]));
writer.close();
}
>>
>>56436947
Escape sequence.
By default, you add " to any user string. If the string already contains ", replace it with \". If it contains \, replace with \\.

Problem solved.

Next time go read what serialisation, XML, JSON is and how it all is already built in Java so code monkeys can actually implements what is needed instead of reinventing wheel.

Also database. Try sqlite for starters, because storing table datas in files is mostlikely retarded. Should also be build in Java.
>>
>>56438519

So what happens when the user inputs "\\" ?
>>
>>56439815
It becomes \\\\
>>
>>56439855

How does this decode then?

Sorry if these are stupid questions but I'm kind of lost here.

Normally I would just not allow users to input the token as a value, but bossman says we can't do that anymore. He wants it so that the user can input whatever they want without breaking it.

I don't understand :

>By default, you add " to any user string. If the string already contains ", replace it with \". If it contains \, replace with \\.

Are you talking about using an enhanced for loop to check the output?
>>
>>56439964
you put " " around any input so it's a string, if there are " already in the input you put \" so it uses it as a normal character and not the end of string, same for normal \, you put \\ so it uses \ as a normal character
>>
>>56440216

Maybe this is asking too much, but can I see an example? I'm still not understanding how a user can't just break this by inputting the right characters AND allowing the program to consistently decode without changing data.

I have this right now...

   
String printInput;
if (userInput.contains(",")) {
userInput = userInput.replaceAll(",", "~!~");
}


printInput = userInput;

return printInput;

}
.

In the decode method, it finds all incidents of ~!~ and turns them into commas.

The problem is if you write ~!~, it pretty much just turns it into a comma.

You're talking about implementing some kind of loop and I have no idea what that would even look like or how the decode would work with it.
>>
>>56440341
basically:
String doubleQuote = Character.toString((char) 34);
String backslash = Character.toString((char) 92);

userInput = userInput.replaceAll(backslash, backslash + backslash);

userInput = userInput.replaceAll(doubleQuote, backslash + doubleQuote);


Basically the code above takes the ascii values for double quote and backslash, and casts them to chars, and then converts them to strings.

It replaces all instances of quote chars with a backslash followed by a quote, and all instances of backslashes with two backslashes in a row. (Hopefully the String.replaceAll method can do that without an infinite loop.)

When you're reading back from the file, when you encounter a backslash, just don't even check whether the next character is a quote or a backslash, just read it in verbatim.
>>
>>56436947
A professional java programmer will find a well established library for this and import the jar into the classpath. Almost anything you need, if it isn't already in the native release can be found in an apache commons library.

https://commons.apache.org/proper/commons-lang/javadocs/api-2.6/org/apache/commons/lang/StringEscapeUtils.html

Now the C and CPP fags will sperg off, lol.
Thread posts: 15
Thread images: 2


[Boards: 3 / a / aco / adv / an / asp / b / bant / biz / c / can / cgl / ck / cm / co / cock / d / diy / e / fa / fap / fit / fitlit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mlpol / mo / mtv / mu / n / news / o / out / outsoc / p / po / pol / qa / qst / r / r9k / s / s4s / sci / soc / sp / spa / t / tg / toy / trash / trv / tv / u / v / vg / vint / vip / vp / vr / w / wg / wsg / wsr / x / y] [Search | Top | Home]

I'm aware that Imgur.com will stop allowing adult images since 15th of May. I'm taking actions to backup as much data as possible.
Read more on this topic here - https://archived.moe/talk/thread/1694/


If you need a post removed click on it's [Report] button and follow the instruction.
DMCA Content Takedown via dmca.com
All images are hosted on imgur.com.
If you like this website please support us by donating with Bitcoins at 16mKtbZiwW52BLkibtCr8jUg2KVUMTxVQ5
All trademarks and copyrights on this page are owned by their respective parties.
Images uploaded are the responsibility of the Poster. Comments are owned by the Poster.
This is a 4chan archive - all of the content originated from that site.
This means that RandomArchive shows their content, archived.
If you need information for a Poster - contact them.