我是 Java 的新手,所以我不知道太多花哨的东西而且我有点卡住了。
我需要做的是,获取一个文本文件;将内容导入 ArrayList
;检查每一行中两个“#”之间的特定 Char
;然后再次检查 ArrayList
中的每一行,看看其中一行是否已经有那个 Char
;如果是这样,那么我需要重新输入整个 String
行并重新检查它;如果没有,那么我需要将它放在 ArrayList
的底部并放入文本文件中。
到目前为止,我已经到了需要检查输入行的 Char
是否重复的部分,但是我只能做一次。例如,如果我再次输入它,我会得到一个 IndexOutOfBoundsException 因为其中一个循环。所以,我想我会使用 boolean
退出循环并重新进入它们,但不幸的是,在输入整个 String
一次后,它要求我这样做再次重复这个过程,因为我退出并进入了错误的循环,但我似乎无法弄清楚是哪一个。
ArrayList<String> Prices = new ArrayList<String>();
try (BufferedReader br = new BufferedReader (new FileReader("file.txt"))){
String CurrentLine;
while ((CurrentLine = br.readLine()) != null){
Prices.add(CurrentLine);
}
}catch (IOException e) {
e.printStackTrace();
}
boolean CheckSequence = true;
boolean Redo = false;
String StringArrayPrices[] = new String[Prices.size()];
Scanner sc = new Scanner (System.in);
while (CheckSequence == true){
System.out.println("Insert product in the following format, 'Product#Letter#Number': ");
String ProductString = sc.nextLine();
char ProductArray[] = ProductString.toCharArray();
for (int i = 0; i < Prices.size(); i++){
StringArrayPrices[i] = Prices.get(i);
char CharArrayPrice[] = StringArrayPrices[i].toCharArray();
for (int j = 0; j < CharArrayPrice.length && Redo == false; j++){
if (CharArrayPrice[j] == '#' && CharArrayPrice[j+2] == '#'){
char TemporaryLetter = CharArrayPrice[j];
for (int k = 0; k < ProductArray.length && Redo == false; k++){
if (ProductArray[k] == '#' && ProductArray[k+2] == '#'){
char TemporaryLetter2 = ProductArray[k];
if (TemporaryLetter == TemporaryLetter2){
System.out.println("The letter is repeating. Enter product again or exit by writing '-1': ");
ProductString = sc.nextLine();
if (ProductString == "-1"){
CheckSequence = false;
}
Redo = true;
}
if (TemporaryLetter != TemporaryLetter2){
Prices.add(ProductString);
}
}
}
}
}
}
}
现在,当我重新输入 String
时,我收到第一条消息“”Insert product in the following format, 'Product#Letter#Number': "”,而不是“”字母在重复。再次输入产品或通过写入'-1':“”退出。
第一行应该只在我输入一个全新的 String
时出现,而不是在前一个字符串重复时出现。
我希望我没有让这太困惑。
最佳答案
所有的循环都不是要走的路。
我不完全确定要求,但我认为这符合要求。
请注意,您可能应该定义一个 Product
类来封装大部分内容。
List<String> prices;
Set<String> letters;
try (BufferedReader reader = new BufferedReader(new FileReader("file.txt"))) {
prices = reader.lines.collect(Collectors.toList());
}
letters = prices.stream.map(p -> getLetter(p)).collect(Collectors.toSet());
Scanner scanner = new Scanner(System.in);
boolean shouldContinue = true;
while (shouldContinue) {
System.out.println("Enter Product");
String product = scanner.nextLine();
if (product.equals("-1")) {
shouldContinue = false;
} else {
String letter = getLetter(product);
if (letters.contains(letter) {
System.out.println("Repeat. Re-enter");
} else {
prices.add(product);
letters.add(letter);
}
}
}
与 helper
private String getLetter(String s) {
return s.split("#")[1]; //Or use the appropriate regex to extract the letter.
}
可以使用一些错误处理。
为了避免 java 8 的流,您可以使用与问题中相同的代码来构建 prices
和
Set<String> letters = new HashSet<>();
for (String price : prices) {
letters.add(getLetter(price));
}
关于java - 使用 boolean 值退出循环然后重做循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43894213/