Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

We are suppose to write an application that reads a file with integers in it, an

ID: 3629610 • Letter: W

Question

We are suppose to write an application that reads a file with integers in it, and creates two new files. One containing even integers and another containing odd integers. The problem i'm having is we are suppose to name the output files based on whatever the user file is. So for example: if the users file is "Usersfile", the output files should be "Userfile.even" and "Userfile.odd". I cant figure out how i'm suppose to make this happen. I just put in "file.even" and "file.odd" in as place holders for now.

//------------------------------------------code-----------------------------------------------------

import java.util.Scanner;
import java.io.*;

class Hw03
{
//--------------------------------------------------------------------------------------------
   public static void main(String[] args) throws IOException
   {
      Scanner kb = new Scanner(System.in);
      System.out.print("Enter name of file containing integers: ");
      String fileName = kb.nextLine();

      File file = new File(fileName);
      Scanner inputFile = new Scanner(file);

      PrintWriter outputFile1 = new PrintWriter("file.even");
      PrintWriter outputFile2 = new PrintWriter("file.odd");
      int numElements = 0;
      int even = 0;
      int odd = 0;
      while (inputFile.hasNext())
      {
          numElements=inputFile.nextInt();
          System.out.println(numElements);
          if (numElements%2==0)
          {
             outputFile1.println(numElements);
             even++;
          }
          else
          {
             outputFile2.println(numElements);
             odd++;
          }
       }
       inputFile.close();
       outputFile1.close();
       outputFile2.close();

        System.out.println("Processed "+(even+odd)+" integers. "+even+" even "+odd+" odd");

   }
//--------------------------------------------------------------------------------------------
}// end class HW03

Explanation / Answer

This code!!! Are you perhaps in CS 141 in Cal poly? If so, then you are a classmate haha! Anyways, here is my code... be sure not to copy it word for word but instead study from it.

public static void main(String[] args) throws Exception
   {
      Scanner keyboard=new Scanner(System.in);
      String filename;
      System.out.print("Enter name of files containing integers: ");
      filename=keyboard.nextLine();
      File file=new File(filename+".txt");
      if (!file.exists())
      {
         System.out.println("The file name "+filename+" does not exist");
         System.exit(0);
      }
      Scanner readfile= new Scanner(file);
      PrintWriter evenwrite = new PrintWriter(filename+".even.txt");
      PrintWriter oddwrite = new PrintWriter(filename+".odd.txt");
      int count=0;
      int numEven=0;
      int numOdd=0;
      while (readfile.hasNext())
      {
         count++;
         int num=readfile.nextInt();
         if (num%2==0)
         {
            evenwrite.println(num);
            numEven++;
         }
         else
         {
            oddwrite.println(num);
            numOdd++;
         }
      }
      System.out.println("Processed "+count+" integers");
      System.out.println(numEven+" even");
      System.out.println(numOdd+" odd");
      evenwrite.close();
      oddwrite.close();
      readfile.close();
   }

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote