如何使用替换方法将值存储在新数组中?

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

我想在数组中存储非“h”值。所以为了给你一个背景,我需要制作一个基本的收银机,它可以接受 5 件物品,并在上面标明价格。然而,有些商品将包含 HST(税)。了解哪些项目有税,哪些没有。用户将在输入美元金额之前或之后按 h 或 H。我已将具有 HST 的值存储在一个数组中,但我将如何存储非 HST 值?

注意:我试着按照我的“h”值来做,但它不起作用,这就是我感到困惑的原因

我不能使用 Arrayslist 或任何其他数组方法

示例输入:

4.565H
H2.3435
4.565h
5.234
5.6576h

示例输出:

HST Values:
4.565
2.3435
4.565
5.6576

Non-HST Values
5.234

这是我试过的但它不起作用:

  // 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]);
      }
    }
  }
}

试试这个:

我改变的东西:

  1. numbers2[index2] = Double.parseDouble(value); // 这里不需要替换任何东西

  2. index2++ ,我看到你递增 index 但不是 index2

  3. 当你打印 HST 和 non-HST 值时,你不需要去到 numbers.lengthnumbers2.length,因为你知道 [=13] 的值=] 和 index2,您已经知道每个数组中的值。

如果你这样做,那么你打印时就不需要做空检查了。

    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]);

    }
    } } 

non-HST 值解析失败的问题通常在 中得到解决,但是还有一些其他要点可以使代码更清晰:

  • 提供常量而不是数组的硬编码长度5
  • 删除多余的消息
  • 改进逻辑,重命名变量并删除多余的
  • 改进对用户输入错误的处理
  • 使用带有 case-insensitive 正则表达式的字符串 matches 方法来检查项目是否为 HST
public static void main(String[] args) {
    
    final int SIZE = 5;
    
    // Create a double array variable, and set the limit
    double[] hstItems = new double[SIZE];
    double[] nonHstItems = new double[SIZE];

    // Create scanner object and set scanner variables
    Scanner inp = new Scanner(System.in);
 
    System.out.printf("Enter up to %d HST or non-HST items%n", SIZE);

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

    while (counter++ < SIZE) {
        String value = inp.nextLine();
        try {
            if (value.matches("(?i)(h.+|.+h)")) {
                hstItems[hstCount++] = Double.parseDouble(value.replaceAll("(?i)h", ""));
                System.out.println("HST will be taken into account for this value");
            } else {
                nonHstItems[nonHstCount++] = Double.parseDouble(value);
                System.out.println("No HST is taken into account for this value");
            }
        } catch (Exception ex) {
            System.out.printf("Failed to parse value: %s, error: %s, please try again%n", value, ex.getMessage());
            counter--;
        }
    }

    System.out.println("\nHST Values:");
    for (int i = 0; i < hstCount; i++) {
        System.out.println(hstItems[i]);
    }

    System.out.println("\nNon-HST Values:");
    for (int i = 0; i < nonHstCount; i++) {
        System.out.println(nonHstItems[i]);
    }
}