Search code examples
javaarraysreplaceall

How to store values in a new array by using replace method?


I want to store non "h" values in an array. So to give you a background, 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 stored the values with the HST in an array, but how would I store the non-HST values?

NOTE: I tried doing it as the same as my "h" values, but it would not work that's why I am confused

I cannot use Arrayslist or any other array methods

Sample input:

4.565H
H2.3435
4.565h
5.234
5.6576h

Sample Output:

HST Values:
4.565
2.3435
4.565
5.6576

Non-HST Values
5.234

This is What I tried But it won't work:

  // 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;
int index2 = 0;

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

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

while (go) {
  String value = inp.nextLine();
  value = 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");
  }else{
    numbers2[index2] = Double.parseDouble(value.replace("","")); // value.replace is an issue
  }
  counter++;
  if (counter == 5) {
    go = false;
  }
}
System.out.println("\nHST Values:");

for (int i = 0; i < numbers.length; i++) {

  // If there is any absence of values, print the HST values
  if (numbers[i] != null) {
    System.out.println(numbers[i]);
  }
}
System.out.println("\nNon-HST Values:");
for (int x = 0; x < numbers2.length; x++){
  if (numbers2[x] != null){
  System.out.println(numbers2[x]);
      }
    }
  }
}

Solution

  • Try this:

    Things I changed:

    1. numbers2[index2] = Double.parseDouble(value); // no need to replace anything here

    2. index2++ , I see you increment index but not index2

    3. When you print HST and non-HST value, you do not need to go till numbers.length or numbers2.length, because you know the values of index and index2, you already know the values in each array.

    If you do this way then you do not need to do null check when you are printing.

        import java.util.Scanner;
        public 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");
    
        int counter = 0;
        int index = 0;
        int index2 = 0;
    
       Double[] numbers = new Double[5];
        Double[] numbers2 = new Double[5];
    
       boolean go = true;
    
        while (go) {
            String value = inp.nextLine();
            value = 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");
            } else {
                numbers2[index2] = Double.parseDouble(value); // changed here
                index2++; //added this line
            }
            counter++;
            if (counter == 5) {
                go = false;
            }
        }
        System.out.println("\nHST Values:");
    
        for (int i = 0; i < index; i++) { // changed here
    
            // no need to do null check now
                System.out.println(numbers[i]);
    
        }
        System.out.println("\nNon-HST Values:");
        for (int x = 0; x < index2; x++) { // changed here
            // no need to do null check now
                System.out.println(numbers2[x]);
    
        }
        } }