Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Fliereader/Filewriter (Need Help!)

Threaded View

  1. #1
    Junior Member
    Join Date
    Nov 2012
    Posts
    2
    My Mood
    HaHa
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Question Fliereader/Filewriter (Need Help!)

    Ok so Im trying to make a program in Java that will act as an ATM. What I want it to be able to do is have someone enter a starting balance and then have the ability to withdraw money and deposit money into that account. The hard part is that I want the program to keep that account balance, so even after I close the program, the amount in the balance is still stored. Right now im just doing a little test try out FileWriter and FileReader in Java. So here is my code:

    import java.io.*;
    import java.util.Scanner;
     
    public class Files{
     
    	public static void main(String[] args) throws IOException
    	{
     
    	Scanner input = new Scanner( System.in );
     
    	int num;
     
    	System.out.print("Enter an integer: ");
    		num = input.nextInt();
     
    	File f;
    	f = new File ("File.txt");
     
    	if(!f.exists())
    	{
     
    		f.createNewFile();
    		System.out.println( "New file File.txt has been created" );
     
    	}
     
    	FileWriter fs = new FileWriter(f);
    	BufferedWriter op = new BufferedWriter(fs);
     
    	op.write("" + num);
    	op.close();
     
    	FileInputStream ifs = new FileInputStream(f);
    	DataInputStream in = new DataInputStream(ifs);
    	BufferedReader br = new BufferedReader(new InputStreamReader(in));
    	int str;
     
    	while((str = br.readLine() != null))
    	{
    		System.out.println(str);
    	}
     
    	in.close();
     
    	}
    }

    I keep getting this error "required : boolean found: int" and "required : int found: boolean" Im assuming it has to do with the line "while((str = br.readLine() !null))

    Thanks! :)

    import java.io.*;
    import java.util.Scanner;
     
    public class Files{
     
    	public static void main(String[] args) throws IOException
    	{
     
    	Scanner input = new Scanner( System.in );
     
    	int num;
     
    	System.out.print("Enter an Integer: ");
    		num = input.nextInt();
     
    	File f;
    	f = new File ("File.txt");
     
    	if(!f.exists())
    	{
     
    		f.createNewFile();
    		System.out.println( "New file File.txt has been created" );
     
    	}
     
    	FileWriter fs = new FileWriter(f);
    	BufferedWriter op = new BufferedWriter(fs);
     
    	op.write("" + num);
    	op.close();
     
    	FileInputStream ifs = new FileInputStream(f);
    	DataInputStream in = new DataInputStream(ifs);
    	BufferedReader br = new BufferedReader(new InputStreamReader(in));
    	int str;
     
    	while(str == br.readLine())
    	{
    		System.out.println(num);
    	}
     
    	in.close();
     
    	}
    }

    I got rid of the != null, since from my understand, null is mostly used for string. I have used this program before for doing almost this same job, but instead of using an integer, I used a String. It worked like a charm, and it printed out the String correctly. This time however, I can get the integer to save to the "File.txt" but it wont reprint the value. After fixing it and recompiling it, I not get the error "Incomparable Types: int and String".
    Last edited by Techsam; November 12th, 2012 at 09:09 PM. Reason: I looked over my code, I think I might have fixed some of it, but its still not perfect.