Member Avatar for mehnihma

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?

// 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

Check out the toLowerCase() method and the toUpperCase() method in the java.lang.String class. These method's should let you convert the input so that it always matches what you are checking for.

either that, or, if you want more control, and you don't want for all entries to take both upper and lowercase (for instance: b, B and s are valid, but S is invalid)

case "b": performB();
          break;
case "B": performB();
          break;
case "s": performS();
          break;
Member Avatar for mehnihma

I've did it with String options = in.next().toLowerCase();

but now I have problems with getting out of a loop when a press any character other then B b S or s?

Can you help me with that?

well, you'll need to, either add the other options you want, or, just add

case "s": performS();
break;
default: // this will handle the actions you want for every other button pressed

Member Avatar for mehnihma

well, you'll need to, either add the other options you want, or, just add

case "s": performS();
break;
default: // this will handle the actions you want for every other button pressed

can you please explain this to me?
thanks

can you please explain this to me?
thanks

what explanation do you need on this part?? have you written the code you've shown us yourself?

case "S": performS();
          break;

the above snippet of code will let the performS() method run, when you enter the character "S".

so, if you want to perform certain actions when o is being pressed, you 'll need to add:

case "o": // write the code you want performed when "o" is pressed.
          break;

so, if you want both o and O to be taken into account, either you add another case "O" part, or you perform something like:

options = options.toLowerCase();
Member Avatar for mehnihma

I've done that
I was asking is there any other way for to do that.

And I've succeded to solve to brake the loop with :

default:
									if (!options.equalsIgnoreCase("s") && !options.equalsIgnoreCase("b"))
									System.out.println("No action taken");
System.exit(0)
 finished = true;
break;

not really.
see, if you have

case "s": performS();
          break;

you don't need to check for "s" in your default. default takes care of all the values that aren't specified in a case <value> block.
also, System.exit(0); doesn't just end your loop, it completely shuts down the JVM. so you will never actually reach the finished = true; line.
also, if default is the last of your options in the list, you don't have to put a break;
after it. that is only to make sure it won't run any other code after it in the switch-case block, but since there's no code after it, it's not necessary.

Member Avatar for mehnihma

I have added String options = in.next().toLowerCase(); for both S and s

I am new to java so I make a lot of mistakes...


Then which is the best way to check for any other characters and finish the loop?


In the assignment I have this, so I think I've done it ok?

7.	How do you terminate the program?  The exit method in the System class terminates the currently running Java Virtual Machine (JVM).  See javadocs.  The method has one parameter.  According to javadocs, “the argument serves as a status code; by convention, a nonzero status code indicates abnormal termination”. Status codes are important for a Windows batch file or Unix script file.  Here, you can use a value of 0 for the status code.

what do you mean with:
added String options = in.next().toLowerCase(); for both s and S???
with the option how you read it, you automatically do it for each value you enter.

Member Avatar for mehnihma

Sorry I wrote it because s and S where mentioned :D
Yes I know that

one more stupid question :D


I am not sure what do to continue the loop after some entered the numbers , to again give the options:

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 to exit!\n");
         System.out.println("Enter Option : ");

before the brake

ehm ... what exactly do you mean?

Member Avatar for mehnihma

I have done it with

do{

// 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 to exit!\n");
System.out.println("Enter Option : "); 

} while(true);

Thank you very much for your help,
I think I have done all now :D

ehm ... that is an infinite loop, since ... well, true will, by default, always be true :)

Member Avatar for mehnihma

yes
and I needed and infinite loop :D

Thanks

you do know you won't be able to (in a normal way) end your program like this?

Member Avatar for mehnihma

yes I know that
because program never ends
But this was asked of me so I did it this way

every program ends, just the way in which might change.

besides, if you code it like this:

do{
     
    // 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 to exit!\n");
    System.out.println("Enter Option : ");
     
    } while(true);

your program won't actually do anything, it will just repeatedly print these lines, you won't be able to enter any input, even though the text you are printing might suggest otherwise

Member Avatar for mehnihma

this is that part of the code

do{
					// 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 to exit!\n");
					System.out.println("Enter Option : "); 

						String options = in.next().toLowerCase();
						
						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);
							break;	
							
							}
							
							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);
							break;	
							
							} // 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);
							break;	
							
							}
							
							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);
							break;	
							
							} // end if else for selling stocks
							
							
							
							
							
							
							
							}	
							
							
							
							
							// default value for any other character
							default:
							if (!options.equalsIgnoreCase("s") && !options.equalsIgnoreCase("b"))
							System.out.println("No action taken");
							System.exit(0);
							
							finished = true;
							break;	
							}
							
							
							
							
							}
							} while(true);    // end main method
							
} } // end class TradeStock

well, in that case, your program will surely come to an end sooner or later.
you are creating a number of objects every time you go through the loop, which will (after a while) use all of your resources, after which you'll get StackOverFlowException

Member Avatar for mehnihma

since I have never programmed before and I still am heavily learning I think I did it somewhat ok :D
I am leaning java for less then a month

but thanks for your help!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.