Results 1 to 7 of 7
  1. #1
    JojoDiaz is offline Member
    Join Date
    Sep 2011
    Posts
    29
    Rep Power
    0

    Post Really need help with this one

    Hey everyone

    What i need to do is write a program that reads a file containing integers, and then takes those integers and creates two new files. One with the even integers, and one with the odd integers.
    I also need a counter which keeps track of how many numbers i processed.

    This is what i have, but i'm not sure if I'm going in the right direction.


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

    public class FileInput
    {
    public static void main(String[] args) throws IOException
    {
    Scanner keyboard = new Scanner(System.in);
    System.out.print("enter your input filename: ");
    String fileName = keyboard.nextLine();

    File myFile = new File (fileName);
    // check for file existence
    if (!myFile.exists())
    {
    System.out.println("file not found");
    System.exit(0);
    }
    Scanner inputFile = new Scanner(myFile);

    String name;
    // detect the end of a file
    while (inputFile.hasNext())
    {
    name = inputFile.nextLine();
    System.out.println(name);
    }
    inputFile.close();
    }
    }


    and i know that
    if (i%2==0)
    then write to even
    if(n%2!==0)
    write file to odd

    Thanks in advance everyone.

  2. #2
    Opid is offline Member
    Join Date
    Oct 2011
    Posts
    7
    Rep Power
    0

    Default Re: Really need help with this one

    Looks good so far, are you having trouble implementing the if statements that test even or odd? i would do if even write to even file then do counter++ to keep track of how many numbers

  3. #3
    JojoDiaz is offline Member
    Join Date
    Sep 2011
    Posts
    29
    Rep Power
    0

    Default Re: Really need help with this one

    Hey Opid, i'm having trouble actually making it write out to two files one with even and one with odd numbers.

    Would it be this?

    PrintWriter outfile = new PrintWriter ("filename.even");

    IWhat i need to do is match the filename the user inputs. So if the file name is Java for example, it makes Java.even and Java.odd

  4. #4
    Opid is offline Member
    Join Date
    Oct 2011
    Posts
    7
    Rep Power
    0

    Default Re: Really need help with this one

    This is what i would do:
    System.out.println("Enter file");
    String outfileName = keyboard.nextLine();
    outfileName=outfileName+".even";
    System.out.println(outfileName);
    PrintWriter outfile = new PrintWriter (outfileName);
    then start printing :D

  5. #5
    JojoDiaz is offline Member
    Join Date
    Sep 2011
    Posts
    29
    Rep Power
    0

    Default Re: Really need help with this one

    That makes sense! but do i have to make another one for the odd?? I'm so confused with this program haha. This is what i have, maybe you could tell me what i have to do after this... i have the right idea i just haven't been able to write it into code.

    public static void main ( String [] args ) throws Exception
    {
    Scanner kb = new Scanner(System.in);
    System.out.print("enter your input filename: ");
    String fileName = kb.nextLine();
    fileName=fileName+".even";
    File myFile = new File (fileName);

    if (!myFile.exists())
    {
    System.out.println("file not found");
    System.exit(0);
    }

    Scanner inputFile = new Scanner(myFile);

    String name;

    while (inputFile.hasNext())
    {
    name = inputFile.nextLine();
    System.out.println(name);
    }
    inputFile.close();

    PrintWriter outputFile = new PrintWriter(fileName);
    if (n%2==0)
    outputFile.print(n);
    outputFile.close();

  6. #6
    Opid is offline Member
    Join Date
    Oct 2011
    Posts
    7
    Rep Power
    0

    Default Re: Really need help with this one

    Ok well first I think you have two lines switched around.
    This is your code:

    Scanner kb = new Scanner(System.in);
    System.out.print("enter your input filename: ");
    String fileName = kb.nextLine(); //Reads in file name
    fileName=fileName+".even"; //Adds .even to string
    File myFile = new File (fileName); // Creates your read in file BUT it will search for a "java.EVEN" not just "java"

    Try this:

    Scanner kb = new Scanner(System.in);
    System.out.print("enter your input filename: ");
    String fileName = kb.nextLine(); //Reads in file name
    String outPutFileEven=fileName;
    String outPutFileOdd=fileName;
    outPutFileEven+=".even";
    outPutFileOdd+=".odd";

    then do your my file line of code.
    That way the input file that the program is searching for will not have .even or .odd on it.
    Then in the if logic

    PrintWriter outEven = new PrintWriter(outPutFileEven);
    PrintWriter outOdd = new PrintWriter(outPutFileOdd);

    use the two different print writers then :D
    also the if logic will need to be in your for loop. "While file still has numbers, check if odd, check if even, print to appropriate file."

  7. #7
    JojoDiaz is offline Member
    Join Date
    Sep 2011
    Posts
    29
    Rep Power
    0

    Default Re: Really need help with this one

    Thank you so much for your help!

Posting Permissions

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