Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

JTextField reading and writing problem  RSS feed

 
michael perkins
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello everyone. I have a problem I've been trying to work out for awhile. I have a JTextField that is connected with buttons that can open new text files or what have you. When I open a specific type of text file I am trying to take the data in that file, reformat it, and print it out in another file. All of my buttons work fine and I can open the file fine, I read in the data one line at a time using

String[] data = edPane.getText().split("\\n");

which I suspect works correctly. The jest of my problem is that you should never see the file you open, it is already in a specific format, but the only text that appears would be in the format which I have specified. The only part of this I cannot seem to get, and its probably quite easy but I'm just overlooking, is how would I get text I reformatted to appear using the data from the file I open? Sorry if that's not very clear I've never been very good at explaining, thanks in advance.
 
Greg Brannon
Bartender
Posts: 563
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, it's not very clear. The statement, "The jest of my problem is that you should never see the file you open, it is already in a specific format, but the only text that appears would be in the format which I have specified. The only part of this I cannot seem to get, and its probably quite easy but I'm just overlooking, is how would I get text I reformatted to appear using the data from the file I open?" is very confusing.

"you should never see the file you open" - 'see' it where? what (file?) does the user see?

"it (the file?) is already in a specific format" - what format? why is it important?

"the only text that appears (where?) would be in the format which I have specified" - at this point, I'm completely lost.

Some code would be helpful, preferably narrowed to the lines that aren't working. Example outputs, incorrect and as desired, would also help.

Something like,

This is the code:

This is what it does (including any error message):

This is what I want it to do:
 
michael perkins
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would post code but there are several pages of it, and all of my code works, my problem is taking theory into code. But I will explain more if I can.

When I run the program, I have a MenuBar with item in it. One of these is "open." This uses a JFileChooser and ShowOpenDialog to allow me to choose the data file I want to use. In this data file is a list of numbers and names, on item per line. When I open this I should never see the original file, but make a new file in a format of a name on a line followed by numbers until the next name, basically. So what I am having problems going from theory to code, after I choose the data file and perform the operations on it, how would I put this output and opening it in a new file. And sorry for the bad explanation at first, maybe this one is better, just had a real hurried day. Thanks again.
 
Rob Spoor
Sheriff
Posts: 21087
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
michael perkins wrote:I read in the data one line at a time using

String[] data = edPane.getText().split("\\n");

which I suspect works correctly.

Don't suspect, be certain. In this case I think you're wrong. You're not splitting on line breaks ("\n") but on a backslash followed by an "n". "\n" works just fine in regexes.
 
michael perkins
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Spoor wrote:
michael perkins wrote:I read in the data one line at a time using

String[] data = edPane.getText().split("\\n");

which I suspect works correctly.

Don't suspect, be certain. In this case I think you're wrong. You're not splitting on line breaks ("\n") but on a backslash followed by an "n". "\n" works just fine in regexes.


I usually am certain but I've had a hard time testing it because it wasn't working to a point where I could. My teacher is beyond terrible and I have a hard time trying to teach myself java while taking several other classes. I am usually a pretty good coder, not the best in java I will admit, but I have found my problem, which was I was calling my 'openFile' method at the wrong time which was a terrible mistake on my part and I had not even suspected that I would make that mistake. Anyway I'm glad I found this place, it seems to be very useful and I will be using it in the future, thanks again.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!