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!)

  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 08:09 PM. Reason: I looked over my code, I think I might have fixed some of it, but its still not perfect.

  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,262
    Thanks
    176
    Thanked 821 Times in 764 Posts
    Blog Entries
    5

    Default Re: Fliereader/Filewriter (Need Help!)

    See the API for the classes and methods you are using.
    BufferedReader (Java Platform SE 6)
    Note the returned value from readLine is a String. Why is the variable str of type int? Easy fix is to change it to a String.

  3. The Following User Says Thank You to copeg For This Useful Post:

    Techsam (November 12th, 2012)

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

    Default Re: Fliereader/Filewriter (Need Help!)

    I have str set as a variable int because I was thinking that since the data that is stored on the "File.txt" that is going to be read is an int, that the variable str should be an integer. Ill see if changing the variable to a String will help. Thanks!

    --- Update ---

    Ok, it was a combination of the variable str, and a small little error in my brackets. Its working now! Thanks a million!