Results 1 to 7 of 7

Thread: Assignment

  1. #1
    bada_bum is offline Member
    Join Date
    Oct 2013
    Posts
    4
    Rep Power
    0

    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 only create a file with numbers but i cannot separate it to an odd file and even number. Sorry for my English
    Java Code:
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    package javaapplication34;
    
    
    import java.util.Scanner;
    import java.io.*;
    /**
     *
     * @author Egor
     */
    public class JavaApplication34 {
    
        /**
         * @param args the command line arguments
         */
        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("number.txt");
            PrintWriter even=new PrintWriter("number.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.
          inputFile.close();
          even.close();
          odd.close();
         
            // TODO code application logic here
        }
    }

  2. #2
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,060
    Rep Power
    6

    Default Re: Assignment

    Why is the line you're reading from the file called friendName? Copypasta from another assignment?

    You're almost there. You can use Integer (Java Platform SE 6) to convert that String into an int. You've got that number var, but it doesn't seem to be declared anywhere...
    Get in the habit of using standard Java naming conventions!

  3. #3
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default Re: Assignment

    Assignment

    Duplicate post

  4. #4
    bada_bum is offline Member
    Join Date
    Oct 2013
    Posts
    4
    Rep Power
    0

    Default Re: Assignment

    I did manage to complete 2 steps but I am stuck with the third one .It did not print average at all
    Java Code:
    package javaapplication34;
    
    
    import java.util.Scanner;
    import java.io.*;
    
    public class JavaApplication34 {
    
        public static void main(String[] args)throws IOException {
            int number=0;
            int total=0;
            double average=0;
            int counter=0;
            int totalCounter=0;
    
          PrintWriter nm=new PrintWriter("number.txt");
          FileWriter fwriter=new FileWriter("odd.txt",true);
          PrintWriter odd=new PrintWriter(fwriter);
            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("number.txt");
          Scanner inputFile = new Scanner(file);
    
          // Read lines from the file until no more are left.
          while (inputFile.hasNext())
          {
          String numbers = inputFile.nextLine();
        number--;
          if (number%2==0){
                  even.println(number);
                  
              }
              else{
                  odd.println(number+"["+counter+"]");
                  counter++;
                  
              }
    //      average=total/totalCounter;
          System.out.println(numbers);
          }
          
         
          even.close();
          odd.close();
          inputFile.close();
    File myFile=new File("odd.txt");
    Scanner keyboard =new Scanner(myFile);
    while (keyboard.hasNext()){
        String tmp=keyboard.nextLine();
       
        if (number%3==0){
                      totalCounter++;
                     total+=number;
                  }
        System.out.println(tmp);
       
    }
     odd.println(average=total/totalCounter);
    keyboard.close();
    odd.close();
    }
    }

  5. #5
    bada_bum is offline Member
    Join Date
    Oct 2013
    Posts
    4
    Rep Power
    0

    Default Re: Assignment

    I did manage to complete 2 steps but I am stuck with the third one .It did not print average at all
    Java Code:
    package javaapplication34;
    
    
    import java.util.Scanner;
    import java.io.*;
    
    public class JavaApplication34 {
    
        public static void main(String[] args)throws IOException {
            int number=0;
            int total=0;
            double average=0;
            int counter=0;
            int totalCounter=0;
    
          PrintWriter nm=new PrintWriter("number.txt");
          FileWriter fwriter=new FileWriter("odd.txt",true);
          PrintWriter odd=new PrintWriter(fwriter);
            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("number.txt");
          Scanner inputFile = new Scanner(file);
    
          // Read lines from the file until no more are left.
          while (inputFile.hasNext())
          {
          String numbers = inputFile.nextLine();
        number--;
          if (number%2==0){
                  even.println(number);
                  
              }
              else{
                  odd.println(number+"["+counter+"]");
                  counter++;
                  
              }
    //      average=total/totalCounter;
          System.out.println(numbers);
          }
          
         
          even.close();
          odd.close();
          inputFile.close();
    File myFile=new File("odd.txt");
    Scanner keyboard =new Scanner(myFile);
    while (keyboard.hasNext()){
        String tmp=keyboard.nextLine();
       
        if (number%3==0){
                      totalCounter++;
                     total+=number;
                  }
        System.out.println(tmp);
       
    }
     odd.println(average=total/totalCounter);
    keyboard.close();
    odd.close();
    }
    }

  6. #6
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,060
    Rep Power
    6

    Default Re: Assignment

    Does that even compile?
    Get in the habit of using standard Java naming conventions!

  7. #7
    bada_bum is offline Member
    Join Date
    Oct 2013
    Posts
    4
    Rep Power
    0

    Default Re: Assignment

    I re wrote my code and right i cannot append the average to the end of my file
    Java Code:
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    package javaapplication40;
    
    import java.util.*;
    import java.io.*;
    public class JavaApplication40 {
    
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) throws IOException {
        int nbOdd = 0, nbEven = 0;
        int total=0,tmp=0,counter=0,averager=0;
        File file = new File("MainNumbers.txt");
        File oddFile=new File ("OddNumbers.txt");
        File evenFile=new File("EvenNumbers.txt");
        PrintWriter evenOutput=new PrintWriter(evenFile);
        PrintWriter oddOutput=new PrintWriter(oddFile);
        	PrintWriter output = new PrintWriter(file);
        	
        	for(int i = 1; i <= 101; i ++ )
        	{
        		output.println(i);
        	}
        	output.close();
     
        	
        	int[] numbers = new int[100];
            int [] evenNumbers=new int [50];
            int [] oddNumbers=new int [50];
        	
        	Scanner input = new Scanner(file);
        	
        	for(int i = 0; i < 100; i++)
        	{
        		numbers[i] = input.nextInt();
        	}
        	for (int i=0;i<100;i++){
                 
                if (numbers[i]%2==0){
                    evenNumbers[nbEven++]=numbers[i];
                    evenOutput.println(numbers[i]);
                }
               else {
                    oddNumbers[nbOdd++]=numbers[i];
                    oddOutput.println(numbers[i]);
               }
            }
        	input.close();
            evenOutput.close();
           oddOutput.close();
        	Scanner oddInput=new Scanner(oddFile);
              for (int i=0;i<evenNumbers.length;i++){
                  if(evenNumbers[i]%3==0) {
                      tmp=evenNumbers[i];
                      total+=tmp;
                      counter++;
                  }
              }
              oddOutput.println(averager);
              averager=total/counter;
              System.out.println(averager+"");
              oddInput.close();
              oddOutput.close();
    //    	for(int i = 0; i < 100; i++)
    //    	{
    //    		System.out.println(numbers[i] + " ");
    //    	}
    }
    }

Similar Threads

  1. Help with assignment
    By snobbysteven in forum New To Java
    Replies: 1
    Last Post: 10-20-2012, 07:24 AM
  2. Assignment Help
    By Albany in forum New To Java
    Replies: 6
    Last Post: 12-03-2011, 12:39 AM
  3. Can somebody help me in this assignment?
    By lulzim in forum New To Java
    Replies: 25
    Last Post: 03-02-2011, 12:02 AM
  4. Need help with a HW assignment
    By mackavelirip in forum New To Java
    Replies: 4
    Last Post: 02-17-2011, 02:36 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •