此程序需要从1个文本文件中读取“信用卡”号码,将它们与第2个进行比较,如果匹配,则将其与第3个进行比较,然后显示所有3个匹配的数字。
我遇到了很多麻烦,无法弄清楚什么是错的。任何帮助将不胜感激。
@echo off
set completepath=c:\users\microsoft\desktop\source
set destination=c:\users\microsoft\desktop\destination
set /a yesterday=%date:~4,2% - 1
set yesterday_date=%date:~10,2%%date:~7,2%%yesterday%
FOR /R %completepath% %%G IN (*.csv) DO call :process "%%~dpG" "%%~nG"
pause >nul
:process
SET %name%=%~2
SET chkname=%name:*%yesterday_date%=?%
IF "%chkname:~0,1%"=="?" (
xcopy %~1 %destination% /y
)
答案 0 :(得分:1)
我在这里看到了一些错误。
您永远不会在match = true
中设置main
因此会永远循环。
confirm = searchTwo(creditNumber);
if(confirm == true){
confirm = searchTwo(creditNumber);
System.out.println(confirm);
break;
}
这段代码是多余的。
仅当searchTwo()
返回searchThree()
时, true
才会返回。
您没有在while
循环中检查是否相等,您只是设置变量。
while(match = false & creditNumber != null)
应更改为:
while(match == false && creditNumber != null)
您需要在while循环中读取一个新行,而不只是在其外部读取一行。您目前的方式是,只读取文件的第一行。
答案 1 :(得分:0)
我创建了一个包含以下信用卡文件的简单项目:
<强> creditCards1.txt:强>
5416247983974134
5572941594858518
5110952365939493
5154933849210203
5272835420269481
<强> creditCards2.txt:强>
5562949040660851
5139055358731815
5154933849210203
5152652348339972
5382178174490350
5272835420269481
5564201755352300
5572941594858518
<强> creditCards3a.txt:强>
5121672532009148
5328229152859756
5272835420269481
5258620703451297
5154933849210203
5411765815619689
<强> creditCards3b.txt:强>
5121672532009148
5328229152859756
5258620703451297
5411765815619689
<强> creditCards4.txt:强>
5121672532009148
5328229152859756
5272835420269481
5258620703451297
5411765815619689
然后我创建了以下junit测试用例:
package creditcard.search;
import java.io.File;
import java.io.IOException;
import java.util.Set;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import org.junit.Before;
import org.junit.Test;
public class CreditCardSearchTest {
private CreditCardSearch creditCardSearch;
@Before
public void setUp() {
creditCardSearch = new CreditCardSearch();
creditCardSearch.addCreditCardsFile(new File("creditCards1.txt"));
creditCardSearch.addCreditCardsFile(new File("creditCards2.txt"));
}
@Test
public void assert_creditcards_found_in_2_files() throws IOException {
final Set<String> creditcards = creditCardSearch.searchForMatchesInAllFiles();
assertNotNull(creditcards);
assertEquals(3, creditcards.size());
assertTrue("5154933849210203", creditcards.contains("5154933849210203"));
assertTrue("5272835420269481", creditcards.contains("5272835420269481"));
assertTrue("5572941594858518", creditcards.contains("5572941594858518"));
}
@Test
public void assert_creditcards_found_in_3_files() throws IOException {
creditCardSearch.addCreditCardsFile(new File("creditCards3a.txt"));
final Set<String> creditcards = creditCardSearch.searchForMatchesInAllFiles();
assertNotNull(creditcards);
assertEquals(2, creditcards.size());
assertTrue("5154933849210203", creditcards.contains("5154933849210203"));
assertTrue("5272835420269481", creditcards.contains("5272835420269481"));
}
@Test
public void assert_creditcards_not_found_in_3_files() throws IOException {
creditCardSearch.addCreditCardsFile(new File("creditCards3b.txt"));
final Set<String> creditcards = creditCardSearch.searchForMatchesInAllFiles();
assertNotNull(creditcards);
assertTrue(creditcards.isEmpty());
}
@Test
public void assert_creditcards_found_in_4_files() throws IOException {
creditCardSearch.addCreditCardsFile(new File("creditCards4.txt"));
final Set<String> creditcards = creditCardSearch.searchForMatchesInAllFiles();
assertNotNull(creditcards);
assertEquals(1, creditcards.size());
assertTrue("5272835420269481", creditcards.contains("5272835420269481"));
}
}
最后但并非最不重要的是实施:
package creditcard.search;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
public class CreditCardSearch {
private final Set<File> files = new HashSet();
public void addCreditCardsFile(final File file) {
files.add(file);
}
public Set<String> searchForMatchesInAllFiles() throws IOException {
if(files.size() < 2) {
throw new IllegalArgumentException("a minimum of 2 creditcard files required");
}
final Map<Path, List<String>> linesPerPath = new HashMap<>();
for(final File file : files) {
final Path path = file.toPath();
linesPerPath.put(path, Files.readAllLines(path));
}
final Set<String> creditcards = new HashSet();
final Iterator<Path> it = linesPerPath.keySet().iterator();
List<String> linesA = null;
while (it.hasNext()) {
if (null == linesA) {
linesA = linesPerPath.get(it.next());
}
List<String> linesB = linesPerPath.get(it.next());
linesA.retainAll(linesB);
}
creditcards.addAll(linesA);
return creditcards;
}
}
junit测试结果:
-------------------------------------------------------------------------------
Test set: creditcard.search.CreditCardSearchTest
-------------------------------------------------------------------------------
Tests run: 4, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.105 sec
项目代码可以在GitHub找到:https://github.com/StefanHeimberg/stackoverflow-31952730