Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / October 2005

Tip: Looking for answers? Try searching our database.

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

Thread view: 
klub - 27 Oct 2005 10:48 GMT
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
klub - 27 Oct 2005 10:50 GMT
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
Roedy Green - 27 Oct 2005 11:11 GMT
>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.
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.

klub - 27 Oct 2005 12:49 GMT
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)
Roedy Green - 27 Oct 2005 13:03 GMT
>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.

Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.

klub - 27 Oct 2005 12:50 GMT
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
Roedy Green - 27 Oct 2005 13:05 GMT
>// to Double dd from String g
>try {
[quoted text clipped - 3 lines]
>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.
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.

klub - 27 Oct 2005 21:44 GMT
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);
        }
}
Roedy Green - 31 Oct 2005 11:10 GMT
>            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);
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.