Results 1 to 7 of 7
Thread: Really need help with this one
- 10-03-2011, 05:18 AM #1
Member
- Join Date
- Sep 2011
- Posts
- 29
- Rep Power
- 0
Really need help with this one
Hey everyone
-.gif)
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.
- 10-03-2011, 06:01 AM #2
Member
- Join Date
- Oct 2011
- Posts
- 7
- Rep Power
- 0
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
- 10-03-2011, 06:07 AM #3
Member
- Join Date
- Sep 2011
- Posts
- 29
- Rep Power
- 0
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
- 10-03-2011, 06:17 AM #4
Member
- Join Date
- Oct 2011
- Posts
- 7
- Rep Power
- 0
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
- 10-03-2011, 08:48 AM #5
Member
- Join Date
- Sep 2011
- Posts
- 29
- Rep Power
- 0
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();
- 10-03-2011, 03:53 PM #6
Member
- Join Date
- Oct 2011
- Posts
- 7
- Rep Power
- 0
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."
- 10-04-2011, 12:30 AM #7
Member
- Join Date
- Sep 2011
- Posts
- 29
- Rep Power
- 0


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks