首页 > 解决方案 > (已修复)如何在动态数组(文件、流、缓冲区)上只找到一个字符串值?

问题描述

我的小项目是关于通过 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);
  }
}

标签: javaarrays

解决方案


这是因为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


推荐阅读