In: Computer Science
Working on a program but nothing is being sent to the output file. How can I fix this?
import java.util.Scanner;
import java.io.*;
public class Topic7Hw {
static Scanner sc = new Scanner (System.in);
public static void main(String[] args) throws
IOException {
PrintWriter outputFile = new
PrintWriter ("output.txt");
outputFile.println("hello");
final int MAX_NUM = 10;
int[] acctNum = new int[MAX_NUM];
double[] balance = new double[MAX_NUM];
System.out.print("Enter number of initial account:
");
int numAccts = sc.nextInt();
readAccts(acctNum, balance, numAccts);
//infinite loop
while(true)
{
menu();
System.out.print("Enter option: ");
char ch = sc.next().charAt(0);
//switch statement
switch(ch)
{
case 'W': case 'w':
withdrawal(acctNum, balance, numAccts);
break;
case 'D': case 'd':
deposit(acctNum, balance, numAccts);
break;
case 'N': case 'n':
numAccts = newAcct(acctNum, balance, numAccts);
break;
case 'B': case 'b':
balance(acctNum, balance, numAccts);
break;
case 'Q': case 'q':
System.exit(0);
case 'X': case 'x':
numAccts = deleteAcct(acctNum, balance,
numAccts);
default:
System.out.println("Wrong option. Try again.");
}
outputFile.close();
}
}
public static void menu() throws IOException{
System.out.println("Select one of
the following:");
System.out.println("W - Withdrawal");
System.out.println("D - Deposit");
System.out.println("N - New account");
System.out.println("B - Balance");
System.out.println("Q - Quit");
System.out.println("X - Delete Account");
}
public static void withdrawal(int[] acctNum, double[] balance, int
numAccts)
throws IOException {
System.out.print("Enter account number: ");
int account = sc.nextInt();
int i= findAcct(acctNum, numAccts, account);
if(i==-1)
{
System.out.print("Invalid account");
return;
}
System.out.print("Enter withdrawal amount: $");
double amount = sc.nextDouble();
if(amount<=0)
{
System.out.print("Invalid amount");
return;
}
System.out.println("Transaction Type:
withdrawal");
System.out.println("Account Number: " +
acctNum[i]);
System.out.println("Amount to withdrawal: $" +
amount);
System.out.println("Old Balance: $" +
balance[i]);
balance[i] = balance[i] - amount;
System.out.println("New Balance: $" +
balance[i]);
}
public static void deposit(int[] acctNum, double[] balance, int
maxAccts)
throws IOException {
System.out.print("Enter account number: ");
int account = sc.nextInt();
int i = findAcct(acctNum, maxAccts, account);
if(i==-1)
{
System.out.print("Invalid account");
return;
}
System.out.print("Enter deposit amount: $");
double amount = sc.nextDouble();
if(amount<=0)
{
System.out.print("Invalid amount");
return;
}
System.out.println("Transaction Type: Deposit");
System.out.println("Account Number: " + acctNum[i]);
System.out.println("Amount to Deposit: $" + amount);
System.out.println("Old Balance: $" + balance[i]);
balance[i] = balance[i] + amount;
System.out.println("New Balance: $" + balance[i]);
}
public static int newAcct(int[] acctNum, double[] balance, int
numAccts)
throws IOException {
System.out.print("Enter account number: ");
int acct = sc.nextInt();
int k = findAcct(acctNum, numAccts, acct);
if(k!=-1)
{
System.out.print("Account already exist!");
return numAccts;
}
acctNum[numAccts] = acct;
balance[numAccts] = 0;
return numAccts+1;
}
public static void balance(int[] acctNum, double[] balance, int
numAccts)
throws IOException {
System.out.print("Enter account number: ");
int acct = sc.nextInt();
int i = findAcct(acctNum, numAccts, acct);
if(i==-1)
{
System.out.print("Invalid account");
return;
}
System.out.println("Account Number: " + acctNum[i]);
System.out.println("Balance: $" + balance[i]);
}
public static int deleteAcct(int[] acctNum, double[] balance, int
numAccts)
throws IOException {
System.out.print("Enter account number: ");
int account = sc.nextInt();
int k = findAcct(acctNum, numAccts, account);
if(k==-1 || balance[k]!=0)
{
System.out.print("Invalid account");
return numAccts;
}
for(int i=k; i<numAccts-1; i++)
{
acctNum[i] = acctNum[i+1];
balance[i] = balance[i+1];
}
return numAccts-1;
}
public static void readAccts(int[] acctNum, double[] balance, int
maxAccts)
throws IOException {
for(int i=0; i<maxAccts; i++)
{
System.out.print("Enter account number: ");
acctNum[i] = sc.nextInt();
System.out.print("Enter initial balance: $");
balance[i] = sc.nextDouble();
}
}
public static void printAccts(int[] acctNum, double[] balance, int
maxAccts)
throws IOException {
System.out.println("Account number Balance ");
PrintWriter outputFile = new PrintWriter ("output.txt");
outputFile.print("Account number Balance ");
for(int i=0; i<maxAccts; i++)
{
System.out.println(acctNum[i] + "\t$" + balance[i]);
outputFile.print(acctNum[i] + "\t$" + balance[i]);
}
outputFile.close();
}
public static int findAcct(int[] acctNum, int maxAccts, int
account)
throws IOException {
for(int i=0; i<maxAccts; i++)
{
if(acctNum[i]==account)
return i;
}
return -1;
}
}
Dear Student,
You have to flush the stream. Thats the part missing in your code.
....
....
public static void main(String[] args) throws IOException {
PrintWriter outputFile = new PrintWriter ("output.txt");
outputFile.println("hello");
outputFile.flush(); // Add this line where ever you want to print.
final int MAX_NUM = 10;
int[] acctNum = new int[MAX_NUM];
double[] balance = new double[MAX_NUM];
...
...
Happy Learning.
Update as per Ask :
import java.util.Scanner;
import java.io.*;
import java.util.Scanner;
import java.io.*;
public class Topic7Hw {
static Scanner sc = new Scanner (System.in);
public static void main(String[] args) throws IOException {
PrintWriter outputFile = new PrintWriter ("output.txt");
outputFile.println("hello");
final int MAX_NUM = 10;
int[] acctNum = new int[MAX_NUM];
double[] balance = new double[MAX_NUM];
System.out.print("Enter number of initial account: ");
int numAccts = sc.nextInt();
readAccts(acctNum, balance, numAccts);
printAccts(acctNum, balance, numAccts);
//infinite loop
while(true)
{
menu();
System.out.print("Enter option: ");
char ch = sc.next().charAt(0);
//switch statement
switch(ch)
{
case 'W': case 'w':
withdrawal(acctNum, balance, numAccts);
break;
case 'D': case 'd':
deposit(acctNum, balance, numAccts);
break;
case 'N': case 'n':
numAccts = newAcct(acctNum, balance, numAccts);
break;
case 'B': case 'b':
balance(acctNum, balance, numAccts);
break;
case 'Q': case 'q':
System.exit(0);
case 'X': case 'x':
numAccts = deleteAcct(acctNum, balance, numAccts);
default:
System.out.println("Wrong option. Try again.");
}
outputFile.close();
}
}
public static void menu() throws IOException{
System.out.println("Select one of the following:");
System.out.println("W - Withdrawal");
System.out.println("D - Deposit");
System.out.println("N - New account");
System.out.println("B - Balance");
System.out.println("Q - Quit");
System.out.println("X - Delete Account");
}
public static void withdrawal(int[] acctNum, double[] balance, int numAccts)
throws IOException {
System.out.print("Enter account number: ");
int account = sc.nextInt();
int i= findAcct(acctNum, numAccts, account);
if(i==-1)
{
System.out.print("Invalid account");
return;
}
System.out.print("Enter withdrawal amount: $");
double amount = sc.nextDouble();
if(amount<=0)
{
System.out.print("Invalid amount");
return;
}
System.out.println("Transaction Type: withdrawal");
System.out.println("Account Number: " + acctNum[i]);
System.out.println("Amount to withdrawal: $" + amount);
System.out.println("Old Balance: $" + balance[i]);
balance[i] = balance[i] - amount;
System.out.println("New Balance: $" + balance[i]);
}
public static void deposit(int[] acctNum, double[] balance, int maxAccts)
throws IOException {
System.out.print("Enter account number: ");
int account = sc.nextInt();
int i = findAcct(acctNum, maxAccts, account);
if(i==-1)
{
System.out.print("Invalid account");
return;
}
System.out.print("Enter deposit amount: $");
double amount = sc.nextDouble();
if(amount<=0)
{
System.out.print("Invalid amount");
return;
}
System.out.println("Transaction Type: Deposit");
System.out.println("Account Number: " + acctNum[i]);
System.out.println("Amount to Deposit: $" + amount);
System.out.println("Old Balance: $" + balance[i]);
balance[i] = balance[i] + amount;
System.out.println("New Balance: $" + balance[i]);
}
public static int newAcct(int[] acctNum, double[] balance, int numAccts)
throws IOException {
System.out.print("Enter account number: ");
int acct = sc.nextInt();
int k = findAcct(acctNum, numAccts, acct);
if(k!=-1)
{
System.out.print("Account already exist!");
return numAccts;
}
acctNum[numAccts] = acct;
balance[numAccts] = 0;
return numAccts+1;
}
public static void balance(int[] acctNum, double[] balance, int numAccts)
throws IOException {
System.out.print("Enter account number: ");
int acct = sc.nextInt();
int i = findAcct(acctNum, numAccts, acct);
if(i==-1)
{
System.out.print("Invalid account");
return;
}
System.out.println("Account Number: " + acctNum[i]);
System.out.println("Balance: $" + balance[i]);
}
public static int deleteAcct(int[] acctNum, double[] balance, int numAccts)
throws IOException {
System.out.print("Enter account number: ");
int account = sc.nextInt();
int k = findAcct(acctNum, numAccts, account);
if(k==-1 || balance[k]!=0)
{
System.out.print("Invalid account");
return numAccts;
}
for(int i=k; i<numAccts-1; i++)
{
acctNum[i] = acctNum[i+1];
balance[i] = balance[i+1];
}
return numAccts-1;
}
public static void readAccts(int[] acctNum, double[] balance, int maxAccts)
throws IOException {
for(int i=0; i<maxAccts; i++)
{
System.out.print("Enter account number: ");
acctNum[i] = sc.nextInt();
System.out.print("Enter initial balance: $");
balance[i] = sc.nextDouble();
}
}
public static void printAccts(int[] acctNum, double[] balance, int maxAccts)
throws IOException {
System.out.println("Account number Balance ");
PrintWriter outputFile = new PrintWriter ("output.txt");
outputFile.print("Account number Balance ");
for(int i=0; i<maxAccts; i++)
{
System.out.println(acctNum[i] + "\t$" + balance[i]);
outputFile.print(acctNum[i] + "\t$" + balance[i]);
}
outputFile.close();
}
public static int findAcct(int[] acctNum, int maxAccts, int account)
throws IOException {
for(int i=0; i<maxAccts; i++)
{
if(acctNum[i]==account)
return i;
}
return -1;
}
}