Write number variable to text file... not working... help!

K

klub

okay basically im a new java programmer and am trying to make a simple
bank account typish program... one that you can deposit and withdraw
and all and the balance will be read from/writen to a file, so that it
can be a permanent use. I use JCreator and compile/run directly in the
program. Here is my code:

import static java.lang.System.out;
import java.util.Scanner;
import java.io.*;

class bank {

public static void main (String args []) throws IOException {
Scanner inputScanner = new Scanner(System.in);
Scanner balanceScanner = new Scanner(new File("C:\\Documents and
Settings\\Caleb\\My Documents\\java\\balance.txt"));
double balance = balanceScanner.nextDouble();

out.println("Your account balance is $" + balance);
out.println("Would you like to make a deposit or withdrawal?");
out.println("d=deposit, w=withdrawal");
String action = inputScanner.next();

if (action.equals("d")) {
out.println("How much would you like to deposit?");
double depositAmount = inputScanner.nextDouble();
balance = balance + depositAmount;
out.println("Your balance is now $" + balance);

FileWriter out = new FileWriter("C:\\Documents and
Settings\\Caleb\\My Documents\\java\\balance.txt");
out.write(balance);
out.close();
} if (action.equals("w")) {
out.println("How much would you like to withdraw?");
double withdrawalAmount = inputScanner.nextDouble();
balance = balance - withdrawalAmount;
out.println("Your balance is now $" + balance);

}

}
}

The error message it shows is: "Cannot find symbol method
write(double), line 24, which is out.write(balance);... and i dont have
any clue how to fix it so that the double variable "balance" will be
written back to that file
 
K

klub

oh one more thought is that i tried instead of writing a variable to
the file i just did out.write("test");... this worked fine, but my
variable thing wont work still
 
R

Roedy Green

The error message it shows is: "Cannot find symbol method
write(double), line 24, which is out.write(balance);... and i dont have
any clue how to fix it so that the double variable "balance" will be
written back to that file

First, bank is a class, so should be spelled Bank and live in a file
called Bank.java.

Second: I modified your code to :
Scanner balanceScanner = new Scanner(new
File("C:/temp/balance.txt"));

so that I could run it on my machine. You might like to invent some
some shorter-to-type dir names to keep your experiments.

Third System.out is a PrintStream. Its most common methods are
print() and println( ... );

PrintStream has a low level write ( byte ) method for writing raw
bytes to the stream, but that is not what you want to do. You want to
print a double, converting it first to string of characters. You just
used the wrong method.

You could convert the double to String and print it or let print() do
the default conversion.

see http://mindprod.com/applets/converter.html

I am impressed with your use of the static import. That is first time
I have seen a newbie take advantage of it to shorten System.out.
 
K

klub

so i take it i make the converter be double-->string and then it gives:
// to Double dd from float f
dd = new Double(f);

what exactly do i do with that?

p.s. thanks a ton for the help... and about the static import thing i
got that outta the book im using (Java 2 for dummies... which doesnt
explain how to write stuff to files)
 
K

klub

oops... i meant
// to Double dd from String g
try {
/* best, locale-insensitive*/ dd = new Double(g);
/* or locale-insensitive */ dd = Double.valueOf(g);
} catch (NumberFormatException e){}
for that conversion thing heh musta hit the scroll bar on my mouse or
something
 
R

Roedy Green

so i take it i make the converter be double-->string and then it gives:
// to Double dd from float f
dd = new Double(f);

what exactly do i do with that?

You are not trying to convert float to double.
You are trying to convert double to String
It is an applet not a static display.
 
R

Roedy Green

// to Double dd from String g
try {
/* best, locale-insensitive*/ dd = new Double(g);
/* or locale-insensitive */ dd = Double.valueOf(g);
} catch (NumberFormatException e){}
for that conversion thing heh musta hit the scroll bar on my mouse or
something

you did not mean that either. Double is not the same as double.

You want the code to convert from double to String.

in other words, the top line should say "to String from double"

and you select one of the techniques shown.
 
K

klub

hey thanks dude i finally figured it out using this:
import java.io.*;
class double_to_string {

public static void main (String args[]) throws IOException {
double input = 34.78;
String output = new String();

output = String.valueOf(input);
System.out.println(output);
}
}
 
R

Roedy Green

double input = 34.78;
String output = new String();

output = String.valueOf(input);
System.out.println(output);

you can shorten that to:

double input = 34.78d;
String output = String.valueOf(input);
System.out.println(output);

And since println is smart you can collapse that further to:

double input = 34.78d;
System.out.println(input);

or even:
System.out.println(34.78d);
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top