0

So I need to make a basic cash register that accepts 5 items in an array with a price on them. Nevertheless, some items will have HST(tax) included with them. To know which items have tax and which don't. The user will press h or H before or after entering the dollar amount. I have got most of the program working, but I cannot get my code to recognize the upper case H to put the tax in it. Here is my code, Thx in advance for any information:

// 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();      
    value.toLowerCase();
    // 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]);
}
}
}

1 Answers1

0

I would remove indexOfh entirely: you can OR a bunch of statements together. This is safe because >= is evaluated before ||.

// Set the index value to "h" or "H"
boolean containsh = value.indexOf('h') >= 0
    || value.indexOf('H') >= 0;

Also, you can make your requirement a little tighter by using startsWith and endsWith to ignore an 'H' or 'h' in the middle.

// Set the index value to "h" or "H"
boolean containsh = value.startsWith("h")
    || value.startsWith("H") || value.endsWith("h")
    || value.endsWith("H");

Additionally, you have a resource leak because you never close your Scanner: you can use try-with-resources to have Java manage this for you. That, along with a few other simplifications, yields a new version like this.

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

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

        // Create scanner object and set scanner variables
        try (Scanner inp = new Scanner(System.in)) {
            System.out.println("Press any key to start");
            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];

            while (counter++ < 5) {
                String value = inp.nextLine();
                value.toLowerCase();

                // Set the index value to "h" or "H"
                boolean containsh = value.startsWith("h")
                        || value.startsWith("H") || value.endsWith("h")
                        || value.endsWith("H");

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

            for (int i = 0; i < numbers.length; i++) {
                System.out.println(numbers[i]);
            }
        }
    }
}

Results:

Press any key to start

Enter the amount of each item
Upto 5 inputs are allowed!

1
2H
HST will be taken account for this value
H3
HST will be taken account for this value
h4.7
HST will be taken account for this value
5.h6
HST Values:
2.0
3.0
4.7
0.0
0.0
Kevin Seymour
  • 766
  • 9
  • 25