这行Java代码中的indexOf是什么意思?

huangapple go评论112阅读模式
英文:

What does this particular line in java using indexOf mean?

问题

  1. numbers[index] = Double.parseDouble(value.replace("h", ""));

这行代码的含义是:将输入的值去除首尾的字符 "h",然后将剩余部分解析为一个双精度浮点数,并将其赋值给数组中指定索引位置的元素。

简单易懂的解释:

  1. value 是从用户输入中获取的字符串。
  2. value.replace("h", "") 是将字符串中的所有 "h" 替换为空字符串,这样就去除了首尾可能存在的 "h"。
  3. Double.parseDouble(...) 将经过处理的字符串转换为双精度浮点数。
  4. numbers[index] 是一个数组,用于存储浮点数值。index 是一个计数器,表示当前存储位置的索引。
  5. 所以,这行代码的作用是将经过处理的浮点数值存储到数组中的指定位置。

例如,如果用户输入了 "3.5h",经过处理后 "h" 被去除,剩下 "3.5" 被转换成浮点数,然后这个浮点数会被存储在 numbers 数组的特定位置,由 index 变量指示。

英文:

My Code:

  1. // Import scanner class
  2. import java.util.Scanner;
  3. // Create class and method
  4. class Main {
  5. public static void main(String[] args) {
  6. Scanner inp = new Scanner(System.in);
  7. System.out.println("Press any key to start");
  8. String key = inp.nextLine();
  9. System.out.println("\nEnter the amount of each item");
  10. System.out.println("Upto 5 inputs are allowed!\n");
  11. int counter = 0;
  12. int index = 0;
  13. double[] numbers = new double[5];
  14. boolean go = true;
  15. while(go) {
  16. String value = inp.nextLine();
  17. int indexOfH = value.indexOf("h");
  18. boolean containsH = indexOfH == 0 || indexOfH == (value.length()-1);
  19. if(containsH){ //Validate h at beginning or end
  20. numbers[index] = Double.parseDouble(value.replace("h", ""));
  21. System.out.println("HST will be taken account for this value");
  22. }
  23. counter++;
  24. if (counter == 5){
  25. go = false;
  26. }
  27. }
  28. System.out.println("Printing Valid values");
  29. for(int i=0; i< numbers.length; i++) {
  30. System.out.println(numbers[i]);
  31. }
  32. }
  33. }

What does this line in my code mean?: `numbers[index] = Double.parseDouble(value.replace("h", ""));

I am new to java arrays, so can you explain it in a simple and easy way?
`

答案1

得分: 2

  1. int indexOfH = value.indexOf("h");
  2. boolean containsH = indexOfH == 0 || indexOfH == (value.length()-1);
  3. `indexOfH` 是字符串中找到 `"h"` 的字符位置
  4. 更清楚的表达可以是
  5. int indexOfH = value.indexOf("h");
  6. boolean containsH = value.startsWith("h") || value.endsWith("h");
  7. 显然`h` 是一个标记
  8. 通过以下方式从 `value` 中去除标记
  9. String numValue = value.replace("h", "");
  10. 然后可以将其转换为双精度浮点数
  11. numbers[index] = Double.parseDouble(numValue);
  12. 因此`value` 可能包含 `"3.14h"` `"h2.89"`。)
  13. 另一个注意`value.indexOf('h')` 更加合乎逻辑因为现在是在查找单个字符的位置而不是整个字符串
英文:
  1. int indexOfH = value.indexOf("h");
  2. boolean containsH = indexOfH == 0 || indexOfH == (value.length()-1);

indexOfH is the char position in the string where "h" is found.
More clear would have been:

  1. int indexOfH = value.indexOf("h");
  2. boolean containsH = value.startsWith("h") || value.endsWith("h");

Evidently "h" was a marker.

The value is stripped by

  1. String numValue = value.replace("h", "");

And can then be converted to a double:

  1. numbers[index] = Double.parseDouble(numValue);

(So value might have contained "3.14h" or "h2.89".)

An other note: value.indexOf('h') would have been more logical, as now the position of a char instead of an entire String is sought.

huangapple
  • 本文由 发表于 2020年10月9日 22:54:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/64282463.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定