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: read double from a file

  1. #1
    Member
    Join Date
    Jan 2013
    Location
    Central Texas
    Posts
    39
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default read double from a file

    I have created a "Savings Account" so to speak and I need to be able to read the balance from a file I have called balance.txt The file is formatted like this

    ******************************
     Tue Jun 04 23:59:51 CDT 2013
    ******************************
     Balance: $999.99
    ******************************
    How do I get it to read only the numbers after "Balance: $"?

    I am trying to do:

    Scanner s = new Scanner("path to file");
        if (s.hasNextDouble){
            double balance = s.nextDouble();
        }
    but it breaks the section of code where I write to the file.

    public void toFile() throws IOException{
     String stars = "******************************";
     String content = stars + "\n " + date.toString() + "\n" + stars + "\n Balance: $" + balance + "\n" + stars;
      File file = new File("path to file");
      FileWriter fw = new FileWriter(file.getAbsoluteFile());
      BufferedWriter bw = new BufferedWriter(fw);
      bw.write(content);
      bw.close();
    }
    Eclipse puts a red line under void and throws. If I take away the ; from the scanner declaration, writing to file is fixed but the if statement and the scanner declaration are borked.


  2. #2
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: read double from a file

    Scanner s = new Scanner("path to file");
        if (s.hasNextDouble){
            double balance = s.nextDouble();
        }
    What is the value of balance after this code runs? Verify it with a println or debugger. What exactly do you do with this value besides read it in this snippet?
    "but it breaks the section of code" This does not really explain what problem you have.
    Post a SSCCE or explain what is going on and what you are stuck on. (or both)

  3. #3
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    1,069
    Thanks
    7
    Thanked 215 Times in 191 Posts

    Default Re: read double from a file

    The compiler problem sounds like it could be mismatched braces. But that's only a guess without seeing the code.

Similar Threads

  1. Read input, read file, find match, and output... URGENT HELP!
    By MooseHead in forum What's Wrong With My Code?
    Replies: 3
    Last Post: April 3rd, 2012, 12:01 PM
  2. [SOLVED] Read double from console without having to read a string and converting it to double.
    By Lord Voldemort in forum File I/O & Other I/O Streams
    Replies: 3
    Last Post: June 26th, 2011, 09:08 AM
  3. [SOLVED] Read double value from console
    By Lord Voldemort in forum What's Wrong With My Code?
    Replies: 7
    Last Post: June 24th, 2011, 02:15 PM
  4. Read a text file and parse the contents of file
    By HelloAll in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: March 3rd, 2011, 05:47 AM
  5. how to read an integer of DOUBLE datatype with type casting
    By amr in forum What's Wrong With My Code?
    Replies: 3
    Last Post: December 14th, 2010, 03:03 PM