(修复)如何在动态数组(File,Stream,Buffer)上只找到一个String值



我的小项目是通过do while和switch向动态数组添加新数字。我的问题是,当我想搜索数组中存在的字符串值时,它会在if中完全忽略它

package savearray;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.Scanner;
public class SaveArray {
public static int op, xWhile, nArray, array[], accumulator;
public static String strArray, strSeeNumber, newStringArray[];
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
do {
System.out.println("-----------------------------");
System.out.println("1: Add numbers | 2: Read numbers | 3: See existing number");
System.out.println("-----------------------------");
op = in.nextInt();
switch (op) {
case 1:
xWhile = 1;
accumulator++;
System.out.println("Size of the array:");
nArray = in.nextInt();
array = new int[nArray];
try {
for (int i = 0; i < nArray; i++) {
System.out.println("Enter the desired number");
array[i] = in.nextInt();
}
// Accumulator for when the operation is done more than 1 time, the array will not be null at the beginning, and when it is run a second time or more, don't override the strArray and act like a database, saving values until the program ends.
if (accumulator == 1) {
strArray = "";
}
for (int i : array) {
strArray += "[" + i + "]";
}
//Save array
FileOutputStream saveFile = new FileOutputStream("DB.txt");
OutputStreamWriter outWrite = new OutputStreamWriter(saveFile);
outWrite.write(strArray);
outWrite.flush();
outWrite.close();
saveFile.close();
System.out.println("File saved.");
} catch (IOException e) {
}
break;
case 2:
xWhile = 1;
try {
//Open array
FileInputStream saveFile = new FileInputStream("DB.txt");
InputStreamReader inReader = new InputStreamReader(saveFile);
BufferedReader buffRead = new BufferedReader(inReader);
String strArray = buffRead.readLine();
buffRead.close();
inReader.close();
saveFile.close();
//Print array
newStringArray = strArray.split(",");
for (String i : newStringArray) {
System.out.println(i);
}
} catch (IOException e) {
}
break;
case 3:
xWhile = 1;
in.skip("n");
System.out.println("Input the number:");
strSeeNumber = in.nextLine();
for (String newStringArray1 : newStringArray) {
//The problem is here:
if (newStringArray1.equals(strSeeNumber)) {
System.out.print("[" + newStringArray1 + "]");
} else {
System.out.println("Number not found, find an existing number again.");
}
}
break;
default:
xWhile = 0;
break;
}
} while (xWhile == 1);
}
}

所以我认为我的问题是,我在比较文本文件中的字符串值,我希望这听起来合乎逻辑哈哈哈,所以要修复它并找到你想找到的确切数字,你应该使用Pattern和Matcher(或你经历过的任何解决方案(:

package savearray;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.Scanner;
public class SaveArray {
public static int op, xWhile, nArray, array[], accumulator;
public static String strArray, strSeeN, newStringArray[];
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
do {
System.out.println("-----------------------------");
System.out.println("1: Add numbers | 2: Read numbers | 3: See existing number");
System.out.println("-----------------------------");
op = in.nextInt();
switch (op) {
case 1:
xWhile = 1;
accumulator;++;
System.out.println("Size of the array");
nArray = in.nextInt();
array = new int[nArray];
try {
for (int i = 0; i < nArray; i++) {
System.out.println("Enter the desired number");
array[i] = in.nextInt();
}
// Accumulator for when the operation is done more than 1 time, the array will not be null at the beginning, and when it is run a second time or more, don't override the strArray and act like a database, saving values until the program ends.
if (accumulator; == 1) {
strArray = "";
}
for (int i : array) {
strArray += "[" + i + "]";
}
//Save array
FileOutputStream saveFile = new FileOutputStream("DB.txt");
OutputStreamWriter outWrite = new OutputStreamWriter(saveFile);
outWrite.write(strArray);
outWrite.flush();
outWrite.close();
saveFile.close();
System.out.println("File saved.");
} catch (IOException e) {
}
break;
case 2:
xWhile = 1;
try {
//Open array
FileInputStream saveFile = new FileInputStream("DB.txt");
InputStreamReader inReader = new InputStreamReader(saveFile);
BufferedReader buffRead = new BufferedReader(inReader);
String strArray = buffRead.readLine();
buffRead.close();
inReader.close();
saveFile.close();
//Print array
newStringArray = strArray.split(",");
for (String i : newStringArray) {
System.out.println(i);
}
} catch (IOException e) {
}
break;
case 3:
xWhile = 1;
in.skip("n");
System.out.println("Enter the number:");
strSeeN = in.nextLine();
Pattern pattern = Pattern.compile(strSeeN);
Matcher matcher = pattern.matcher(strArray);
while (matcher.find()) {
System.out.println("Number found: " + matcher.group());
}
break;
default:
xWhile = 0;
break;
}
} while (xWhile == 1);
}
}

这是因为newStringArray中没有元素,如果您直接选择选项1,第二次选择选项3,它将抛出一个空指针异常。您忘记了从文件中读取内容。尝试阅读并进行比较。以及在写入文件时,在值后面附加一个逗号,否则拆分将不起作用。

int index=0;
for (int i : array) {
if(index==nArray-1)
strArray += "[" + i + "]";
else
strArray += "[" + i + "],";
index++;
}

在您的案例3 中修改以下内容

System.out.println("Input the number:");
strSeeNumber = in.nextLine();
try{
ileInputStream saveFile = new FileInputStream("DB.txt");
InputStreamReader inReader = new InputStreamReader(saveFile);
BufferedReader buffRead = new BufferedReader(inReader);
String strArray = buffRead.readLine();
buffRead.close();
inReader.close();
saveFile.close();//Print array
System.out.println(strArray);
newStringArray = strArray.replaceAll("\[", "").replaceAll("\]", "").replaceAll("\s", "").split(",");
for (String newStringArray1 : newStringArray) {
System.out.println(newStringArray1);
if (newStringArray1.equals(strSeeNumber)) {
System.out.println("Found Number -[" + newStringArray1 + "]");
} else {
System.out.println("Number not found, find an existing number again.");
}
}

在这里测试您的代码-它正在工作:https://onlinegdb.com/rJ5LhsC8L

相关内容

最新更新