• Post Reply Bookmark Topic Watch Topic
  • New Topic

importing files  RSS feed

 
nathan gibson
Ranch Hand
Posts: 120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
im trying to make a program that will calculate the probability of you having two sons, two daughters, or a boy and a girl. the program is supposed to import a file that has random things like bb, gg, and bg, which it uses to calculate the probability. this is an assignment for my school that i dont quite understand. so far i have
i dont know where to really go after this. i do believe we are supposed to use loops also. any help is much appreciated.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
this looks like you are jumping in and writing code before you have thought out what you want to do. That is almost ALWAYS a sure fire recipe for disaster.

My suggestion would be to throw all this away. Then, write down what your program should do - in English (or whatever your natural language of choice is). Think very broadly. This is what's called pseudo-code. Something like

open the file
read each line
add one to the appropriate counter (BB, BG, GB, or GG)
repeat until end of file
compute and print percentages


Once you are done with this, see if you can write the code for each step. if not, refine it. the last one might need to be broken down into something like

get the total number of lines read
compute the percentage for BB
compute the percentage for BG
...etc


When you do start writing code, write as little as possible before you compile, debug, test and fix. EVERY time i write a java program, I do my first compile after this much code:



I then add one or two lines, and re-compile. So after this, I might write code to open the file, and close it. Then i'd re-compile and test.

Then i'd try printing out each line of the file (which requires me to read each line), and re-compile/test.

Note that at each step, I put in just enough additional code to verify what i've done works. I usually then comment it out (so I can easily restore it if something breaks) until i'm satisfied it ALL works, and clean it up at the end - possibly even saving a version with all the extra as a draft in case I need to go back and re-work part of it.
 
nathan gibson
Ranch Hand
Posts: 120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
my biggest problem is that im not really familiar with all the new code that i need to do the things like open the file and how to deal with the file after it is imported. we just started this type of assignment today and im not at all familiar with it, and i dont know where to find good examples that will help me to figure out exactly how the code works.
 
Campbell Ritchie
Marshal
Posts: 56525
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As Fred has already told you, do it in tiny stages.
Suggest
  • Get something you can run from the command line where you write BB GG GB BG and add the frequencies.
  • Get something where you read text from a file and print it to System.out.
  • Try putting the whole lot together.

  • There are several ways you can read from a text file.
  • Since Java5 the Scanner class has probably been the most popular
  • You can use the older method with BufferedReaders and FileReaders if you prefer.
  • Other links: Scanner. Java™ Tutorials about I/O. If you use a BufferedReader, make sure to search the Ranch for the use of "finally" with a BufferedReader.
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!