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 2 of 2

Thread: Assignment

  1. #1
    Junior Member
    Join Date
    Oct 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Assignment

    1. Write a program which creates a file with all the numbers from 1 to 100.
    2. Read this file. Separate the odd numbers from even numbers and write the odd numbers to a new file called odd.dat, even numbers to even.dat.
    3. Read the odd.dat file, total all the numbers which are divisible by 3, find the average of these numbers and append to end of this file.
    So far I manage to create a file and read it from it but I cannot separate it properly
     
    package javaapplication34;
     
     
    import java.util.Scanner;
    import java.io.*;
     
    public class JavaApplication34 {
     
     
        public static void main(String[] args)throws IOException {
            int number=0;
          Scanner keyboard = new Scanner(System.in);
          PrintWriter nm=new PrintWriter("number.txt");
           PrintWriter odd=new PrintWriter("odd.txt");
            PrintWriter even=new PrintWriter("even.txt");
     
          do {
     
           nm.println(number);
           number++;
          }while(number<101);
          nm.close();
         // Get the filename.
          System.out.print("Enter the filename: ");
          String filename = keyboard.nextLine();
     
          // Open the file.
          File file = new File(filename);
          Scanner inputFile = new Scanner(file);
     
          // Read lines from the file until no more are left.
          while (inputFile.hasNext())
          {
             // Read the next name.
             String friendName = inputFile.nextLine();
              if (number%2==0){
                  even.println(number);
              }
              else{
                  odd.println(number);
              }
             // Display the last name read.
             System.out.println(friendName);
     
          }
     
     
          // Close the file.
          even.close();
          odd.close();
          inputFile.close();
     
     
            // TODO code application logic here
        }
    }


  2. #2
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Assignment

    Assignment

    Duplicate post
    Improving the world one idiot at a time!

Similar Threads

  1. Help with Assignment
    By docguy89 in forum What's Wrong With My Code?
    Replies: 12
    Last Post: March 15th, 2013, 08:30 AM
  2. New assignment
    By clarky2006 in forum Java Theory & Questions
    Replies: 4
    Last Post: February 11th, 2013, 06:20 PM
  3. Help with my assignment.
    By javapol in forum Java Theory & Questions
    Replies: 1
    Last Post: February 8th, 2013, 08:20 AM
  4. assignment troubles polymorphism (guide for assignment included)
    By tdawg422 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 8th, 2011, 10:01 AM
  5. Replies: 1
    Last Post: February 22nd, 2010, 08:20 AM