• Post Reply Bookmark Topic Watch Topic
  • New Topic

check file format before processing  RSS feed

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi there,

I am trying to ensure that the file I read contains the following format:

text:text
text:text

I don't want to read a file if it doesn't have this format

I currently check each line for the ':' character but this isn't exactly right, please help as I'm not sure how to check the file without reading it

 
Rancher
Posts: 1815
15
Android Eclipse IDE Java Java ME
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What exactly are you trying to do? Reading the contents of the file and check for :text or you only wish to read the .txt files? If later is the case you can have a look on

http://download.oracle.com/javase/1.4.2/docs/api/javax/swing/filechooser/FileFilter.html

 
Sheriff
Posts: 22846
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
nida lulu wrote:please help as I'm not sure how to check the file without reading it

I don't know if you meant to say this, but it should be obvious that you can't tell what is in the file unless you read the file. Probably you meant to say something else but got confused. Could you clarify what you really want to do?
 
nida lulu
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry, what I mean to say is,
I want to process "stringOne then stringtwo" and they are in the form stringOne:stringTwo
the two strings are separated by the ':' but if a file does not have this format then I will choose to skip it as it is not what I am looking for.
 
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK, well, the small piece of code you show where you check each line of the file sounds like the only way to do things; it's just a question of finding the proper check. What precisely is wrong with the 'indexOf(":")' that you're using now? In other words, how can we improve your solution?
 
nida lulu
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well I was just wondering what would happen if I had a file like the following:

Scenario One:
StringOne::...String Two.....StringThree

Scenario Two:
StringOne : StringTwo

If I was using indexOf(':') would this still be true for both scenarios? even though for my program I only care for scenario two
 
Paul Clapham
Sheriff
Posts: 22846
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That depends on your requirements. You haven't said what you want to do in those two scenarios. Before you start programming, you need to know what your program is supposed to do. So far you don't seem to know, or at least you haven't communicated that to us.

Quite often these requirements are given to you by other people. And quite often they are incomplete or vague, so that you can't tell what your program is supposed to do. In that case you have to go to those other people and ask them to clarify. However it isn't clear whether that is the case here.

By the way that's a good question you ask: "I was wondering what would happen if...". That's just the sort of question a programmer should ask. Only it should be followed up by "Okay, what is supposed to happen if... anyway?"
 
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
does your scenario 1 not work because it has two colons? if so, you can perhaps instead do a count on the number of colons on the line.

does it not work because you have a string2 and a string3? How can someone tell where string2 ends and string3 begins?

The trick here (and actually, always) is to define on paper what is and what isn't a valid example. And that means to spell it out in words in great detail. Simply providing an example or two is NOT a definition. It may help a team understand some of the scenarios and help spark the discussion of the definition, but MUCH more detail is needed.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!