File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes go back to top of class and write/read to file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "go back to top of class and write/read to file" Watch "go back to top of class and write/read to file" New topic

go back to top of class and write/read to file

Caleb Jones

Joined: Jun 14, 2005
Posts: 1
I was wondering if i could take this code and at the end of if(deposit.equals("y") statement whether or not it is possible to start over at the top of the class (and still keep the new balance). Also, is it possible to get the current balance from a file and then save it to the file when im done with the application?

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

class bank {
public static void main(String args[]) {
double balance;
balance = 0.00;

out.println("Your current balance is $" + balance + ".");
out.println("Do you want to make a deposit? (y\\n)");
Scanner depositScanner = new Scanner(;
String deposit =;
if (deposit.equals("y")) {
out.println("Please enter your deposit amount:");
Scanner depositamountScanner = new Scanner(;
double depositamount = depositamountScanner.nextDouble();
balance = balance + depositamount;
out.println("Your new deposit amount is: $" + balance + ".");
else {
("Please come again.")


[ EJFH: Remove exhortation to take discussions offline; we don't do that here. ]
[ June 15, 2005: Message edited by: Ernest Friedman-Hill ]
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Hi, welcome to the ranch!

You're probably looking for a looping construct, maybe "while". We like to hint at answers here and let you have the joy of working out the details (no sarcasm intended, honest!) so this isn't even Java...

Another hint - I'd try to make a routine that is exactly that many lines of Java. A line like "get the deposit amount" turns into a call to a new method that does the kind of prompt and read you already have.

After you get that going, think about the file bit. Read from file (if it exists), do the stuff above, rewrite the file.

Give this a try, feel encouraged to post more code. Have fun!

A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Hentay Duke
Ranch Hand

Joined: Oct 27, 2004
Posts: 198
Just the phrase "go back to top of class" reeks of bad Object Oriented design.

Follow the advice Stan is giving you and while you're doing it try to think more object oriented. In java programs don't start at the top of a class per se. You have an entry point, such as the main method in a stand alone app, and from there you call other objects and methods to achieve what you need. in fact I'd take most of your code out of the main method and just call a constructor from there.
[ June 15, 2005: Message edited by: Hentay Duke ]
I agree. Here's the link:
subject: go back to top of class and write/read to file
It's not a secret anymore!