Fileinput and output stream

hi. i was trying to learn this fileinput and output stream in java. i was trying to solve this exercise problem in which a user need to ask for the inputfile (Which i have created mydata.txt which has 5 positive numbers and 5 negative numbers ). my program will ask the user for the input file and then output the results in 2 seperate files one output will be for positive integers which the program will extract from myData,txt and negative integers to another output file. i am very confused. my program does not give me any error msgs but outputs a number 0.

import java.util.Scanner;

import java.io.*;

class fileEg{

publicstaticvoid main (String[] args)throws IOException

{

int num=0, num1;

Scanner user =new Scanner(System.in);

String inputFileName, outputFileName, fileName;

System.out.println("Input File Name ");

inputFileName = user.nextLine().trim();

File input =new File (inputFileName);

Scanner scan =new Scanner(input);

System.out.print("Output File Name: ");

outputFileName = user.nextLine().trim();

File output =new File( outputFileName );

PrintStream print =new PrintStream( output );

while(scan.hasNextInt()){

if(num =='+'){

print.println("the numbers are " + num);

}

else{

print.println("The numbers ares " + num);

}

print.close();

}

}

}

any help will be really appreciated. any theories or example.

Thanks

[2438 byte] By [fastmikea] at [2007-11-26 13:19:33]
# 1
Well you never actually assign anything to num after the first time, when you initialize it in the declaration to zero.
paulcwa at 2007-7-7 17:46:27 > top of Java-index,Java Essentials,New To Java...
# 2
allrite i changed my code num = scan.nextInt();still the program output is 10 which is the total length of the file 10 numbers .
fastmikea at 2007-7-7 17:46:27 > top of Java-index,Java Essentials,New To Java...
# 3

import java.util.Scanner;

import java.io.*;

class fileEg {

public static void main (String[] args) throws IOException

{

int num, num1;

Scanner user = new Scanner(System.in);

String inputFileName, outputFileName, fileName;

System.out.println("Input File Name ");

inputFileName = user.nextLine().trim();

File input = new File (inputFileName);

Scanner scan = new Scanner(input);

System.out.print("Output File Name: ");

outputFileName = user.nextLine().trim();

File output = new File( outputFileName );

PrintStream print = new PrintStream( output );

while(scan.hasNextInt()) {

num= scan.nextInt();

if(num == '+') {

print.println( "the numbers are " + num);

}

else {

print.println("The numbers ares " + num);

}

print.close();

}

}

}

fastmikea at 2007-7-7 17:46:27 > top of Java-index,Java Essentials,New To Java...
# 4
> still the program output is 10 which is the total> length of the file 10 numbers .I don't understand what you mean by that. The length of what file?
paulcwa at 2007-7-7 17:46:27 > top of Java-index,Java Essentials,New To Java...
# 5

> while(scan.hasNextInt()) {

>num= scan.nextInt();

> if(num == '+') {

>print.println( "the numbers are " + num);

> }

>else {

> print.println("The numbers ares " + num);

>}

> print.close();

>}

num is never going to be '+' (becuase you only read it when it's an int, and + isn't an int), so that if/else statement doesn't make any sense.

Basically your code should read numbers until it finds a + (or any other non-int) at which point it will close the file and quit.

paulcwa at 2007-7-7 17:46:27 > top of Java-index,Java Essentials,New To Java...
# 6

what i mean to say paul is i have a txt file by the name of myData.txt. i have following numbers in the txt file.

10

20

30

40

50

-10

-20

-30

-40

-50

my program is asking the input file from a user which i have achieved. what i really want to do is create 2 output files one for all positive integers and one for all - integeres say for instance when i run my program this is what it ask.

Input File Name

myData.txt

Output File Name: saloo4.txt

Process completed.

when i check my saloo4.txt file it says :

the numbers are 10

so i changed my if statement a little bit.

while(scan.hasNextInt()) {

num= scan.nextInt();

if(num >0) {

print.println( "the numbers are " + num);

}

else {

print.println("The numbers ares " + num);

}

print.close();

}

still it is giving me the same thing.

fastmikea at 2007-7-7 17:46:27 > top of Java-index,Java Essentials,New To Java...
# 7

If you want to create two output files, then create two PrintStreams, one for each file. Don't call them "print", call them (say) "negativeNumbers" and "positiveNumbers").

Then you can do something like this:while(scan.hasNextInt()) {

num= scan.nextInt();

if (num >0) {

positiveNumbers.println( "the numbers are " + num);

} else {

negativeNumbers.println("The numbers ares " + num);

}

}

print.close();

Also, look at the print.close() before. You put that inside of the while loop,. which means that it would close the output stream immediately after producing a single line of output. I'm kind of confused how you managed to run this without getting an exception...were you getting an IOException each time you ran this?

paulcwa at 2007-7-7 17:46:27 > top of Java-index,Java Essentials,New To Java...
# 8

ahhhh great scott. errr i hate this programming and i love it also i guess drinking beer and programming is a bad idea lol. That was the problem paul i had that print.close() inside the while loop omg how can i do that

Thanks alot a bunch paul and no the compiler did not give me any IO exception also. if it would have that i would have done something about ut. Thanks a bunch i am not new in java just completed the first stage with all the loops while do while for if and else statement now moving to second stage printfile and then methods and then arrays and then i am done lol. this is really a kool website i urge all the newbies to go and learn java from this site.

http://chortle.ccsu.edu/java5/cs151java.html#23

and solve all the exercise problems

fastmikea at 2007-7-7 17:46:27 > top of Java-index,Java Essentials,New To Java...
# 9

import java.util.Scanner;

import java.io.*;

class fileEg {

public static void main (String[] args) throws IOException

{

int num;

Scanner user = new Scanner(System.in);

String inputFileName, outputFileName, negativeFileName;

System.out.println("Input File Name ");

inputFileName = user.nextLine().trim();

File input = new File (inputFileName);

Scanner scan = new Scanner(input);

System.out.print("Output File positive Name: ");

outputFileName = user.nextLine().trim();

File output1 = new File( outputFileName );

PrintStream positiveNumbers = new PrintStream( output1 );

System.out.print("Output File negative Name: ");

negativeFileName = user.nextLine().trim();

File output = new File( negativeFileName );

PrintStream negativeNumbers = new PrintStream( output );

while(scan.hasNextInt()) {

num= scan.nextInt();

if (num >0) {

positiveNumbers.println( "the numbers are " + num);

} else {

negativeNumbers.println("The numbers ares " + num);

}

}

positiveNumbers.close();

negativeNumbers.close();

}

}

Thanks paul

Message was edited by:

fastmike

fastmikea at 2007-7-7 17:46:27 > top of Java-index,Java Essentials,New To Java...