gpt4 book ai didi

java - (已修复)如何在动态数组(文件、流、缓冲区)上查找一个字符串值?

转载 作者:行者123 更新时间:2023-12-01 17:42:49 24 4
gpt4 key购买 nike

我的小项目是通过 do while 和 switch 将新数字添加到动态数组中。我的问题是,当我想搜索数组中存在的字符串值时,它在 if 中完全忽略了它。问题出在第三种情况 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);
}
}

所以我认为我的问题是我正在比较文本文件中的字符串值,我希望这听起来合乎逻辑哈哈哈,所以要修复它并找到您想要找到的确切数字,您应该使用模式和匹配器(或其他您经历过的解决方案):

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

关于java - (已修复)如何在动态数组(文件、流、缓冲区)上查找一个字符串值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60921290/

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