Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Loop over text file andd do some maths

  1. #1
    Junior Member
    Join Date
    Jul 2014
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Loop over text file andd do some maths

    Hi guys,

    I'm not new to java by any means, but in 3 years of being familiar with it, I've never been able to pick it up. I'm currently trying to write a program that will loop over an input file (containing a set of random numbers), then I want the program to count and add together any numbers that are even and less than 85.

    So the output would look something like 'Numbers under 85: 5. The sum of those numbers is 182.

    Now I'm thinking that I'm looking at a few for loops inside a while loop but I just have no idea how to implement it.

    Any help would be appreciated.

    Thanks,

    John


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Loop over text file andd do some maths

    Welcome to the forum! Please read this topic to learn how to post code in code or highlight tags and other useful info for new members.

    Welcome back. You can master it if you want to.

    Start just as you have, writing out in plain language a design that makes sense to you and then code that. Restating what you've said slightly differently:

    1. Read each line of a file until every line has been read
    1.A. Parse the line read to an integer

    2. If the number from 1.A is even AND less than 85:
    2.A. increment the count of numbers found
    2.B. add the number from 1.A to a total of all numbers found

    3. Report the results:
    3.A. report the count of numbers found
    3.B. report the total of all numbers found

    Part 1 sounds like a while() loop. Part 2 is probably an if() statement or 2 within the while() loop. Part 3 can only be known after the while() loop is done.

    You've pretty much noodled this through already. You could take the above outline of the design of this program, put it into your own words, and comment a source code file to get started.

    Before the above is done, a file must be opened for reading. It might be better to start with an array of integers before even trying to read the file, but that alters the program's design somewhat. Even so, it could be a stepping stone.

    You can find tutorials on each of the parts in the Oracle Tutorials.

    Come back when you have some code and need help, or ask any other questions you need answered to get started.

  3. The Following User Says Thank You to GregBrannon For This Useful Post:

    johndoe (July 25th, 2014)

  4. #3
    Junior Member
    Join Date
    Jul 2014
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Loop over text file andd do some maths

    Hi Greg,

    Thank you for replying. Even at its strongest, my java was never particularly great but I still kind of get the process, my issue is with the actual writing of the code.
    It's things like imports and 'extends' that usually trip me up.

    For example, I haven't the first idea how to parse the line to find the intergers. I would guess that's done by medium of scanner?

    Honestly I don't know where to start with actually writing the code. The most I'm getting to is creating a scanner.

  5. #4
    Senior Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    666
    Thanks
    0
    Thanked 121 Times in 105 Posts

    Default Re: Loop over text file andd do some maths

    Does the file only contain integers and nothing else?
    The problem would become very simple then.

  6. #5
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Loop over text file andd do some maths

    I see a lot of people pass through beginning programming who have an idea of something they want to do, usually writing a game, but they barely get off the ground because they don't know the basics. They fail to launch, get discouraged, and quit, usually repeating the cycle several times.

    Maybe you already know the basics (variables, assignments, program control - looping and branching - arrays, I/O, basic data structures, etc.) in another language and would like to translate that knowledge to Java but don't know how or don't have the patience to study the basics all over again. I don't know, you didn't say.

    Whatever your story, you need to really learn the basics in Java, and you need to do it in an ordered way with lots of practice along the way. Get a book - there are several free e-books available - start at page 1. Read every word, write every example program and do every exercise until they compile and run correctly, and then modify them until you understand absolutely everything there is to know about them and the programming tools and concepts they present. Do it because you're curious, want to learn and understand how it all works, and because you love programming. If that's not your motivation, then ask yourself now why you're here.

    Done correctly, within 4 to 6 months, depending on how hard you work and how much you practice, you'll be confident about imports, know when to extend and when not to, will be able to write a correct main() method in your sleep, will know when to write a method and whether it should be static or not, etc. This knowledge will come to you with study and practice, practice, and more practice.

    There's no magic, no shortcuts, no Vulcan mind meld to squirt the knowledge into your head. There's just hard work that you do eagerly, because you love it and want to know.

    Now find a book and get to work. Come back when you need help with anything.

Similar Threads

  1. Reading a text file + Nested Loop
    By iCurtisIT in forum What's Wrong With My Code?
    Replies: 7
    Last Post: December 1st, 2013, 03:13 PM
  2. Loop used to read different data values from a text file
    By mjrox96 in forum Loops & Control Statements
    Replies: 10
    Last Post: July 10th, 2013, 03:49 PM
  3. Java code Split text file into multiple text file
    By jayraj in forum What's Wrong With My Code?
    Replies: 26
    Last Post: April 19th, 2013, 06:14 AM
  4. Using for loop to read in data from text file using Scanner class
    By Scippi in forum Loops & Control Statements
    Replies: 23
    Last Post: February 26th, 2013, 10:53 PM
  5. java program to copy a text file to onother text file
    By francoc in forum File I/O & Other I/O Streams
    Replies: 3
    Last Post: April 23rd, 2010, 03:10 PM