Results 1 to 7 of 7
- 10-14-2012, 05:31 AM #1
Member
- Join Date
- Oct 2012
- Posts
- 9
- Rep Power
- 0
Program does not want to proceed with options 1,2 and 3.
Hi everyone,
New to JAVA and this is my first post.
I'm following the book Learn to Program with Java from John Smiley. Since the program is available for free I don't think its a problem to print it out here.
Basically I seem to have written the program just like the book and I do not have any bugs or red lines to show I have mistakes. However, the program will answer back when I do not put the right number choice, or if I leave it blank and if I press cancel. These will all give me back an answer and than exit. However, the 1,2, and 3 response does not work. The GUI just remains blank and waits for another response. Any help appreciated.
XML Code:1 /* 2 * To change this template, choose Tools | Templates 3 * and open the template in the editor. 4 */ 5 package practice; 6 7 8 import javax.swing.JOptionPane; 9 10 public class Practice6_2 { 11 12 static double balance = 0; 13 static double newBalance = 0; 14 static double adjustment = 0; 15 16 public static void main(String[] args){ 17 String response; 18 String moreBankingBusiness; 19 moreBankingBusiness = 20 JOptionPane.showInputDialog 21 ("Do you want to do some banking?"); 22 moreBankingBusiness = moreBankingBusiness.toUpperCase(); 23 while (moreBankingBusiness.equals("YES")){ 24 response = JOptionPane.showInputDialog 25 ("What would you like to do? (1=Deposit, 2=Withdraw, 3=Get Balance)"); 26 if (response == null){ 27 JOptionPane.showMessageDialog 28 (null, "You clicked on the Cancel button"); 29 System.exit(0); 30 } 31 else 32 if (response.isEmpty()){ 33 JOptionPane.showMessageDialog 34 (null,"You must make an entry in the InputBox"); 35 System.exit(0); 36 } 37 else 38 if (Integer.parseInt(response) < 1 | 39 Integer.parseInt(response) > 3){ 40 JOptionPane.showMessageDialog 41 (null, response + "- is not a valid banking function"); 42 System.exit(0); 43 } 44 if (Integer.parseInt(response) == 1){ 45 makeDeposit(); 46 } 47 if (Integer.parseInt(response) == 2){ 48 makeWithdrawal(); 49 } 50 if (Integer.parseInt(response) == 3){ 51 getBalance(); 52 } 53 moreBankingBusiness = 54 JOptionPane.showInputDialog 55 ("Do you have more banking business?"); 56 moreBankingBusiness = 57 moreBankingBusiness.toUpperCase(); 58 }//end of while 59 JOptionPane.showMessageDialog 60 (null, "Thanks for banking with us!"); 61 System.exit(0); 62 }//end of main 63 64 private static void makeDeposit(){ 65 adjustment = Double.parseDouble 66 (JOptionPane.showInputDialog("Enter the Deposit Amount")); 67 newBalance = balance + adjustment; 68 JOptionPane.showMessageDialog 69 (null,"***SMILEY NATIONAL BANK***\n\n" + 70 "Old Balance is: " + balance + "\n" + 71 "Adjustment is: +" + adjustment + "\n" + 72 "New Balance is: " + newBalance + "\n"); 73 balance = newBalance; 74 } 75 private static void makeWithdrawal(){ 76 adjustment = Double.parseDouble 77 (JOptionPane.showInputDialog("Enter the Withdrawal Amount")); 78 newBalance = balance - adjustment; 79 JOptionPane.showMessageDialog 80 (null,"***SMILEY NATIONAL BANK***\n\n" + 81 "Old Balance is: " + balance + "\n" + 82 "Adjustment is: -" + adjustment + "\n" + 83 "New Balance is: " + newBalance + "\n"); 84 balance = newBalance; 85 } 86 private static void getBalance(){ 87 JOptionPane.showMessageDialog 88 (null, "***SMILEY NATIONAL BANK***\n\n" + 89 "Your Current Balance is: " + balance ); 90 } 91 }//end of classLast edited by JavaPete; 10-14-2012 at 08:23 PM. Reason: I made it a little more readable but dont know if its in the [code] format
- 10-14-2012, 08:51 AM #2
Senior Member
- Join Date
- Oct 2010
- Location
- Germany
- Posts
- 780
- Rep Power
- 4
Re: Program does not want to proceed with options 1,2 and 3.
I would replace the line
if (response.endsWith("")){
with
if (response.isEmpty()){
the endsWith("") is true in all cases....no idea what the author was thinking
- 10-14-2012, 05:07 PM #3
Re: Program does not want to proceed with options 1,2 and 3.
Hello and welcome! Please use [code][/code] tags when posting code so we can easily read it!
- 10-14-2012, 06:50 PM #4
Member
- Join Date
- Oct 2012
- Posts
- 9
- Rep Power
- 0
- 10-15-2012, 12:09 AM #5
Re: Program does not want to proceed with options 1,2 and 3.
You should try to repost the code without the line numbers. That would make it easier to look at.
"The secret to getting what you want is to reject everything that you don't want." -Wolbers
- 10-15-2012, 01:07 AM #6
Member
- Join Date
- Oct 2012
- Posts
- 9
- Rep Power
- 0
Re: Program does not want to proceed with options 1,2 and 3.
Ok I solved my problems.
Added else to the if statements and one of the braces were backwards
- 10-15-2012, 05:10 PM #7
Re: Program does not want to proceed with options 1,2 and 3.
Glad you got it worked out. In the future, use code tags as mentioned above, omit line numbers (no one will want to strip those out when trying to run your code) and post any/all compiler/runtime errors along with your code. Makes it much easier for us to help! Good luck!
Similar Threads
-
Finding the merged ranges in excel in a java program using jexcel Options
By srinivasany in forum New To JavaReplies: 1Last Post: 03-28-2012, 12:43 PM -
HELP - Creating OPTIONS in program and LOOPS
By whateverme in forum New To JavaReplies: 14Last Post: 12-07-2010, 05:55 AM -
Where to proceed? Applets or Swing?
By Mattedatten in forum New To JavaReplies: 4Last Post: 02-28-2010, 02:48 PM -
New to Java - not sure how to proceed with my new task
By matpj in forum New To JavaReplies: 15Last Post: 01-07-2009, 04:42 AM -
Where to proceed now?
By Ixnay in forum New To JavaReplies: 4Last Post: 07-29-2007, 07:16 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks