nid help in java....
Link |
by killerdevil
on 2008-02-04 06:42:39
|
hi guys!! i nid a little help in java... below shows the code which i have written... wad i nid help in is: 1) case 1: in the credit expenses (CCE), cannot have any negative $$... CODE: /** * @(#)CreditCardExpenses.java * * * * @author * @version 1.00 2007/12/15 */ import java.text.*; public class CreditCardExpenses { static int month, choice; static DecimalFormat fmt= new DecimalFormat ("0.00"); public static void main(String[] args) { double[][] CCE = new double[12][8]; String[][] accNum= new String[12][8]; String dummyHolder, q=""; int i=0, j=0, count = 0; int accCount=0; boolean canAdd = false; String[] monthList = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"}; do{ System.out.println("***********Credit Card Expenses Tracking***********"); System.out.println("1)Enter Credit card expenses by month"); System.out.println("2)Display detailed credit card expenses by month"); System.out.println("3)Quick glance at monthy total credit card expenses"); System.out.println("4)Exit"); System.out.print("Please select your choice <1-4>: "); q=CspInput.readString(); try { choice=Integer.parseInt(q); } catch (NumberFormatException e) { System.out.println("\nSorry, \""+q+"\" is not within the choice."); System.out.println("Please choose between choice 1-4 only.\n"); continue; } System.out.println("****************************************************\n"); switch (choice) { case 1:{ do{ System.out.print("Enter month <1 for Jan - 12 for Dec>: "); q=CspInput.readString(); try { month=Integer.parseInt(q); } catch (NumberFormatException e) { System.out.println("\nSorry, \""+q+"\" is not within the choice."); System.out.println("Please choose between choice 1-12 only.\n"); continue; } System.out.println("--------------------------------------"); System.out.println(monthList[month-1]+" expense accCount = 0; }while (month==0); while(true) { accCount++; System.out.print("Enter account number "+accCount+" dummyHolder = CspInput.readString(); if(dummyHolder.equals("")) break; count = 0; while(true) { canAdd = true; if(accNum[month-1][count]==null) break; if(accNum[month-1][count].equals(dummyHolder)) break; count++; if(count>7) { System.out.println("All accounts are used for "+monthList[month-1]+", unable to add new account!"); canAdd = false; break; } } if(canAdd) do{ accNum[month-1][count] = dummyHolder; System.out.print("Enter credit card expenses for "+accNum[month-1][count]+ ": $"); q = CspInput.readString(); try { CCE[month-1][count]=Double.parseDouble(q); } catch (NumberFormatException e) { System.out.println("\nSorry, \""+q+"\" is not within the choice."); System.out.println("Please enter numbers only.\n"); continue; } System.out.println("Updated "+ accNum[month-1][count] + " with $"+fmt.format(CCE[month-1][count])); }while (CCE[month-1][count]==0); } } break; case 2:{ System.out.print("Enter month <1 for Jan - 12 for Dec>: "); month= CspInput.readInt(); //if (accNum[month-1][0].length()!=0) double totalSpend = 0.0; for (i=1; i<8; i++) { if (accNum[month-1][i-1]==null) break; System.out.println(accNum[month-1][i-1]+"\t\t $"+fmt.format(CCE[month-1][i-1])); totalSpend = totalSpend + CCE[month-1][i-1]; } if(totalSpend>0) { System.out.println("\nTotal credit card expenses for the month of " +monthList[month-1]+" is $"+fmt.format(totalSpend)); } else System.out.println("No credit card expense entry for this month.\n\n"); } break; case 3: { double monthSpend; System.out.println("Monthly credit card expenses: "); for (i=0; i<12; i++) { monthSpend = 0.0; for(j=0; j<8; j++) { //if(accNum[i][j].length()==0) //check acc exist or not if(accNum[i][j]==null) break; monthSpend = monthSpend + CCE[i][j]; } if(monthSpend > 0) { if(monthSpend > 5000) System.out.println(monthList[i]+" \t $"+fmt.format(monthSpend)+" \t Credit card expenses for this month has exceeded $5000."); else System.out.println(monthList[i]+" \t $"+fmt.format(monthSpend)); } } } break; case 4:{ double monthSpend; for (i=0; i<12; i++) //loop the 12 months { monthSpend = 0.0; //reset the variable to 0 to cal the monthly spending for(j=0; j<8; j++) { //if(accNum[i][j].length()==0) //check acc exist or not if(accNum[i][j]==null) break; monthSpend = monthSpend + CCE[i][j]; } if(monthSpend > 0) { System.out.println("Expense for "+monthList[i]+" is $"+fmt.format(monthSpend)); } } System.out.println("\nThank you for using our Credit Card Expenses Tracking System."); } break; default: { System.out.println("ERROR!"); System.out.println("Please choose between choice 1-4 only.\n"); } } }while (choice!=4); } }
asd
|
Re: nid help in java....
Link |
by
on 2008-02-04 16:43:34 (edited 2008-02-04 16:43:47)
|
You're saying the CCE can't be entered negative, right? To test for that, change this:System.out.print("Enter credit card expenses for "+accNum[month-1][count]+ ": $"); q = CspInput.readString(); try { CCE[month-1][count]=Double.parseDouble(q); } to this: System.out.print("Enter credit card expenses for "+accNum[month-1][count]+ ": $"); q = CspInput.readString(); double p = Double.parseDouble(q); if(p < 0) { System.out.println("nSorry, ""+q+"" is not within the choice."); System.out.println("Please choose between choice 1-12 only.n"); } else { try { CCE[month-1][count] = p; } catch ... etc } Try that. Should work. Probably isn't the most high-tech way to do it but you get the idea. |