This program is giving me an error in the main method at "outputFile.flush();" and "outputFile.close();" saying that it is unreachable code. Why is that, and 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);
while(true)
{
menu();
System.out.print("Enter an 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.flush();
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 exists! ");
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 {
PrintWriter outputFile = new
PrintWriter("output.txt");
outputFile.println("Hello 2");
System.out.println("Account number Balance ");
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.flush();
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;
}
}
The code is unreachable because the while loop runs infinite times. i have made a small change by introducing a boolean flag. Th code now works. I have highlighted the changes.
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);
boolean flag=true;
while(flag)
{
menu();
System.out.print("Enter an 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':
flag=false;
System.exit(0);
case 'X': case 'x':
numAccts = deleteAcct(acctNum, balance, numAccts);
default:
System.out.println("Wrong option. Try again.");
}
}
outputFile.flush();
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 exists! ");
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 {
PrintWriter outputFile = new PrintWriter("output.txt");
outputFile.println("Hello 2");
System.out.println("Account number Balance ");
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.flush();
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;
}
}

This program is giving me an error in the main method at "outputFile.flush();" and "outputFile.close();" saying...
If I enter a negative value the program throws an error and the withdrawal amount is added to the balance please help me find why? public abstract class BankAccount { private double balance; private int numOfDeposits; private int numOfwithdrawals; private double apr; private double serviceCharge; //Create getter and setter methods public double getBalance() { return balance; } public void setBalance(double balance) { this.balance = balance; } public int getNumOfDeposits() { return numOfDeposits; } public void setNumOfDeposits(int numOfDeposits) { this.numOfDeposits =...
I am trying to write a Geometry.java program but Dr.Java is giving me errors and I dont know what I am doing wrong. import java.util.Scanner; /** This program demonstrates static methods */ public class Geometry { public static void main(String[] args) { int choice; // The user's choice double value = 0; // The method's return value char letter; // The user's Y or N decision double radius; // The radius of the circle double length; // The length of...
I need to update the code listed below to meet the criteria listed on the bottom. I worked on it a little bit but I could still use some help/corrections! import java.util.Scanner; public class BankAccount { private int accountNo; private double balance; private String lastName; private String firstName; public BankAccount(String lname, String fname, int acctNo) { lastName = lname; firstName = fname; accountNo = acctNo; balance = 0.0; } public void deposit(int acctNo, double amount) { // This method is...
How would you write the following program using switch statements instead of if-else statements (in java) import java.util.*; public class ATM { public static void main(String[] args) { Scanner input = new Scanner (System.in); double deposit, withdrawal; double balance = 6985.00; //The initial balance int transaction; System.out.println ("Welcome! Enter the number of your transaction."); System.out.println ("Withdraw cash: 1"); System.out.println ("Make a Deposit: 2"); System.out.println ("Check your balance: 3"); System.out.println ("Exit: 4"); System.out.println ("***************"); System.out.print ("Enter Your Transaction Number "); transaction...
Could someone re-write this code so that it first prompts the user to choose an option from the calculator (and catches if they enter a string), then prompts user to enter the values, and then shows the answer. Also, could the method for the division be rewritten to catch if the denominator is zero. I have the bulk of the code. I am just having trouble rearranging things. ------ import java.util.*; abstract class CalculatorNumVals { int num1,num2; CalculatorNumVals(int value1,int value2)...
Help check why the exception exist do some change but be sure to use the printwriter and scanner and make the code more readability Input.txt format like this: Joe sam, thd, 9, 4, 20 import java.io.File; import java.io.PrintWriter; import java.io.IOException; import java.io.FileNotFoundException; import java.io.FileWriter; import java.util.Scanner; public class Main1 { private static final Scanner scan = new Scanner(System.in); private static String[] player = new String[622]; private static String DATA = " "; private static int COUNTS = 0; public static...
Need help debugging. first class seems fine. second class is shooting an error on s = super.getString(prompt); third class is giving me an error in the while loop where int num = console.getInt("Enter an integer:"); //-------------------------------------------------------------------------- import java.util.Scanner; public class Console { private Scanner sc; boolean isValid; int i; double d; public Console() { sc = new Scanner(System.in); } public String getString(String prompt) { System.out.print(prompt); return sc.nextLine();...
NOTE: Use the Account class codes in the section below these questions. Modify the main method in the CheckingAccountDemo class: Take out all previous code. Declare an object array with 10 accounts. Create a for loop to ask user to input each account’s information (name, account number, and initial balance) from keyboard and then initialize for each account object. Create a while loop to allow one to work on depositing and withdrawing operations on any account till one want to...
public static void main(String[] args) { System.out.println("Welcome to the Future Value Calculator\n"); Scanner sc = new Scanner(System.in); String choice = "y"; while (choice.equalsIgnoreCase("y")) { // get the input from the user System.out.println("DATA ENTRY"); double monthlyInvestment = getDoubleWithinRange(sc, "Enter monthly investment: ", 0, 1000); double interestRate = getDoubleWithinRange(sc, "Enter yearly interest rate: ", 0, 30); int years = getIntWithinRange(sc, "Enter number of years: ", 0, 100); System.out.println(); ...