gpt4 book ai didi

JAVA,比较两个字符串

转载 作者:行者123 更新时间:2023-12-02 07:03:14 24 4
gpt4 key购买 nike

[已编辑}好的我明白了,让我重新表述一下。 numVol 为 45,文件内容为

54;a;23;c;de;56
23;d;24;c;h;456
45;87;c;y;535
432;42;h;h;543

但我仍然无法解决我的问题,它返回 543。所以我想做的是当它等于 numVol 时返回 line 但只检查一行的第一个数字。

<小时/>

我在比较两个字符串时遇到问题。假设我有一个包含以下内容的 .csv 文件:54;a;b;c;de 并且 numVol 值为 54。该方法应该返回 54,但是由于某种原因它没有进入“if”并返回“de”。

public static String test(int numVol)throws Exception{
File file = new File("test.csv");
Scanner scanner = new Scanner(file);
scanner.useDelimiter(";");
String line = "";
String sNumVol = ""+numVol; //create a string with numVol value in it
while (scanner.hasNext()){
line = scanner.next();
if(line.equals(sNumVol)){
scanner.close();
return line;
}
}
scanner.close();
return line;
}

最佳答案

问题是,既然您已经告诉 Scanner 使用 ; 作为分隔符,它就不再使用空格作为分隔符了。 。因此,针对 "45" 进行测试的 token 不是 "45",而是 "456\n45" (上一行的末尾、换行符和下一行的开头),这不匹配。

更改您的 useDelimiter 行以使用两者分号和空格作为分隔符:

scanner.useDelimiter("[;\\s]");

...然后扫描仪分别看到“456”“45”,并匹配“45”

这段代码:

import java.util.*;
import java.io.*;

public class Parse {
public static final void main(String[] args) {
try {
String result = test(45);
System.out.println("result = " + result);
}
catch (Exception e) {
System.out.println("Exception");
}
}

public static String test(int numVol)throws Exception{
File file = new File("test.csv");
Scanner scanner = new Scanner(file);
scanner.useDelimiter("[;\\s]"); // <==== Change is here
String line = "";
String sNumVol = ""+numVol;
while (scanner.hasNext()){
line = scanner.next();
if(line.equals(sNumVol)){
scanner.close();
return line;
}
}
scanner.close();
return line;
}
}

使用这个test.csv:

54;a;23;c;de;5623;d;24;c;h;45645;87;c;y;535432;42;h;h;543

Shows this:

$ java Parseresult = 45

The way to find the answer to this problem was simply to walk through the code with a debugger and watch the value of line, or (if for some reason you don't have a debugger?!), insert a System.out.println("line = " + line); statement into the loop to see what was being compared. For instance, if you insert a System.out.println("line = " + line); above the line = scanner.next(); line above and you just use ";" as the delimiter:

import java.util.*;
import java.io.*;

public class Parse {
public static final void main(String[] args) {
try {
String result = test(45);
System.out.println("result = " + result);
}
catch (Exception e) {
System.out.println("Exception");
}
}

public static String test(int numVol)throws Exception{
File file = new File("test.csv");
Scanner scanner = new Scanner(file);
scanner.useDelimiter(";"); // <== Just using ";"
String line = "";
String sNumVol = ""+numVol;
while (scanner.hasNext()){
line = scanner.next();
System.out.println("line = [[" + line + "]]");
if(line.equals(sNumVol)){
scanner.close();
return line;
}
}
scanner.close();
return line;
}
}

你会看到这个:

$ java Parseline = [[54]]line = [[a]]line = [[23]]line = [[c]]line = [[de]]line = [[5623]]line = [[d]]line = [[24]]line = [[c]]line = [[h]]line = [[45645]]line = [[87]]line = [[c]]line = [[y]]line = [[535432]]line = [[42]]line = [[h]]line = [[h]]line = [[543]]result = 543

...这有助于可视化问题。

关于JAVA,比较两个字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16389455/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com