I'm doing a project where I have to catch a user inputting a string into one of the prompt.
Problem is it seems I always get catch without a try error no matter what I do.
it keeps saying expected ";" on the catch statement... which is incorrect.
error(s) are appearing at line 49 - try without a catch.
Line 102 - catch without a try.
line 92 - while statement is unreachable.
and line 107 which is a bracket it says " class, or enum expected".
I couldn't tell you what I did fix because my original code was something completely different so I restarted a few things to fix certain problems that could occur.
I've looked around the internet found bad examples to try to fix my code with myself.
//This is a program that will calculate a monthly payment and loop it. If user gives bad data you will be prompted. package monthypayment2; import java.text.NumberFormat; import java.util.Scanner; public class Monthypayment2 { private static double monthlypay1; private static String choice; private static boolean n; private static String False; private static boolean y; /** * @param args the command line arguments */ public static void main(String[] args) { Scanner in = new Scanner(System.in); //format number and currency NumberFormat NF = NumberFormat.getCurrencyInstance(); double months; double numyears; double loanamount; double rate; double monthlypay1; String choice = "y"; double calc1; double calc2; double calc3; double calc4; while (choice.equals("y")) { try { //prompt loan amount loanamount=0; rate = 0; numyears = 0; System.out.print("Enter Loan Amount:"); loanamount = Double.parseDouble(in.nextLine()); //prompt rate System.out.print("Enter Rate:"); rate = Double.parseDouble(in.nextLine()); //prompt years System.out.print("Enter Number of Years:"); numyears = Double.parseDouble(in.nextLine()); rate = rate / 12 / 100; months = 12 * numyears; //calculation monthly calc1 = loanamount * rate; calc2 = (Math.pow (1 + rate, months)); calc3 = (calc2 -1); calc4 = (calc2 / calc3); monthlypay1 = loanamount * rate * calc4; if ((loanamount > 0 && rate > 0) && numyears > 0 ) { System.out.println("The Monthly Payment is:" + NF.format (monthlypay1)); } else { System.out.println("You need to enter positive numerical data!"); } break; } while (monthlypay1 < 0) { System.out.print("You need to enter positive numerical data!"); break; } System.out.print(""); System.out.print("Would you like to continue calculations( y / n)?"); choice = in.nextLine(); catch (Exception e) { } } } }