Question

In: Computer Science

Working on a program but nothing is being sent to the output file. How can I...

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;
}


}

Solutions

Expert Solution

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;

}

}


Related Solutions

How do I do this: Write a program that can read a text file of numbers...
How do I do this: Write a program that can read a text file of numbers and calculate the mean and standard deviation of those numbers. Print the result in another text file. Put the result on the computer screen. EACH LINE OF THE PROGRAM MUST BE COMMENTED!
Write a program that creates an output file named rand_nums.txt. Open the file and write 100...
Write a program that creates an output file named rand_nums.txt. Open the file and write 100 random integers between -50 and +50 (inclusive) to the file. Be sure to handle any file IO exceptions. Remember to close the file. Write a program that opens rand_nums.txt for input. Create two output files pos.txt and neg.txt. Read through the input file, one line at a time, converting each line into an integer (no exception handling, yet). If the number is positive, write...
C Programming: Write a program that accepts 2 arguments, an input file and an output file....
C Programming: Write a program that accepts 2 arguments, an input file and an output file. The program is to store in the output file the contents of the input file in reverse. If the input file looks like this: Hello World.\n This is Line 2\n This is the end.\n then the output file should look like this: \n .dne eht si sihT\n 2 eniL si sihT\n .dlroW olleH The main program should look like this: int main(int argc, char...
I am not quite sure how to get this program working or how to start it...
I am not quite sure how to get this program working or how to start it in the first place. Write a full Java program and do the following: 1- Create a generic class and declare a one dim array as a private member. 2- Add a constructor to initialize the array. 3- A set method to set the array. 4- Sort method to sort the array. 5- Print method to print the array. 6- Reverse method to reverse the...
In C Programming Language Write a program to output to a text log file a new...
In C Programming Language Write a program to output to a text log file a new line starting with day time date followed by the message "SUCCESSFUL". Please screenshot the results.
Objectives To learn to code, compile, and run a program using file input and an output...
Objectives To learn to code, compile, and run a program using file input and an output file. Assignment Plan and code a program utilizing one file for input and one file for output to solve the following problem: Write a program to determine the highest number, the lowest number, their total, and the average of each line of numbers in a file. A file contains 7 numbers per line. How many lines a file contains is unknown. Note Label all...
Using java, I need to make a program that reverses a file. The program will read...
Using java, I need to make a program that reverses a file. The program will read the text file character by character, push the characters into a stack, then pop the characters into a new text file (with each character in revers order) EX: Hello World                       eyB       Bye            becomes    dlroW olleH I have the Stack and the Linked List part of this taken care of, I'm just having trouble connecting the stack and the text file together and...
How do I write a C++ program to call a frequency table from a csv file,...
How do I write a C++ program to call a frequency table from a csv file, using vector? Data given is in a csv file. Below is part of the sample data. Student ID English Math Science 100000100 80 90 90 100000110 70 60 70 100000120 80 100 90 100000130 60 60 60 100000140 90 80 80
How would I complete this program using three files, which are a "SoccerPlayer.h" file, a "SoccerPlayer.cpp"...
How would I complete this program using three files, which are a "SoccerPlayer.h" file, a "SoccerPlayer.cpp" file, and a "main.cpp" file. C++ Please! This program will store roster and rating information for a soccer team. Coaches rate players during tryouts to ensure a balanced team. (1) Prompt the user to input five pairs of numbers: A player's jersey number (0 - 99) and the player's rating (1 - 9). Store the jersey numbers in one int vector and the ratings...
c# code working but output not right, I need to output all numbers like : Prime...
c# code working but output not right, I need to output all numbers like : Prime factors of 4 are: 2 x 2 here is just 2 Prime factors of 7 are: 7 Prime factors of 30 are: 2 x 3 x 5 Prime factors of 40 are: 2 x 2 x 2 x 5 here is just 2,5 Prime factors of 50 are: 2 x 5 x 5 here is just 2,5 1) How I can fix it 2)I...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT