• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Java Programming File Format Converter?

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello. I was hoping to get some help with a java program I have to write. Basically I'm given an input file that has a bunch of questions and I have to change the format of the text file.

For example:
Type: TF
1. Are monkeys animals?
a. True
*b. False

Type: MC
2. What's my favorite color?
a. blue
b. red
*c. orange


So I would have to take those two questions (that were in the input text file) and I would have to change them to this:

TF Are monkey's animals? True
MC What's my favorite color? blue incorrect red incorrect orange correct

I was just wondering if someone could help me on how to do this.

Could someone help me with the pseudo code for this please?
 
Marshal
Posts: 80616
468
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch

Please show us how far you have got so far.
 
Joespeh Faqaim
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm not entirely sure but here's how I believe it should be.

- I need to get the file scanned in and then I need to scan lines for certain letters. The best way to do this is to search for MC and TF. So if the file is being scanned and it finds MC then it will first place MC at the front of the line.
- So then I would search for a number, for example 1. or 2. and then place the question that follows right after it. (Not entirely sure how to do that)
- For the answer part I would scan for the asterisk(*) and then place the line that goes after it.
 
Ranch Hand
Posts: 137
5
Eclipse IDE Postgres Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Joespeh,
Welcome to Ranch!
We would like to help you. I saw this same question when you posted it SO( ) and even there i asked you to explain what exactly you want to do. By the way, now i see some improvement in your question here.
As Campbell said, we need some of your work to see, in order to understand your standard. If you show us some code, then we can help you and improve it. First you must tryout and show us. That doesn't mean that we asking you to solve your own problem. But we need some kick-start from you. Good luck!
 
Joespeh Faqaim
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Jude Niroshan
Ranch Hand
Posts: 137
5
Eclipse IDE Postgres Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok. You are almost done. Now you can take a whole line in your text file into the variable 'line'. Have another separate String variable(Better if you use StringBuilder) to construct your final output.(so called formatted way).

inside your while loop, that means for each line you read in that text file, you can check whether the current line is the question or the answer. Regular Expressions will save you in this situation. All you have to do is, just to check

line(String 'line' variable) starts with a number> Question
line(String 'line' variable) starts with a asterix> Answer

and concatenate it to the new String variable (or append to StringBuilder) that i asked you to create.
 
Marshal
Posts: 5950
407
IntelliJ IDE Python TypeScript Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Joespeh, I added code tags to your post (see -> UseCodeTags) to make your code easier to read. Looks much better now.

Can you tell us a little about the code you have posted. What precisely are you trying to achieve, and what problem are you facing right now?
 
Joespeh Faqaim
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Alright so I'm just not entirely sure where to go after I've scanned the text file. What would I use to search for the number and the letters for the question and answers?
 
Sheriff
Posts: 17734
302
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Joespeh, your example is a little confusing in that it's not consistent. For the True/False question, the * is on b, which is the incorrect answer. On the multiple choice question, however, the * is on the correct choice, option c. So what is the * supposed to indicate, the correct or incorrect answers?
 
Jude Niroshan
Ranch Hand
Posts: 137
5
Eclipse IDE Postgres Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Joespeh Faqaim wrote:
For example:
Type: TF
1. Are monkeys animals?
a. True
*b. False

Type: MC
2. What's my favorite color?
a. blue
b. red
*c. orange

So I would have to take those two questions (that were in the input text file) and I would have to change them to this:

TF Are monkey's animals? True
MC What's my favorite color? blue incorrect red incorrect orange correct



It seems like you wanted to extract the answers along with the questions. If this is not the question, please tell us what exactly you want to do?
 
Joespeh Faqaim
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I apologize, it is supposed to be on the correct answer, sorry I didn't catch that. The asterisk would be in front of a not b.
 
Joespeh Faqaim
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jude Niroshan wrote:

Joespeh Faqaim wrote:
For example:
Type: TF
1. Are monkeys animals?
a. True
*b. False

Type: MC
2. What's my favorite color?
a. blue
b. red
*c. orange

So I would have to take those two questions (that were in the input text file) and I would have to change them to this:

TF Are monkey's animals? True
MC What's my favorite color? blue incorrect red incorrect orange correct



It seems like you wanted to extract the answers along with the questions. If this is not the question, please tell us what exactly you want to do?



Yeah that is what I would like to do.
 
Joespeh Faqaim
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Okay so I think I'm making progress. I'm able to search through the file. But now I'm running into a different problem. So my file has multiple questions that start off with Type: TF but when I run my program as it is, the counter I have only goes to 1. Can you guys tell me why this is? And how I could fix it?



 
Campbell Ritchie
Marshal
Posts: 80616
468
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No. That is your project and you will learn nothing if somebody else does the work.

What is the counter? What does it count?
More to the point, why do you have everything in one method? A method should do one thing; if it does two things it ought to be divided into two methods. You are reading from the file, putting things into a Set, and several other things, all in the one method.
And Java® is an object language. Where do you have any objects? Do you have a question class, or an answer class? Why are you not creating objects from the Strings you are reading; then the objects can do the manipulations and identify the correct answer?
 
Campbell Ritchie
Marshal
Posts: 80616
468
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think your change from BufferedReader to Scanner is an improvement, because you can read part of a line. You can check you Scanner whether it has a next int with one of its methods. If you reach a line starting Type after a blank line, you know you are starting a new question: you can use the line you read to determine which type of question (TF, MC, S) you are dealing with.

You realise there is a problem with ths sort of file?. You are very dependent on all the lines being in the correct format. You need to beware of this sort of question

Type: S
What did people use to write letters before word processors were invented?
Typewriters



Why are you using a Set to store your Strings? You know you should get out of using Strings as soon as possible. The sooner you stop handling Strings and start handling other sorts of object, the better.
 
Campbell Ritchie
Marshal
Posts: 80616
468
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To repeat what I said earlier: why are you using a Set?
 
Joespeh Faqaim
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Alright, I have a different question now. I decided to use if statements to do this.



So that code above works and prints out what I want. But I want to add the line afterwards as well. I want to use an if statement again so I put it like this:



Unfortunately when I do this, the code doesn't do anything and ignores the first part as well and doesn't print anything out. What am I doing wrong? A general hint, not asking to do this for me.
 
Bartender
Posts: 3648
16
Android Mac OS X Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Joespeh, just curious how much OO concepts do you know? I asked this because parsing the lines like you have right now may work but down the road, may become hard to enhance/maintain.

Since you are working with question/answer pairs, I would suggest using classes Question and Answer.

The characteristics of a question becomes:
question type = TF or MC or S or whatever down the road
question text
list of answers


The characteristics of an answer becomes:
answer text
correct flag


Hope you still follow me up to here.

How to link the 2 together? The hint in mentioned above. I will let you ponder what collection to use.
 
Rancher
Posts: 4801
50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


You have read the nextLine into trueFalse, but then check whether line startsWith 3.
 
Campbell Ritchie
Marshal
Posts: 80616
468
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This really looks as if you are guessing. You can guess 1000000 times (as long as all the guesses are different ‍) and one of the guesses will probably be correct, or you can think about the problem and get it right first time.
I think you need to turn your computer off and write down on paper what you intend to do.
I also think you need the object oriented approach of creating question and answer classes.
 
reply
    Bookmark Topic Watch Topic
  • New Topic