Results 1 to 13 of 13
Like Tree1Likes
  • 1 Post By jMaster

Thread: Problem with case sensitive input

  1. #1
    mehnihma is offline Member
    Join Date
    Sep 2011
    Posts
    19
    Rep Power
    0

    Default Problem with case sensitive input

    I have a problem with case sensitive input
    my options are s and b and how to make a program when I input capital B or S to continue?


    Java Code:
    		 // options for selling and buying stocks
    					 System.out.println("\n");
    					 System.out.println("Options as single upper or lower case character : ");
    					 System.out.println("\tB to buy the stock");
    					 System.out.println("\tS to sell the stock");
    					 System.out.println("\tAny other character to exit!\n");
    				     System.out.println("Enter Option : "); 
    					 
    					 String options = in.next();
    				 										 										 
    					 boolean finished = false;
    					 
    					 
    					
    
    
    					 
    					 String brakeit = null;
    					// while loop for stock market simulation					 										
    						while (!finished)  {
    											
    						
    						// switch case selection structure
    					     switch (options) {
    											
    					     
    					  // buy stocks
    						case  "b" :
    							{
    																									
    						
    																						
    						if (stocks.calcCommission() > 500)
    																											
    							{
    					System.out.println("Cost of Shares : " + formatter.format(priceS * shareS));																									 
    				 System.out.println ("Commision : 500.00 ");
    				 System.out.print ("Total Cost : " +formatter.format(stocks.NewcalcValue() + stocks.newcalcCommision()) );
    				 System.out.println("\n");
    				 finished = true; 
    						  System.exit(0);
    																											 
    							}
    																										
    								else
    																											
    									{
    				System.out.println("Cost of Shares : " + formatter.format(priceS * shareS));																						
    				System.out.println ("Commision : " + formatter.format(stocks.newcalcCommision()));
    				System.out.print ("Total Cost : " + formatter.format(stocks.NewcalcValue() + stocks.newcalcCommision()));
    			    System.out.println("\n");
    				 finished = true; 
    						  System.exit(0);
    																														 
    								} // end if else for buying stocks 										
    																						
    							 }
    			 
    				
    											
    				// sell stocks
    								case  "s" :
    																	 
    		{
    																			
    				
    																					
    				if (stocks.calcCommission() > 500)
    																						
    						{
    				System.out.println("Receipts : " + formatter.format(priceS * shareS));																		
    				 System.out.println ("Commision : 500.00 ");
        			 System.out.print ("Net Receipts : " + formatter.format(stocks.NewcalcValue() + stocks.newcalcCommision()));
    				System.out.println();
    				finished = true; 
    						  System.exit(0);
    																						 
    				}
    																					
    		else
    																						
    		{
    		System.out.println("Receipts : " + formatter.format(priceS * shareS));																				
    		System.out.println ("Commision : " + formatter.format(stocks.newcalcCommision()));
    		System.out.print ("Net Receipts : " + formatter.format(stocks.NewcalcValue() + stocks.newcalcCommision()));	
    		System.out.println("\n");
    		finished = true; 
    						  System.exit(0);
    																						 
    		} // end if else for selling stocks
    																						 
    		 break;												
    			}
    															 
    							
    																	 
    			 }
    											 										 
    
    			 								 
    	 //loop scanner
    	 		 Scanner in2 = new Scanner (System.in);
    			 options = in2.next();
    											 
    					 // if user enters any character except s and b
    						 if (!options.equalsIgnoreCase("s") && !options.equalsIgnoreCase("b"))
    												
    						 {
    															  
    							  finished = true; 
    						  System.exit(0);
    												  
    												  // end if
    		
    						  	} // end while

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Problem with case sensitive input

    What if you change input to lowercase:

    Java Code:
    String options = in.next().toLowerCase();

  3. #3
    mehnihma is offline Member
    Join Date
    Sep 2011
    Posts
    19
    Rep Power
    0

    Default Re: Problem with case sensitive input

    I did and it's working :D

    Thanks !!

    But now I have problem getting out of the loop when I enter any other then s or b

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Problem with case sensitive input

    Quote Originally Posted by mehnihma View Post
    I did and it's working :D

    Thanks !!

    But now I have problem getting out of the loop when I enter any other then s or b
    I'm finding it very difficult to read your code given the non-standard formatting and indentations. Could you reformat your code so it conforms with standards, and so we can read it more easily and help you more easily?

  5. #5
    mehnihma is offline Member
    Join Date
    Sep 2011
    Posts
    19
    Rep Power
    0

    Default Re: Problem with case sensitive input

    Quote Originally Posted by Fubarable View Post
    I'm finding it very difficult to read your code given the non-standard formatting and indentations. Could you reformat your code so it conforms with standards, and so we can read it more easily and help you more easily?
    I am very new to Java
    so if you can tell me how to do that?

    Thanks

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Problem with case sensitive input

    The official Java coding standards can be found here: Code Conventions for the Java Programming Language
    A decent abbreviated version can be found here: Typography

    And the first statement in the last link sums it best I think: "Make your code look like the examples in the Java books"

  7. #7
    mehnihma is offline Member
    Join Date
    Sep 2011
    Posts
    19
    Rep Power
    0

    Default Re: Problem with case sensitive input

    Thanks
    I will try to do that
    but if you see what is wrong in this I would be very grateful.

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Problem with case sensitive input

    Quote Originally Posted by mehnihma View Post
    Thanks
    I will try to do that
    but if you see what is wrong in this I would be very grateful.
    Like most folks here, I'm a bit pressed for time with work and family obligations. I'm afraid that I'm going to have to hold off on any further analysis for now, but again will have a look if you can make it easy for me to see and understand your code. Best of luck.

  9. #9
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,560
    Blog Entries
    7
    Rep Power
    21

    Default Re: Problem with case sensitive input

    Quote Originally Posted by mehnihma View Post
    I did and it's working :D
    That's a miracle because you'r switching on a String value and Java just can't do that.

    kind regards,

    Jos

    edit: darn, Java 1.7 can do it ...
    Last edited by JosAH; 10-30-2011 at 03:30 PM.
    cenosillicaphobia: the fear for an empty beer glass

  10. #10
    mehnihma is offline Member
    Join Date
    Sep 2011
    Posts
    19
    Rep Power
    0

    Default Re: Problem with case sensitive input

    Quote Originally Posted by JosAH View Post
    That's a miracle because you'r switching on a String value and Java just can't do that.

    kind regards,

    Jos
    can you please explain?

  11. #11
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,560
    Blog Entries
    7
    Rep Power
    21

    Default Re: Problem with case sensitive input

    Quote Originally Posted by mehnihma View Post
    can you please explain?
    See the edit in my previous reply. Do you realize you're using features new to Java version 1.7?

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  12. #12
    mehnihma is offline Member
    Join Date
    Sep 2011
    Posts
    19
    Rep Power
    0

    Default Re: Problem with case sensitive input

    Yes I know

    is that a problem in this?
    Do you maybe know why I have problem with getting out of the loop?

  13. #13
    jMaster's Avatar
    jMaster is offline Member
    Join Date
    Oct 2011
    Posts
    16
    Rep Power
    0

    Default Re: Problem with case sensitive input

    Well you could delete the loop scanner part(the last bit of code) and inside your switch you could say this:

    default:
    (my edit, sorry you have to say finished = true in here too)
    System.exit(0);
    break;
    mehnihma likes this.

Similar Threads

  1. Problem with case - might need recursion
    By Angelar in forum New To Java
    Replies: 6
    Last Post: 10-13-2010, 02:25 PM
  2. Help on Java Input Case Insensitivity
    By Suzanne1187 in forum Java Applets
    Replies: 1
    Last Post: 02-10-2009, 09:40 AM
  3. Is Java Case Sensitive???? Prove It
    By j2vdk in forum New To Java
    Replies: 53
    Last Post: 09-01-2008, 09:54 AM
  4. MySQL Case Sensitive Problem
    By techissue2008 in forum JDBC
    Replies: 1
    Last Post: 06-10-2008, 06:23 AM
  5. problem with operator in case
    By jimJohnson in forum New To Java
    Replies: 2
    Last Post: 03-21-2008, 08:22 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •