So what this program does is calculate tax by taking in a filing status and income, now I achieved the goal of my assignment but I want to go beyond cause what other way is there to learn. So what I did was try to have user input weather they would like to run the program again by entering y or n then making it uppercase the going to an if statement Y to run ,N to quit but when my program gets to the part to take in a string it crashes and says
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 0
at java.lang.String.charAt(Unknown Source)
at computingTax.run(computingTax.java:180)
at computingTax.main(computingTax.java:195)
what am I doing wrong? The error starts in the run method towards the end there is a comment "//error starts here"
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 0
at java.lang.String.charAt(Unknown Source)
at computingTax.run(computingTax.java:180)
at computingTax.main(computingTax.java:195)
what am I doing wrong? The error starts in the run method towards the end there is a comment "//error starts here"
import java.util.Scanner; public class computingTax { public int status; public double tax; public double income; //All the tax rates are laid out here depending on filing status //All the math was done to add the max tax from the previous brackets //if the income went into the next tax bracket. public double taxRate(int status,double income){ if(status==0){ if(income>=0 && income<=8350){ tax = income *.10; } if(income>=8351 && income<=33950){ income = income-8350; tax = income *.15; tax= tax+835; } if(income>=33951 && income<=82250){ income= income -33950; tax = income *.25; tax = tax + 4674.85; } if(income>=82251 && income<=171550){ income= income-82250; tax = income *.28; tax = tax +16749.6; } if(income>=171551 && income<=372950){ income= income -171550; tax = income *.33; tax = tax + 39074.35; } if(income>= 372951){ income = income - 372950; tax = income *.35; tax = tax + 105536.02; } } if(status==1){ if(income>=0 && income<=16700){ tax = income *.10; } if(income>=16701 && income<=67900){ income= income-16700; tax = income *.15; tax = tax + 1670; } if(income>=67901 && income<=137050){ income=income-67900; tax = income *.25; tax = tax+9349.85; } if(income>=137051 && income<=208850){ income=income-137050; tax = income *.28; tax = tax + 26637.1; } if(income>=208851 && income<=372950){ income=income-208850; tax = income *.33; tax= tax +46740.82; } if(income>=372951){ income=income-372950; tax = income *.35; tax = tax +100893.49; } } if(status==2){ if(income>=0 && income<=8350){ tax = income *.10; } if(income>=8351 && income<=33950){ income= income-8350; tax = income *.15; tax = tax + 835; } if(income>=33951 && income<=68525){ income=income-33950; tax = income *.25; tax = tax +4674.85; } if(income>=68525 && income<=104425){ income=income -68525; tax = income *.28; tax = tax +13318.35; } if(income>=104426 && income<=186475){ income=income -104425; tax = income *.33; tax = tax + 23370.35; } if(income>= 186476){ income=income -186475; tax = income *.35; tax = tax + 50446.52; } } if(status==3){ if(income>=0 && income<=11950){ tax = income *.10; } if(income>=11951 && income<=45500){ income= income-11950; tax = income *.15; tax = tax +1195; } if(income>=45501 && income<=117450){ income=income-45500; tax = income *.25; tax = tax + 6227.35; } if(income>=117451 && income<=190200){ income=income-117450; tax = income *.28; tax = tax + 24214.6; } if(income>=190201 && income<=372950){ income =income-190200; tax = income *.33; tax = tax + 44584.32; } if(income>=372950){ income =income-372950; tax = income *.35; tax = tax + 104891.49; } } return tax; } public void run(){ int a=0; double b=0; double taxrate; String S1; char choice; Scanner input = new Scanner (System.in); try { do{ System.out.println("Enter your filing status:"); a = input.nextInt();//ask for the filing status if (a<0 || a>3){ System.out.print("Please enter a number 0-3\n"); Thread.sleep(1000); }//if a invalid filing status is entered a message will print }while(a<0 || a>3); } catch (InterruptedException e) { e.printStackTrace(); }// Here we are stuck in a loop till a valid entry is entered try { do{ System.out.println("Enter the taxable income:"); b = input.nextInt();// ask for the income if(b<0){ System.out.println("Please enter a positive income or enter 0"); Thread.sleep(1000); }//if a invalid income is entered a message will print }while(b<0); } catch (InterruptedException e) { e.printStackTrace(); } computingTax test = new computingTax(); taxrate =test.taxRate(a,B)/>/>/>;// put the numbers in and have the math done to get the tax System.out.format("Your tax is %.2f\n", taxrate);//print out the tax rate. System.out.print("would you like to run again? (Y/N):\n"); S1= input.nextLine();//error starts here choice = S1.charAt(0); Character.toUpperCase(choice); if(choice=='Y'){ test.run(); } if(choice=='N'){ System.exit(0); } input.close(); } public static void main(String[] arg){ Scanner input = new Scanner (System.in); computingTax test = new computingTax(); test.run(); input.close(); } }