Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/353.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.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 我必须在一个特定的单词后面把一个数字变成一个整数_Java_Arrays_String_Integer - Fatal编程技术网

Java 我必须在一个特定的单词后面把一个数字变成一个整数

Java 我必须在一个特定的单词后面把一个数字变成一个整数,java,arrays,string,integer,Java,Arrays,String,Integer,我的代码读取整个.txt文档,并将内容放入字符串中。在我的文本文档中,有两个单词(Pos v=“1”和Qty v=“224”)。我必须用不同的整数来表示这些数字。比如int Pos和int Qty 我尝试拆分包含文本文档内容的字符串,并尝试获取元素的索引,然后将它们放入整数中 try { String content = new String ( Files.readAllBytes( Paths.get(filePath) ) ); //System.out.println(c

我的代码读取整个.txt文档,并将内容放入字符串中。在我的文本文档中,有两个单词(Pos v=“1”和Qty v=“224”)。我必须用不同的整数来表示这些数字。比如int Pos和int Qty

我尝试拆分包含文本文档内容的字符串,并尝试获取元素的索引,然后将它们放入整数中

try {
    String content = new String ( Files.readAllBytes( Paths.get(filePath) ) );
    //System.out.println(content);
    String[] datas = content.split("Pos v=\"");
    System.out.println(datas[300]);
} catch (IOException e) {
    //e.printStackTrace();
    System.out.println("Valami hiba van a megadott elérési úttal");
}


它没有任何错误,只是很难做到这一点。

我建议跟踪每个单词,一旦你得到你的单词,或者
Pos
或者
Qty
;从下一个单词中提取数字,该单词将是
v=“1”
(1可以替换为任何值)

int-pos;
整数数量;
//wordArray是用空格分隔符分割字符串形成的数组
for(int i=0;i
您的问题是什么?发布文件的实际内容和预期输出。我有两个不同的字符串。第一个是,第二个是。我想从他们那里得到数字,我想把数字分成两个不同的双倍。第一个double的名称是pos,第二个double的名称是qty。如果代码中没有错误,您可以使用
Integer.parseInt()
从字符串中创建一个int。谢谢,它可以工作,但我如何编辑它,以便能够存储double,而不仅仅是整数?
 int pos;
 int qty;

 // wordArray is an array formed by splitting string with space delimeter
 for(int i = 0; i < wordArray.length - 1; i++){
   // matching whether current word is "Pos" or  "Qty"
   if(wordArray[i].equals("Pos")) {
     // wordArray[i+1] = "v="1""; we are extracting only digit from that word
     pos = Integer.parseInt(wordArray[i+1].replaceAll("[\\D]", ""))
   } else if(wordArray[i].equals("Qty")) {
     qty = Integer.parseInt(wordArray[i+1].replaceAll("[\\D]", ""))
   }       
 }