0

I am creating a cash register where I have to use a scanner and can only have 5 input amounts. It has to also include HST (Harmonized Sales Tax) and that is by only having a "h" or "H" after or before an amount. With some help, I got the program to recognize the lowercase h, but how would I also make the program to recognize an uppercase "H"?

Code:

// Import scanner class
import java.util.Scanner;

// Create class and method
class Main {
public static void main(String[] args) {

// Create scanner object and set scanner variables
Scanner inp = new Scanner(System.in);
System.out.println("Press any key to start");
String key = inp.nextLine();
System.out.println("\nEnter the amount of each item");
System.out.println("Upto 5 inputs are allowed!\n");

// Initialize counter and index variables to use it in the while loop
int counter = 0;
int index = 0;

// Create a double array variable, and set the limit to 5
double[] numbers = new double[5];

// Create a boolean variable to use it in the while loop
boolean go = true;

while(go) {           
    String value = inp.nextLine();      
    
    // Set the index value to "h" or "H"
    int indexOfh = value.indexOf("h");
    
    boolean containsh = indexOfh == 0 || indexOfh == (value.length()-1);
    

    if(containsh){ //Validate h at beginning or end
        numbers[index] = Double.parseDouble(value.replace("h", ""));
        index++;
          System.out.println("HST will be taken account for this value");
    }
    counter++;
    if (counter == 5){
      go = false;
    }
}
System.out.println("HST Values:");

for(int i=0; i< numbers.length; i++) {
        System.out.println(numbers[i]);
     }
   }
 }
  • 2
    String has a method `toLowerCase` that returns the String in all lowercase (it doesn't modify the original, as String are immutable). You can just use that method to convert it to lower case and then you only have to check for the lower case "h": `value.toLowerCase().indexOf("h")` – OH GOD SPIDERS Oct 09 '20 at 16:34
  • Look. Someone asked the same [question](https://stackoverflow.com/questions/64283526/need-to-turn-char-into-lower-case-and-replace-it-in-java) and got an answer. Does it help you? – Abra Oct 09 '20 at 16:47
  • Does this answer your question? [Converting to upper and lower case in Java](https://stackoverflow.com/questions/2375649/converting-to-upper-and-lower-case-in-java) – DPWork Oct 12 '20 at 07:43

0 Answers0