Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/powerbi/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何使用replace方法在新数组中存储值?_Java_Arrays_Replaceall - Fatal编程技术网

Java 如何使用replace方法在新数组中存储值?

Java 如何使用replace方法在新数组中存储值?,java,arrays,replaceall,Java,Arrays,Replaceall,我想在数组中存储非“h”值。所以为了给你们一个背景,我需要做一个基本的收银机,它可以接受5个项目,每个项目都有一个价格。然而,有些项目将包含HST(税)。了解哪些项目有税,哪些没有税。用户将在输入美元金额之前或之后按h或h。我已经将HST的值存储在一个数组中,但是如何存储非HST的值呢 注意:我试着按照我的“h”值来做,但它不起作用,这就是我感到困惑的原因 我不能使用Arrayslist或任何其他数组方法 样本输入: 4.565H H2.3435 4.565h 5.234 5.6576h 样本

我想在数组中存储非“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]);
      }
    }
  }
}
//导入扫描程序类
导入java.util.Scanner;
//创建类和方法
班长{
公共静态void main(字符串[]args){
//创建扫描仪对象并设置扫描仪变量
扫描仪inp=新扫描仪(System.in);
System.out.println(“按任意键启动”);
String key=inp.nextLine();
System.out.println(“\n输入每个项目的金额”);
System.out.println(“最多允许5个输入!\n”);
//初始化计数器和索引变量以在while循环中使用它
int计数器=0;
int指数=0;
int index2=0;
//创建一个双数组变量,并将限制设置为5
Double[]数字=新的Double[5];
Double[]numbers2=新的双精度[5];
//创建一个布尔变量以在while循环中使用它
布尔go=true;
while(go){
字符串值=inp.nextLine();
value=value.toLowerCase();
//将索引值设置为“h”或“h”
int indexOfh=value.indexOf('h');
布尔containsh=indexOfh==0 | | indexOfh==(value.length()-1);
如果(containsh){//在开始或结束时验证h
numbers[index]=Double.parseDouble(value.replace(“h”)和“);
索引++;
System.out.println(“该值将考虑HST”);
}否则{
numbers2[index2]=Double.parseDouble(value.replace(“,”);//value.replace是个问题
}
计数器++;
如果(计数器==5){
go=假;
}
}
System.out.println(“\nHST值:”);
for(int i=0;i
试试这个:

我改变的事情:

  • numbers2[index2]=Double.parseDouble(值)//此处无需替换任何内容

  • index2++
    ,我看到您增加了
    index
    ,但不是
    index2

  • 当您打印HST和非HST值时,不需要一直打印到
    numbers.length
    numbers2.length
    ,因为您知道
    index
    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]);
    
        }
        } } 
    
    import java.util.Scanner;
    公共班机{
    公共静态void main(字符串[]args){
    //创建扫描仪对象并设置扫描仪变量
    扫描仪inp=新扫描仪(System.in);
    System.out.println(“按任意键启动”);
    String key=inp.nextLine();
    System.out.println(“\n输入每个项目的金额”);
    System.out.println(“最多允许5个输入!\n”);
    int计数器=0;
    int指数=0;
    int index2=0;
    Double[]数字=新的Double[5];
    Double[]numbers2=新的双精度[5];
    布尔go=true;
    while(go){
    字符串值=inp.nextLine();
    value=value.toLowerCase();
    //将索引值设置为“h”或“h”
    int indexOfh=value.indexOf('h');
    布尔containsh=indexOfh==0 | | indexOfh==(value.length()-1);
    如果(containsh){//在开始或结束时验证h
    numbers[index]=Double.parseDouble(value.replace(“h”)和“);
    索引++;
    System.out.println(“该值将考虑HST”);
    }否则{
    numbers2[index2]=Double.parseDouble(值);//在此处更改
    index2++;//添加了此行
    }
    计数器++;
    如果(计数器==5){
    go=假;
    }
    }
    System.out.println(“\nHST值:”);
    对于(inti=0;i
    试试这个:

    我改变的事情:

  • numbers2[index2]=Double.parseDouble(值)//此处无需替换任何内容

  • index2++
    ,我看到您增加了
    index
    ,但不是
    index2

  • 当您打印HST和非HST值时,不需要一直打印到
    numbers.length
    numbers2.length
    ,因为您知道
    index
    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]);
    
        }
        } } 
    
    import java.util.Scanner;
    公共班机{
    公共静态void main(字符串[]args){
    //创建扫描仪对象并设置扫描仪变量
    扫描仪inp=新扫描仪(System.in);
    System.out.println(“按任意键启动”);
    String key=inp.nextLine();
    System.out.println(“\n输入每个项目的金额”);
    System.out.println(“最多允许5个输入!\n”);
    int计数器=0;
    int指数=0;
    int index2=0;
    Double[]数字=新的Double[5];
    Double[]numbers2=新的双精度[5];
    布尔go=true;
    while(go){
    字符串值=