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

Read a text file into arrays and output - Java Program ------------------------------------------------------------------------------ I need to have...
Read a text file into arrays and output - Java Program ------------------------------------------------------------------------------ I need to have a java program read an "input.txt" file (like below) and store the information in an respective arrays. This input.txt file will look like below and will be stored in the same directory as the java file. The top line of the text represents the number of people in the group for example. the lines below it each represent the respective persons preferences in regards...
Java - Text File to Arrays and output ------------------------------------------------------------------------------ I need to have a java program...
Java - Text File to Arrays and output ------------------------------------------------------------------------------ I need to have a java program read an "input.txt" file (like below) and store the information in an respective arrays. This input.txt file will look like below and will be stored in the same directory as the java file. The top line of the text represents the number of people in the group for example. the lines below it each represent the respective persons preferences in regards to the other...
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!
Hello, How can I make the program print out "Invalid data!!" if the file has a...
Hello, How can I make the program print out "Invalid data!!" if the file has a grade that is not an A, B, C, D, E, or F or a percentage below zero. The program should sort a file containing data about students like this for five columns: one for last name, one for first name, one for student ID, one for student grade percentage, one for student grade. Smith Kelly 438975 98.6 A Johnson Gus 210498 72.4 C Reges...
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...
How can I check to see if 2 arrays contain the same element using nothing worse...
How can I check to see if 2 arrays contain the same element using nothing worse than linear runtime? (Java) For example if I have 2 arrays with elements {7, 8, 5, 4, 3} and {10, 12, 15, 20, 8} I would want it to return true since there is an 8 in each array.
C++ code a program should prompt the user for the name of the output file. The...
C++ code a program should prompt the user for the name of the output file. The file should be opened for write operations. Values calculated by the program will be written to this file. The program must verify that the file opened correctly. If the file did not open, an error message should be printed and the user should be prompted again. This should continue until the user supplies a valid filename.
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