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".